<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\StockService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class StockController extends Controller
{
    public function __construct(
        private readonly StockService $service
    ) {}

    /**
     * 재고 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->only([
            'search',
            'item_type',
            'item_category',
            'status',
            'location',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
            'start_date',
            'end_date',
        ]);

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

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

    /**
     * 재고 통계 조회
     */
    public function stats(): JsonResponse
    {
        $stats = $this->service->stats();

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

    /**
     * 재고 상세 조회 (LOT 포함)
     */
    public function show(int $id): JsonResponse
    {
        try {
            $stock = $this->service->show($id);

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

    /**
     * 품목유형별 통계 조회
     */
    public function statsByItemType(): JsonResponse
    {
        $stats = $this->service->statsByItemType();

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