<?php

namespace App\Http\Requests\Admin;

use App\Services\LedgerService;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class DayLedgerFilterRequest extends FormRequest
{
    protected function prepareForValidation(): void
    {
        $this->merge([
            'date' => $this->input('date', now()->toDateString()),
            'order_type' => $this->input('order_type', LedgerService::ORDER_TYPE_ALL),
        ]);
    }

    public function authorize(): bool
    {
        $user = Auth::user();
        if (!$user) {
            return false;
        }

        $userType = $user->user_type ?? null;
        $isAdminOrStaff = in_array($userType, ['admin', 'staff'], true);
        return $isAdminOrStaff;
    }

    public function rules(): array
    {
        return [
            'date' => ['required', 'date'],
            'payment_mode' => ['nullable', Rule::in(LedgerService::PAYMENT_MODES)],
            'order_type' => ['nullable', Rule::in([
                LedgerService::ORDER_TYPE_ALL,
                LedgerService::ORDER_TYPE_POS,
                LedgerService::ORDER_TYPE_ONLINE,
            ])],
        ];
    }
}
