<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Bill\StoreBillRequest;
use App\Http\Requests\V1\Bill\UpdateBillRequest;
use App\Http\Requests\V1\Bill\UpdateBillStatusRequest;
use App\Services\BillService;
use Illuminate\Http\Request;

class BillController extends Controller
{
    public function __construct(
        private readonly BillService $service
    ) {}

    /**
     * 어음 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'bill_type',
            'status',
            'client_id',
            'is_electronic',
            'issue_start_date',
            'issue_end_date',
            'maturity_start_date',
            'maturity_end_date',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 어음 등록
     */
    public function store(StoreBillRequest $request)
    {
        $bill = $this->service->store($request->validated());

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

    /**
     * 어음 상세
     */
    public function show(int $id)
    {
        $bill = $this->service->show($id);

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

    /**
     * 어음 수정
     */
    public function update(int $id, UpdateBillRequest $request)
    {
        $bill = $this->service->update($id, $request->validated());

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

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

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

    /**
     * 어음 상태 변경
     */
    public function updateStatus(int $id, UpdateBillStatusRequest $request)
    {
        $bill = $this->service->updateStatus($id, $request->validated()['status']);

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

    /**
     * 어음 요약 (기간별 합계)
     */
    public function summary(Request $request)
    {
        $params = $request->only([
            'bill_type',
            'issue_start_date',
            'issue_end_date',
            'maturity_start_date',
            'maturity_end_date',
        ]);

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

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

    /**
     * 대시보드 상세 조회 (CEO 대시보드 당월 예상 지출내역 me3 모달용)
     */
    public function dashboardDetail()
    {
        $data = $this->service->dashboardDetail();

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