<?php

namespace App\Http\Requests\Advocacy;

use Illuminate\Foundation\Http\FormRequest;

/**
 * Form Request: StoreHealthStoryRequest
 *
 * Validates public health story submissions.
 * Allows both authenticated users and guests to submit stories.
 */
class StoreHealthStoryRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     * Public submission is allowed for everyone.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'author_name'    => ['required', 'string', 'max:191', 'min:2'],
            'author_email'   => ['required', 'email', 'max:191'],
            'author_phone'   => ['nullable', 'string', 'max:30'],
            'location'       => ['nullable', 'string', 'max:191'],
            'campaign_id'    => ['nullable', 'exists:r_campaigns,id'],
            'title'          => ['required', 'string', 'max:191', 'min:5'],
            'content'        => ['required', 'string', 'min:50'],
            'excerpt'        => ['nullable', 'string', 'max:500'],
            'video_url'      => ['nullable', 'url', 'max:500'],
            'featured_image' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:5120'],
            'gallery'        => ['nullable', 'array', 'max:5'],
            'gallery.*'      => ['image', 'mimes:jpeg,png,jpg,webp', 'max:2048'],
            'consent'        => ['required', 'accepted'],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'author_name.required'  => 'Please provide your name.',
            'author_name.min'       => 'Your name must be at least 2 characters.',
            'author_email.required' => 'Please provide your email address.',
            'author_email.email'    => 'Please provide a valid email address.',
            'title.required'        => 'A story title is required.',
            'title.min'             => 'The title must be at least 5 characters.',
            'content.required'      => 'Please write your story.',
            'content.min'           => 'Your story must be at least 50 characters.',
            'featured_image.max'    => 'The featured image cannot exceed 5MB.',
            'gallery.max'           => 'You can upload a maximum of 5 gallery images.',
            'gallery.*.max'         => 'Each gallery image cannot exceed 2MB.',
            'consent.required'      => 'You must consent to the story submission terms.',
            'consent.accepted'      => 'You must consent to the story submission terms.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'author_name'  => strip_tags($this->author_name ?? ''),
            'title'        => strip_tags($this->title ?? ''),
            'excerpt'      => $this->excerpt ? strip_tags($this->excerpt) : null,
            // Link submission to authenticated user if logged in
            'user_id'      => auth()->id(),
            // New public submissions go to pending review
            'status'       => 'pending_review',
        ]);
    }
}
