<?php

declare(strict_types=1);

namespace App\Http\Requests\Admin;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

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

    /** @return array<string, array<int, mixed>> */
    public function rules(): array
    {
        $service = $this->route('service');

        return [
            'plan_id' => [
                'sometimes',
                'nullable',
                'exists:plans,id',
                Rule::exists('plans', 'id')->where(function ($query) use ($service): void {
                    $query->where('service_type', $service->service_type)
                        ->where('status', 'active');
                }),
            ],
            'notes' => ['nullable', 'string', 'max:1000'],
        ];
    }

    /** @return array<string, string> */
    public function messages(): array
    {
        return [
            'plan_id.exists' => 'The selected plan does not exist or is not available for this service type.',
            'notes.max' => 'Notes cannot exceed 1000 characters.',
        ];
    }
}
