<?php

namespace App\Http\Requests;

use App\Services\ClinicSettings;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    public function rules(): array
    {
        $types = $this->user()
            ? app(ClinicSettings::class)->appointmentTypes($this->user()->organization_id)
            : ['new_consultation', 'follow_up', 'routine', 'emergency'];

        return [
            'patient_id' => ['required', 'integer'],
            'doctor_id' => ['required', 'integer'],
            'location_id' => ['required', 'integer'],
            'scheduled_start_at' => ['required', 'date'],
            'type' => ['required', 'string', 'max:80', Rule::in($types)],
            'reason' => ['nullable', 'string', 'max:3000'],
            'discount_amount' => ['sometimes', 'numeric', 'min:0'],
            'allow_backfill' => ['sometimes', 'boolean'],
            'backfill_reason' => ['nullable', 'required_if:allow_backfill,true', 'string', 'max:500'],
        ];
    }
}
