<?php

namespace App\Http\Requests\Research;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use App\Models\RPublication;

/**
 * Form Request: StorePublicationRequest
 * 
 * Validates publication submission data including multi-step form fields.
 * 
 * @author SSHIA Development Team
 */
class StorePublicationRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth()->check();
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'title' => [
                'required',
                'string',
                'max:191',
                'min:10',
            ],
            'abstract' => [
                'required',
                'string',
                'min:100',
                'max:5000',
            ],
            'methodology' => [
                'nullable',
                'string',
                'max:10000',
            ],
            'findings' => [
                'nullable',
                'string',
                'max:10000',
            ],
            'references' => [
                'nullable',
                'string',
                'max:15000',
            ],
            'category' => [
                'required',
                Rule::in([
                    RPublication::CATEGORY_CLINICAL,
                    RPublication::CATEGORY_TECH,
                    RPublication::CATEGORY_SOCIAL,
                ]),
            ],
            'cover_image' => [
                'nullable',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:2048',
            ],
            'status' => [
                'sometimes',
                Rule::in(['draft', 'submitted']),
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'title.required' => 'A title is required for your publication.',
            'title.min' => 'The title must be at least 10 characters.',
            'title.max' => 'The title cannot exceed 191 characters.',
            'abstract.required' => 'An abstract is required.',
            'abstract.min' => 'The abstract must be at least 100 characters.',
            'abstract.max' => 'The abstract cannot exceed 5000 characters.',
            'category.required' => 'Please select a research category.',
            'category.in' => 'Please select a valid category.',
            'cover_image.image' => 'The cover must be an image file.',
            'cover_image.max' => 'The cover image cannot exceed 2MB.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        // Sanitize text fields
        $this->merge([
            'title' => strip_tags($this->title),
            'abstract' => strip_tags($this->abstract, '<p><br><strong><em><ul><ol><li>'),
        ]);
    }

    /**
     * Get validated data with sanitized values.
     */
    public function validated($key = null, $default = null): array
    {
        $validated = parent::validated($key, $default);

        // Add user_id
        $validated['user_id'] = auth()->id();

        return $validated;
    }
}
