<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\ExpectedExpense\StoreExpectedExpenseRequest;
use App\Http\Requests\V1\ExpectedExpense\UpdateExpectedExpenseRequest;
use App\Http\Requests\V1\ExpectedExpense\UpdateExpectedPaymentDateRequest;
use App\Services\ExpectedExpenseService;
use Illuminate\Http\Request;

class ExpectedExpenseController extends Controller
{
    public function __construct(
        private readonly ExpectedExpenseService $service
    ) {}

    /**
     * 미지급비용 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'search',
            'start_date',
            'end_date',
            'client_id',
            'transaction_type',
            'payment_status',
            'approval_status',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

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

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

    /**
     * 미지급비용 등록
     */
    public function store(StoreExpectedExpenseRequest $request)
    {
        $expense = $this->service->store($request->validated());

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

    /**
     * 미지급비용 상세
     */
    public function show(int $id)
    {
        $expense = $this->service->show($id);

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

    /**
     * 미지급비용 수정
     */
    public function update(int $id, UpdateExpectedExpenseRequest $request)
    {
        $expense = $this->service->update($id, $request->validated());

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

    /**
     * 미지급비용 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

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

    /**
     * 미지급비용 일괄 삭제
     */
    public function destroyMany(Request $request)
    {
        $ids = $request->input('ids', []);

        if (empty($ids)) {
            return ApiResponse::error(__('error.no_ids_provided'), 400);
        }

        $count = $this->service->destroyMany($ids);

        return ApiResponse::success(['deleted_count' => $count], __('message.deleted'));
    }

    /**
     * 예상 지급일 일괄 변경
     */
    public function updateExpectedPaymentDate(UpdateExpectedPaymentDateRequest $request)
    {
        $count = $this->service->updateExpectedPaymentDate(
            $request->input('ids'),
            $request->input('expected_payment_date')
        );

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

    /**
     * 미지급비용 요약 (기간별 합계)
     */
    public function summary(Request $request)
    {
        $params = $request->only([
            'start_date',
            'end_date',
            'payment_status',
        ]);

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

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

    /**
     * 대시보드 상세 조회 (CEO 대시보드 당월 예상 지출내역 모달용)
     *
     * @param  Request  $request  transaction_type (purchase, card, bill, null=전체), start_date, end_date, search
     */
    public function dashboardDetail(Request $request)
    {
        $transactionType = $request->query('transaction_type');
        $startDate = $request->query('start_date');
        $endDate = $request->query('end_date');
        $search = $request->query('search');

        $data = $this->service->dashboardDetail($transactionType, $startDate, $endDate, $search);

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