<?php

namespace App\Http\Requests;

use App\Enums\BlockedPeriodEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class ProviderBlockedDayRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $rules = [
            'provider_id' => ['sometimes', 'exists:providers,id'],
            'date'        => ['sometimes', 'date', 'after_or_equal:today'],
            'period'      => ['sometimes', Rule::in([BlockedPeriodEnum::MORNING->value, BlockedPeriodEnum::AFTERNOON->value, BlockedPeriodEnum::ALL->value])],
            'reason'      => ['nullable', 'string'],
            'init_hour'   => ['nullable', 'date_format:H:i'],
            'end_hour'    => ['nullable', 'date_format:H:i'],
        ];

        if ($this->isMethod('POST')) {
            $rules['provider_id'] = ['required', 'exists:providers,id'];
            $rules['date']        = ['required', 'date', 'after_or_equal:today'];
            $rules['period']      = ['required', Rule::in([BlockedPeriodEnum::MORNING->value, BlockedPeriodEnum::AFTERNOON->value, BlockedPeriodEnum::ALL->value])];
            $rules['init_hour']   = ['required', 'date_format:H:i'];
            $rules['end_hour']    = ['required', 'date_format:H:i'];
        }

        return $rules;
    }
}
