<?php

namespace App\Http\Requests\Advocacy;

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

/**
 * Form Request: StoreCampaignRequest
 * 
 * Validates campaign creation and update data.
 */
class StoreCampaignRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        if (!auth()->check()) {
            return false;
        }

        // Allow update (PUT/PATCH) if user can edit campaigns
        if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
            return auth()->user()->can('edit campaigns');
        }

        // Allow create (POST) if user can create campaigns
        return auth()->user()->can('create campaigns');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        $rules = [
            'title' => ['required', 'string', 'max:191', 'min:5'],
            'description' => ['required', 'string', 'min:100', 'max:10000'],
            'objectives' => ['nullable', 'string', 'max:5000'],
            'banner_image' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:5120'],
            'thumbnail' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:2048'],
            'target_audience' => ['nullable', 'string', 'max:500'],
            'regions' => ['nullable', 'array'],
            'regions.*' => ['string', 'max:100'],
            'start_date' => ['required', 'date', 'after_or_equal:today'],
            'end_date' => ['nullable', 'date', 'after:start_date'],
            'status' => [
                'sometimes',
                Rule::in(['draft', 'active', 'paused', 'completed', 'archived']),
            ],
            'is_featured' => ['sometimes', 'boolean'],
            'meta_title' => ['nullable', 'string', 'max:191'],
            'meta_description' => ['nullable', 'string', 'max:500'],
            'meta_keywords' => ['nullable', 'array'],
            'meta_keywords.*' => ['string', 'max:50'],
        ];

        // On update, allow past start dates
        if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
            $rules['start_date'] = ['required', 'date'];
        }

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'title.required' => 'A campaign title is required.',
            'title.min' => 'The title must be at least 5 characters.',
            'description.required' => 'A campaign description is required.',
            'description.min' => 'The description must be at least 100 characters.',
            'start_date.required' => 'A start date is required.',
            'start_date.after_or_equal' => 'The start date must be today or later.',
            'end_date.after' => 'The end date must be after the start date.',
            'banner_image.max' => 'The banner image cannot exceed 5MB.',
        ];
    }

    /**
     * Prepare the data for validation.
     */
    protected function prepareForValidation(): void
    {
        $this->merge([
            'title' => strip_tags($this->title),
            'user_id' => auth()->id(),
        ]);
    }
}
