<?php

namespace App\Http\Requests\Training;

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

class StoreTrainingPackageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return auth()->user()->can('create training packages');
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        return [
            'name' => [
                'required',
                'string',
                'max:191',
                Rule::unique('training_packages', 'name')->whereNull('deleted_at'),
            ],
            'description' => [
                'nullable',
                'string',
                'max:5000',
            ],
            'learning_objectives' => [
                'nullable',
                'string',
                'max:5000',
            ],
            'prerequisites' => [
                'nullable',
                'string',
                'max:2000',
            ],
            'duration_hours' => [
                'required',
                'integer',
                'min:0',
                'max:2000',
            ],
            'duration_days' => [
                'required',
                'integer',
                'min:0',
                'max:365',
            ],
            'capacity' => [
                'required',
                'integer',
                'min:1',
                'max:10000',
            ],
            'fee_amount' => [
                'required',
                'numeric',
                'min:0',
                'max:99999999.99',
            ],
            'currency' => [
                'required',
                'string',
                'size:3',
                'in:UGX,USD,EUR,KES,RWF',
            ],
            'is_free' => [
                'boolean',
            ],
            'certificate_format' => [
                'required',
                'string',
                'in:portrait,landscape',
            ],
            'generates_certificate' => [
                'boolean',
            ],
            'status' => [
                'required',
                'string',
                'in:draft,active,inactive,archived',
            ],
            'is_public' => [
                'boolean',
            ],
            'requires_approval' => [
                'boolean',
            ],
            'category_id' => [
                'nullable',
                'integer',
                'exists:training_categories,id',
            ],
            'tags' => [
                'nullable',
                'string',
                'max:500',
            ],
            'featured_image' => [
                'nullable',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:5120',
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'name.required' => 'The training package name is required.',
            'name.unique' => 'A training package with this name already exists.',
            'duration_hours.required' => 'The duration in hours is required.',
            'capacity.required' => 'The capacity is required.',
            'capacity.min' => 'Capacity must be at least 1.',
            'fee_amount.required' => 'The fee amount is required.',
            'featured_image.image' => 'The featured image must be an image file.',
            'featured_image.max' => 'The featured image may not be greater than 5MB.',
        ];
    }

    /**
     * Prepare the data for validation.
     *
     * @return void
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'name' => strip_tags($this->name ?? ''),
            'description' => strip_tags($this->description ?? ''),
            'learning_objectives' => strip_tags($this->learning_objectives ?? ''),
            'prerequisites' => strip_tags($this->prerequisites ?? ''),
        ]);
    }
}
