<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\TaxInvoice\CancelTaxInvoiceRequest;
use App\Http\Requests\TaxInvoice\CreateTaxInvoiceRequest;
use App\Http\Requests\TaxInvoice\SaveSupplierSettingsRequest;
use App\Http\Requests\TaxInvoice\TaxInvoiceListRequest;
use App\Http\Requests\TaxInvoice\TaxInvoiceSummaryRequest;
use App\Http\Requests\TaxInvoice\UpdateTaxInvoiceRequest;
use App\Http\Requests\V1\TaxInvoice\BulkIssueRequest;
use App\Services\TaxInvoiceService;

class TaxInvoiceController extends Controller
{
    public function __construct(
        private TaxInvoiceService $taxInvoiceService
    ) {}

    /**
     * 세금계산서 목록 조회
     */
    public function index(TaxInvoiceListRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->list($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 세금계산서 상세 조회
     */
    public function show(int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->show($id),
            __('message.fetched')
        );
    }

    /**
     * 세금계산서 생성
     */
    public function store(CreateTaxInvoiceRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->create($request->validated()),
            __('message.created')
        );
    }

    /**
     * 세금계산서 수정
     */
    public function update(UpdateTaxInvoiceRequest $request, int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->update($id, $request->validated()),
            __('message.updated')
        );
    }

    /**
     * 세금계산서 삭제
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->delete($id),
            __('message.deleted')
        );
    }

    /**
     * 세금계산서 발행
     */
    public function issue(int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->issue($id),
            __('message.tax_invoice.issued')
        );
    }

    /**
     * 세금계산서 일괄 발행
     */
    public function bulkIssue(BulkIssueRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->bulkIssue($request->getIds()),
            __('message.tax_invoice.bulk_issued')
        );
    }

    /**
     * 세금계산서 취소
     */
    public function cancel(CancelTaxInvoiceRequest $request, int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->cancel($id, $request->validated()['reason']),
            __('message.tax_invoice.cancelled')
        );
    }

    /**
     * 국세청 전송 상태 조회
     */
    public function checkStatus(int $id)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->checkStatus($id),
            __('message.fetched')
        );
    }

    /**
     * 세금계산서 요약 통계
     */
    public function summary(TaxInvoiceSummaryRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->summary($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 공급자 설정 조회
     */
    public function supplierSettings()
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->getSupplierSettings(),
            __('message.fetched')
        );
    }

    /**
     * 공급자 설정 저장
     */
    public function saveSupplierSettings(SaveSupplierSettingsRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->saveSupplierSettings($request->validated()),
            __('message.updated')
        );
    }

    /**
     * 세금계산서 생성 + 즉시 발행
     */
    public function storeAndIssue(CreateTaxInvoiceRequest $request)
    {
        return ApiResponse::handle(
            fn () => $this->taxInvoiceService->createAndIssue($request->validated()),
            __('message.tax_invoice.issued')
        );
    }
}
