<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Receiving\ProcessReceivingRequest;
use App\Http\Requests\V1\Receiving\StoreReceivingRequest;
use App\Http\Requests\V1\Receiving\UpdateReceivingRequest;
use App\Services\ReceivingService;
use Illuminate\Http\Request;

class ReceivingController extends Controller
{
    public function __construct(
        private readonly ReceivingService $service
    ) {}

    /**
     * 입고 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'status',
            'start_date',
            'end_date',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 입고 통계
     */
    public function stats()
    {
        $stats = $this->service->stats();

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

    /**
     * 입고 등록
     */
    public function store(StoreReceivingRequest $request)
    {
        $receiving = $this->service->store($request->validated());

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

    /**
     * 입고 상세
     */
    public function show(int $id)
    {
        $receiving = $this->service->show($id);

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

    /**
     * 입고 수정
     */
    public function update(int $id, UpdateReceivingRequest $request)
    {
        $receiving = $this->service->update($id, $request->validated());

        return ApiResponse::success($receiving, __('message.updated'));
    }

    /**
     * 입고 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

        return ApiResponse::success(null, __('message.deleted'));
    }

    /**
     * 입고처리 (상태 변경 + 입고 정보 입력)
     */
    public function process(int $id, ProcessReceivingRequest $request)
    {
        $receiving = $this->service->process($id, $request->validated());

        return ApiResponse::success($receiving, __('message.receiving.processed'));
    }
}
