<?php

namespace App\Http\Requests\Blog;

use Illuminate\Foundation\Http\FormRequest;

class GetBlogsRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'search' => 'sometimes|string|max:255',
            'category_id' => 'sometimes|string|exists:blog_categories,id',
            'author_id' => 'sometimes|string|exists:users,id',
            'is_featured' => 'sometimes|boolean',
            'published_from' => 'sometimes|date',
            'published_to' => 'sometimes|date|after_or_equal:published_from',
            'created_from' => 'sometimes|date',
            'created_to' => 'sometimes|date|after_or_equal:created_from',
            'sort_by' => 'sometimes|string|in:title,status,is_featured,published_at,created_at,updated_at,view_count',
            'sort_order' => 'sometimes|string|in:asc,desc',
            'per_page' => 'sometimes|integer|min:1|max:100',
            'page' => 'sometimes|integer|min:1',
        ];
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array<string, string>
     */
    public function messages(): array
    {
        return [
            'search.string' => 'Search term must be a string.',
            'search.max' => 'Search term may not be greater than 255 characters.',
            'category_id.exists' => 'Selected category does not exist.',
            'author_id.exists' => 'Selected author does not exist.',
            'is_featured.boolean' => 'Featured status must be true or false.',
            'published_from.date' => 'Published from date must be a valid date.',
            'published_to.date' => 'Published to date must be a valid date.',
            'published_to.after_or_equal' => 'Published to date must be after or equal to published from date.',
            'created_from.date' => 'Created from date must be a valid date.',
            'created_to.date' => 'Created to date must be a valid date.',
            'created_to.after_or_equal' => 'Created to date must be after or equal to created from date.',
            'sort_by.in' => 'Sort by field is invalid.',
            'sort_order.in' => 'Sort order must be asc or desc.',
            'per_page.integer' => 'Per page must be an integer.',
            'per_page.min' => 'Per page must be at least 1.',
            'per_page.max' => 'Per page may not be greater than 100.',
            'page.integer' => 'Page must be an integer.',
            'page.min' => 'Page must be at least 1.',
        ];
    }
}
