<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\WorkResult\WorkResultStoreRequest;
use App\Http\Requests\WorkResult\WorkResultUpdateRequest;
use App\Services\WorkResultService;
use Illuminate\Http\Request;

class WorkResultController extends Controller
{
    public function __construct(private WorkResultService $service) {}

    /**
     * 목록 조회
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, __('message.work_result.fetched'));
    }

    /**
     * 통계 조회
     */
    public function stats(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->stats($request->all());
        }, __('message.work_result.fetched'));
    }

    /**
     * 단건 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.work_result.fetched'));
    }

    /**
     * 생성
     */
    public function store(WorkResultStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.work_result.created'));
    }

    /**
     * 수정
     */
    public function update(WorkResultUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            return $this->service->update($id, $request->validated());
        }, __('message.work_result.updated'));
    }

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

            return 'success';
        }, __('message.work_result.deleted'));
    }

    /**
     * 검사 상태 토글
     */
    public function toggleInspection(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->toggleInspection($id);
        }, __('message.work_result.inspection_toggled'));
    }

    /**
     * 포장 상태 토글
     */
    public function togglePackaging(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->togglePackaging($id);
        }, __('message.work_result.packaging_toggled'));
    }
}
