<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Popup\StorePopupRequest;
use App\Http\Requests\V1\Popup\UpdatePopupRequest;
use App\Services\PopupService;
use Illuminate\Http\Request;

class PopupController extends Controller
{
    public function __construct(
        private readonly PopupService $service
    ) {}

    /**
     * 팝업 목록 (관리자용)
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'target_type',
            'status',
            'search',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

        $popups = $this->service->index($params);

        return ApiResponse::success($popups, __('message.fetched'));
    }

    /**
     * 활성 팝업 목록 (사용자용)
     */
    public function active(Request $request)
    {
        $departmentId = $request->input('department_id');

        $popups = $this->service->getActivePopups($departmentId);

        return ApiResponse::success($popups, __('message.fetched'));
    }

    /**
     * 팝업 등록
     */
    public function store(StorePopupRequest $request)
    {
        $popup = $this->service->store($request->validated());

        return ApiResponse::success($popup, __('message.created'), [], 201);
    }

    /**
     * 팝업 상세
     */
    public function show(int $id)
    {
        $popup = $this->service->show($id);

        return ApiResponse::success($popup, __('message.fetched'));
    }

    /**
     * 팝업 수정
     */
    public function update(int $id, UpdatePopupRequest $request)
    {
        $popup = $this->service->update($id, $request->validated());

        return ApiResponse::success($popup, __('message.updated'));
    }

    /**
     * 팝업 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

        return ApiResponse::success(null, __('message.deleted'));
    }
}
