<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\SiteBriefing\StoreSiteBriefingRequest;
use App\Http\Requests\SiteBriefing\UpdateSiteBriefingRequest;
use App\Services\SiteBriefingService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
 * 현장설명회 관리 컨트롤러
 */
class SiteBriefingController extends Controller
{
    public function __construct(
        private readonly SiteBriefingService $service
    ) {}

    /**
     * 현장설명회 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->only([
            'search',
            'status',
            'bid_status',
            'partner_id',
            'site_id',
            'start_date',
            'end_date',
            'sort_by',
            'sort_dir',
            'page',
            'per_page',
        ]);

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

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

    /**
     * 현장설명회 상세 조회
     */
    public function show(int $id): JsonResponse
    {
        $siteBriefing = $this->service->show($id);

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

    /**
     * 현장설명회 등록
     */
    public function store(StoreSiteBriefingRequest $request): JsonResponse
    {
        $siteBriefing = $this->service->store($request->validated());

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

    /**
     * 현장설명회 수정
     */
    public function update(UpdateSiteBriefingRequest $request, int $id): JsonResponse
    {
        $siteBriefing = $this->service->update($id, $request->validated());

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

    /**
     * 현장설명회 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        $this->service->destroy($id);

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

    /**
     * 현장설명회 일괄 삭제
     */
    public function bulkDestroy(Request $request): JsonResponse
    {
        $request->validate([
            'ids' => 'required|array|min:1',
            'ids.*' => 'integer',
        ]);

        $deletedCount = $this->service->bulkDestroy($request->input('ids'));

        return ApiResponse::success(
            ['deleted_count' => $deletedCount],
            __('message.bulk_deleted')
        );
    }

    /**
     * 현장설명회 통계 조회
     */
    public function stats(): JsonResponse
    {
        $stats = $this->service->stats();

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