<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProductionOrder\ProductionOrderIndexRequest;
use App\Services\ProductionOrderService;
use Illuminate\Http\JsonResponse;

class ProductionOrderController extends Controller
{
    public function __construct(
        private readonly ProductionOrderService $service
    ) {}

    /**
     * 생산지시 목록 조회
     */
    public function index(ProductionOrderIndexRequest $request): JsonResponse
    {
        $result = $this->service->index($request->validated());

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

    /**
     * 생산지시 상태별 통계
     */
    public function stats(): JsonResponse
    {
        $stats = $this->service->stats();

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

    /**
     * 생산지시 상세 조회
     */
    public function show(int $orderId): JsonResponse
    {
        try {
            $detail = $this->service->show($orderId);

            return ApiResponse::success($detail, __('message.fetched'));
        } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
            return ApiResponse::error(__('error.order.not_found'), 404);
        }
    }
}
