<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UploadGalleryImageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return auth()->user()->can('upload gallery images');
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'images'        => 'required|array|min:1|max:20',
            'images.*'      => 'required|image|mimes:jpeg,png,gif,webp|max:5120',
            'titles'        => 'required|array|min:1',
            'titles.*'      => 'required|string|max:191',
            'descriptions'  => 'nullable|array',
            'descriptions.*' => 'nullable|string|max:1000',
        ];
    }

    /**
     * Get custom error messages.
     */
    public function messages(): array
    {
        return [
            'images.required'          => 'Please select at least one image to upload.',
            'images.max'               => 'You can upload a maximum of 20 images at once.',
            'images.*.image'           => 'Uploaded files must be valid images.',
            'images.*.mimes'           => 'Images must be JPEG, PNG, GIF, or WebP format.',
            'images.*.max'             => 'Each image must not exceed 5MB.',
            'titles.required'          => 'Each image must have a title.',
        ];
    }
}
