<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Purchase\BulkUpdatePurchaseTypeRequest;
use App\Http\Requests\V1\Purchase\BulkUpdateTaxReceivedRequest;
use App\Http\Requests\V1\Purchase\StorePurchaseRequest;
use App\Http\Requests\V1\Purchase\UpdatePurchaseRequest;
use App\Services\PurchaseService;
use Illuminate\Http\Request;

class PurchaseController extends Controller
{
    public function __construct(
        private readonly PurchaseService $service
    ) {}

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

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

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

    /**
     * 매입 등록
     */
    public function store(StorePurchaseRequest $request)
    {
        $purchase = $this->service->store($request->validated());

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

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

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

    /**
     * 매입 수정
     */
    public function update(int $id, UpdatePurchaseRequest $request)
    {
        $purchase = $this->service->update($id, $request->validated());

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

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

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

    /**
     * 매입 확정
     */
    public function confirm(int $id)
    {
        $purchase = $this->service->confirm($id);

        return ApiResponse::success($purchase, __('message.purchase.confirmed'));
    }

    /**
     * 대시보드 상세 조회 (CEO 대시보드 모달용)
     */
    public function dashboardDetail()
    {
        $data = $this->service->dashboardDetail();

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

    /**
     * 매입 요약 (기간별 합계)
     */
    public function summary(Request $request)
    {
        $params = $request->only([
            'start_date',
            'end_date',
            'client_id',
            'status',
        ]);

        $summary = $this->service->summary($params);

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

    /**
     * 매입유형 일괄 변경
     */
    public function bulkUpdatePurchaseType(BulkUpdatePurchaseTypeRequest $request)
    {
        $updatedCount = $this->service->bulkUpdatePurchaseType(
            $request->getIds(),
            $request->getPurchaseType()
        );

        return ApiResponse::success(
            ['updated_count' => $updatedCount],
            __('message.bulk_updated')
        );
    }

    /**
     * 세금계산서 수취 일괄 설정
     */
    public function bulkUpdateTaxReceived(BulkUpdateTaxReceivedRequest $request)
    {
        $updatedCount = $this->service->bulkUpdateTaxReceived(
            $request->getIds(),
            $request->getTaxInvoiceReceived()
        );

        return ApiResponse::success(
            ['updated_count' => $updatedCount],
            __('message.bulk_updated')
        );
    }
}
