<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Pricing\PriceBulkDeleteRequest;
use App\Http\Requests\Pricing\PriceByItemsRequest;
use App\Http\Requests\Pricing\PriceCostRequest;
use App\Http\Requests\Pricing\PriceIndexRequest;
use App\Http\Requests\Pricing\PriceStoreRequest;
use App\Http\Requests\Pricing\PriceUpdateRequest;
use App\Services\PricingService;

class PricingController extends Controller
{
    public function __construct(
        protected PricingService $service
    ) {}

    /**
     * 단가 목록 조회
     */
    public function index(PriceIndexRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->index($request->validated());

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 단가 상세 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $data = $this->service->show($id);

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 단가 등록
     */
    public function store(PriceStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->store($request->validated());

            return ['data' => $data, 'message' => __('message.created'), 'statusCode' => 201];
        });
    }

    /**
     * 단가 수정
     */
    public function update(PriceUpdateRequest $request, int $id)
    {
        return ApiResponse::handle(function () use ($request, $id) {
            $data = $this->service->update($id, $request->validated());

            return ['data' => $data, 'message' => __('message.updated')];
        });
    }

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

            return ['data' => null, 'message' => __('message.deleted')];
        });
    }

    /**
     * 단가 확정
     */
    public function finalize(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $data = $this->service->finalize($id);

            return ['data' => $data, 'message' => __('message.pricing.finalized')];
        });
    }

    /**
     * 품목별 단가 현황 조회
     */
    public function byItems(PriceByItemsRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->byItems($request->validated());

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 변경 이력 조회
     */
    public function revisions(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $data = $this->service->revisions($id);

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 원가 조회 (수입검사 > 표준원가 fallback)
     */
    public function cost(PriceCostRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $data = $this->service->getCost($request->validated());

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 단가 통계 조회
     */
    public function stats()
    {
        return ApiResponse::handle(function () {
            $data = $this->service->stats();

            return ['data' => $data, 'message' => __('message.fetched')];
        });
    }

    /**
     * 단가 일괄 삭제
     */
    public function bulkDestroy(PriceBulkDeleteRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $validated = $request->validated();
            $deletedCount = $this->service->bulkDestroy($validated['ids']);

            return ['data' => ['deleted_count' => $deletedCount], 'message' => __('message.deleted')];
        });
    }
}
