<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Labor\LaborBulkDeleteRequest;
use App\Http\Requests\Labor\LaborIndexRequest;
use App\Http\Requests\Labor\LaborStoreRequest;
use App\Http\Requests\Labor\LaborUpdateRequest;
use App\Services\LaborService;

class LaborController extends Controller
{
    public function __construct(
        protected LaborService $service
    ) {}

    /**
     * 노임 목록 조회
     */
    public function index(LaborIndexRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->index($request->validated());

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 노임 통계 조회
     */
    public function stats()
    {
        return ApiResponse::handle(function () {
            $data = $this->service->stats();

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 노임 상세 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $data = $this->service->show($id);

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 노임 등록
     */
    public function store(LaborStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->store($request->validated());

            return ['data' => $data, 'message' => __('message.created'), 'statusCode' => 201];
        });
    }

    /**
     * 노임 수정
     */
    public function update(LaborUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            $data = $this->service->update($id, $request->validated());

            return ['data' => $data, 'message' => __('message.updated')];
        });
    }

    /**
     * 노임 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $this->service->destroy($id);

            return ['data' => null, 'message' => __('message.deleted')];
        });
    }

    /**
     * 노임 일괄 삭제
     */
    public function bulkDestroy(LaborBulkDeleteRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $validated = $request->validated();
            $deletedCount = $this->service->bulkDestroy($validated['ids']);

            return ['data' => ['deleted_count' => $deletedCount], 'message' => __('message.deleted')];
        });
    }
}
