<?php

namespace App\Http\Requests;

use App\Support\ExpensePaymentMethod;
use App\Support\ExpenseReport;
use App\Support\ExpenseStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ExpenseReportRequest extends FormRequest
{
    public function authorize(): bool
    {
        return (bool) $this->user()?->can('expense-reports.view');
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'date_from' => ['nullable', 'date'],
            'date_to' => ['nullable', 'date', 'after_or_equal:date_from'],
            'expense_category_id' => ['nullable', 'integer'],
            'payment_method' => ['nullable', Rule::in(ExpensePaymentMethod::values())],
            'status' => ['nullable', Rule::in(ExpenseStatus::values())],
        ];
    }

    /**
     * @return array<string, string|null>
     */
    public function filters(): array
    {
        $quickRange = $this->string('quick_range')->toString() ?: null;
        $now = now();

        if ($quickRange) {
            return match ($quickRange) {
                'today' => [
                    'date_from' => $now->toDateString(),
                    'date_to' => $now->toDateString(),
                    'expense_category_id' => $this->input('expense_category_id') ?: null,
                    'payment_method' => $this->input('payment_method') ?: null,
                    'status' => $this->input('status') ?: null,
                ],
                'this_week' => [
                    'date_from' => $now->copy()->startOfWeek()->toDateString(),
                    'date_to' => $now->copy()->endOfWeek()->toDateString(),
                    'expense_category_id' => $this->input('expense_category_id') ?: null,
                    'payment_method' => $this->input('payment_method') ?: null,
                    'status' => $this->input('status') ?: null,
                ],
                'this_year' => [
                    'date_from' => $now->copy()->startOfYear()->toDateString(),
                    'date_to' => $now->copy()->endOfYear()->toDateString(),
                    'expense_category_id' => $this->input('expense_category_id') ?: null,
                    'payment_method' => $this->input('payment_method') ?: null,
                    'status' => $this->input('status') ?: null,
                ],
                default => [
                    'date_from' => $now->copy()->startOfMonth()->toDateString(),
                    'date_to' => $now->copy()->endOfMonth()->toDateString(),
                    'expense_category_id' => $this->input('expense_category_id') ?: null,
                    'payment_method' => $this->input('payment_method') ?: null,
                    'status' => $this->input('status') ?: null,
                ],
            };
        }

        $defaults = ExpenseReport::defaultFilters();

        return [
            'date_from' => $this->input('date_from', $defaults['date_from']),
            'date_to' => $this->input('date_to', $defaults['date_to']),
            'expense_category_id' => $this->input('expense_category_id') ?: null,
            'payment_method' => $this->input('payment_method') ?: null,
            'status' => $this->input('status') ?: null,
        ];
    }
}
