<?php

namespace App\Http\Controllers\Api\V1;

use App\Exports\DailyReportExport;
use App\Exports\ExpenseEstimateExport;
use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Report\DailyReportRequest;
use App\Http\Requests\V1\Report\ExpenseEstimateRequest;
use App\Services\ReportService;
use Maatwebsite\Excel\Facades\Excel;

class ReportController extends Controller
{
    public function __construct(
        private readonly ReportService $service
    ) {}

    /**
     * 일일 일보 조회
     */
    public function daily(DailyReportRequest $request)
    {
        $report = $this->service->dailyReport($request->validated());

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

    /**
     * 일일 일보 엑셀 다운로드
     */
    public function dailyExport(DailyReportRequest $request)
    {
        $report = $this->service->dailyReport($request->validated());
        $filename = 'daily_report_'.$report['date'].'.xlsx';

        return Excel::download(new DailyReportExport($report), $filename);
    }

    /**
     * 지출 예상 내역서 조회
     */
    public function expenseEstimate(ExpenseEstimateRequest $request)
    {
        $report = $this->service->expenseEstimate($request->validated());

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

    /**
     * 지출 예상 내역서 엑셀 다운로드
     */
    public function expenseEstimateExport(ExpenseEstimateRequest $request)
    {
        $report = $this->service->expenseEstimate($request->validated());
        $filename = 'expense_estimate_'.$report['year_month'].'.xlsx';

        return Excel::download(new ExpenseEstimateExport($report), $filename);
    }
}
