<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Common\BulkUpdateAccountCodeRequest;
use App\Http\Requests\V1\Sale\BulkIssueStatementRequest;
use App\Http\Requests\V1\Sale\SendStatementRequest;
use App\Http\Requests\V1\Sale\StoreSaleRequest;
use App\Http\Requests\V1\Sale\UpdateSaleRequest;
use App\Services\SaleService;
use Illuminate\Http\Request;

class SaleController extends Controller
{
    public function __construct(
        private readonly SaleService $service
    ) {}

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

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

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

    /**
     * 매출 등록
     */
    public function store(StoreSaleRequest $request)
    {
        $sale = $this->service->store($request->validated());

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

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

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

    /**
     * 매출 수정
     */
    public function update(int $id, UpdateSaleRequest $request)
    {
        $sale = $this->service->update($id, $request->validated());

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

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

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

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

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

    /**
     * 계정과목 일괄 변경
     */
    public function bulkUpdateAccountCode(BulkUpdateAccountCodeRequest $request)
    {
        $updatedCount = $this->service->bulkUpdateAccountCode(
            $request->getIds(),
            $request->getAccountCode()
        );

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

    /**
     * 매출 요약 (기간별 합계)
     */
    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 getStatement(int $id)
    {
        $statement = $this->service->getStatement($id);

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

    /**
     * 거래명세서 발행
     */
    public function issueStatement(int $id)
    {
        $result = $this->service->issueStatement($id);

        return ApiResponse::success($result, __('message.sale.statement_issued'));
    }

    /**
     * 거래명세서 이메일 발송
     */
    public function sendStatement(int $id, SendStatementRequest $request)
    {
        $result = $this->service->sendStatement($id, $request->validated());

        return ApiResponse::success($result, __('message.sale.statement_sent'));
    }

    /**
     * 거래명세서 일괄 발행
     */
    public function bulkIssueStatement(BulkIssueStatementRequest $request)
    {
        $result = $this->service->bulkIssueStatement($request->getIds());

        return ApiResponse::success($result, __('message.sale.bulk_statement_issued'));
    }
}
