<?php

namespace App\Http\Requests\Training;

use Illuminate\Foundation\Http\FormRequest;

/**
 * StoreTrainingRegistrationRequest
 *
 * Validates training registration creation requests from the dashboard.
 */
class StoreTrainingRegistrationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth()->user()->can('register for training');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'training_package_id' => ['required', 'exists:training_packages,id'],
            'notes'               => ['nullable', 'string', 'max:1000'],
            // Payment is handled on a dedicated payment page after registration,
            // so no payment fields are needed here.
        ];
    }

    /**
     * Get custom validation messages.
     */
    public function messages(): array
    {
        return [
            'training_package_id.required' => 'Please select a training package.',
            'training_package_id.exists'   => 'The selected training package does not exist.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        if ($this->has('notes')) {
            $this->merge(['notes' => strip_tags($this->notes ?? '')]);
        }
    }
}
