<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Shipment\ShipmentStoreRequest;
use App\Http\Requests\Shipment\ShipmentUpdateRequest;
use App\Http\Requests\Shipment\ShipmentUpdateStatusRequest;
use App\Services\ShipmentService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ShipmentController extends Controller
{
    public function __construct(
        private readonly ShipmentService $service
    ) {}

    /**
     * 출하 목록 조회
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->only([
            'search',
            'status',
            'priority',
            'delivery_method',
            'scheduled_from',
            'scheduled_to',
            'can_ship',
            'deposit_confirmed',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

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

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

    /**
     * 상태별 통계 조회 (탭용)
     */
    public function statsByStatus(): JsonResponse
    {
        $stats = $this->service->statsByStatus();

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

    /**
     * 출하 상세 조회
     */
    public function show(int $id): JsonResponse
    {
        try {
            $shipment = $this->service->show($id);

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

    /**
     * 출하 생성
     */
    public function store(ShipmentStoreRequest $request): JsonResponse
    {
        $shipment = $this->service->store($request->validated());

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

    /**
     * 출하 수정
     */
    public function update(ShipmentUpdateRequest $request, int $id): JsonResponse
    {
        try {
            $shipment = $this->service->update($id, $request->validated());

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

    /**
     * 출하 상태 변경
     */
    public function updateStatus(ShipmentUpdateStatusRequest $request, int $id): JsonResponse
    {
        try {
            $shipment = $this->service->updateStatus(
                $id,
                $request->validated('status'),
                $request->validated()
            );

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

    /**
     * 출하 삭제
     */
    public function destroy(int $id): JsonResponse
    {
        try {
            $this->service->delete($id);

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

    /**
     * LOT 옵션 조회
     */
    public function lotOptions(): JsonResponse
    {
        $options = $this->service->getLotOptions();

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

    /**
     * 물류사 옵션 조회
     */
    public function logisticsOptions(): JsonResponse
    {
        $options = $this->service->getLogisticsOptions();

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

    /**
     * 차량 톤수 옵션 조회
     */
    public function vehicleTonnageOptions(): JsonResponse
    {
        $options = $this->service->getVehicleTonnageOptions();

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