<?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\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)
    {
        $taxInvoices = $this->taxInvoiceService->list($request->validated());

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

    /**
     * 세금계산서 상세 조회
     */
    public function show(int $id)
    {
        $taxInvoice = $this->taxInvoiceService->show($id);

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

    /**
     * 세금계산서 생성
     */
    public function store(CreateTaxInvoiceRequest $request)
    {
        $taxInvoice = $this->taxInvoiceService->create($request->validated());

        return ApiResponse::handle(
            data: $taxInvoice,
            message: __('message.created'),
            status: 201
        );
    }

    /**
     * 세금계산서 수정
     */
    public function update(UpdateTaxInvoiceRequest $request, int $id)
    {
        $taxInvoice = $this->taxInvoiceService->update($id, $request->validated());

        return ApiResponse::handle(
            data: $taxInvoice,
            message: __('message.updated')
        );
    }

    /**
     * 세금계산서 삭제
     */
    public function destroy(int $id)
    {
        $this->taxInvoiceService->delete($id);

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

    /**
     * 세금계산서 발행
     */
    public function issue(int $id)
    {
        $taxInvoice = $this->taxInvoiceService->issue($id);

        return ApiResponse::handle(
            data: $taxInvoice,
            message: __('message.tax_invoice.issued')
        );
    }

    /**
     * 세금계산서 일괄 발행
     */
    public function bulkIssue(BulkIssueRequest $request)
    {
        $result = $this->taxInvoiceService->bulkIssue($request->getIds());

        return ApiResponse::handle(
            data: $result,
            message: __('message.tax_invoice.bulk_issued')
        );
    }

    /**
     * 세금계산서 취소
     */
    public function cancel(CancelTaxInvoiceRequest $request, int $id)
    {
        $taxInvoice = $this->taxInvoiceService->cancel($id, $request->validated()['reason']);

        return ApiResponse::handle(
            data: $taxInvoice,
            message: __('message.tax_invoice.cancelled')
        );
    }

    /**
     * 국세청 전송 상태 조회
     */
    public function checkStatus(int $id)
    {
        $taxInvoice = $this->taxInvoiceService->checkStatus($id);

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

    /**
     * 세금계산서 요약 통계
     */
    public function summary(TaxInvoiceSummaryRequest $request)
    {
        $summary = $this->taxInvoiceService->summary($request->validated());

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