<?php

namespace App\Http\Requests\Training;

use Illuminate\Foundation\Http\FormRequest;

/**
 * StoreTrainingSessionRequest
 *
 * Validates training session creation and update requests.
 */
class StoreTrainingSessionRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth()->user()->can('create training sessions')
            || auth()->user()->can('edit training sessions');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $isUpdate = $this->route('session') !== null;

        return [
            'training_package_id' => ['required', 'exists:training_packages,id'],
            'session_name'        => ['required', 'string', 'max:191'],
            'session_date'        => array_filter([
                'required',
                'date',
                // Only enforce future date on create
                $isUpdate ? null : 'after_or_equal:today',
            ]),
            'start_time'          => ['required', 'date_format:H:i'],
            'end_time'            => ['required', 'date_format:H:i', 'after:start_time'],
            'session_type'        => ['required', 'in:in_person,online,hybrid'],
            'status'              => ['nullable', 'in:scheduled,ongoing,completed,cancelled,postponed'],
            'venue_name'          => ['nullable', 'string', 'max:191'],
            'venue_address'       => ['nullable', 'string', 'max:500'],
            'venue_city'          => ['nullable', 'string', 'max:100'],
            'max_participants'    => ['nullable', 'integer', 'min:1'],
            'description'         => ['nullable', 'string', 'max:2000'],
            'meeting_provider'    => ['nullable', 'string', 'max:50'],
            'meeting_link'        => ['nullable', 'url', 'max:500'],
            'meeting_id'          => ['nullable', 'string', 'max:191'],
            'meeting_password'    => ['nullable', 'string', 'max:191'],
            'trainers'            => ['nullable', 'array'],
            'trainers.*'          => ['exists:users,id'],
        ];
    }

    /**
     * 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.',
            'session_name.required'        => 'The session name is required.',
            'session_date.required'        => 'The session date is required.',
            'session_date.after_or_equal'  => 'Session date must be today or a future date.',
            'start_time.required'          => 'The start time is required.',
            'end_time.required'            => 'The end time is required.',
            'end_time.after'               => 'End time must be after start time.',
            'session_type.required'        => 'Please select a session type.',
            'meeting_link.url'             => 'The meeting link must be a valid URL.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        // Strip tags from text fields
        if ($this->has('session_name')) {
            $this->merge(['session_name' => strip_tags($this->session_name ?? '')]);
        }
        if ($this->has('description')) {
            $this->merge(['description' => strip_tags($this->description ?? '')]);
        }
        if ($this->has('venue_name')) {
            $this->merge(['venue_name' => strip_tags($this->venue_name ?? '')]);
        }
    }
}
