<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PropertyRequest 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
    {
        $rules = [
            'title' => [
                'required',
                'string',
                'max:255',
            ],
            'short_description' => [
                'required',
                'string',
                'max:500',
            ],
            'full_description' => [
                'required',
                'string',
            ],
            'area' => [
                'required',
                'numeric',
                'min:1',
                'max:999999.99',
            ],
            'bedrooms' => [
                'required',
                'integer',
                'min:0',
                'max:50',
            ],
            'bathrooms' => [
                'required',
                'integer',
                'min:0',
                'max:50',
            ],
            'type' => [
                'required',
                'string',
                'in:villa,chalet,townhouse,apartment,penthouse,duplex',
            ],
            'display_on_mobile' => [
                'boolean',
            ],
            'display_on_homepage' => [
                'boolean',
            ],
            'is_active' => [
                'boolean',
            ],
            'sort_order' => [
                'nullable',
                'integer',
                'min:0',
            ],
        ];

        // Add image validation rules based on request method
        if ($this->isMethod('POST')) {
            $rules['card_image'] = [
                'required',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:2048',
            ];
            $rules['gallery.*'] = [
                'nullable',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:2048',
            ];
        } else {
            $rules['card_image'] = [
                'nullable',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:2048',
            ];
            $rules['gallery.*'] = [
                'nullable',
                'image',
                'mimes:jpeg,png,jpg,webp',
                'max:2048',
            ];
        }

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages(): array
    {
        return [
            'title.required' => 'The property title is required.',
            'title.max' => 'The property title cannot exceed 255 characters.',
            'short_description.required' => 'The short description is required.',
            'short_description.max' => 'The short description cannot exceed 500 characters.',
            'full_description.required' => 'The full description is required.',
            'area.required' => 'The area is required.',
            'area.numeric' => 'The area must be a number.',
            'area.min' => 'The area must be at least 1.',
            'area.max' => 'The area cannot exceed 999,999.99.',
            'bedrooms.required' => 'The number of bedrooms is required.',
            'bedrooms.integer' => 'The number of bedrooms must be a whole number.',
            'bedrooms.min' => 'The number of bedrooms must be at least 0.',
            'bedrooms.max' => 'The number of bedrooms cannot exceed 50.',
            'bathrooms.required' => 'The number of bathrooms is required.',
            'bathrooms.integer' => 'The number of bathrooms must be a whole number.',
            'bathrooms.min' => 'The number of bathrooms must be at least 0.',
            'bathrooms.max' => 'The number of bathrooms cannot exceed 50.',
            'type.required' => 'The property type is required.',
            'type.in' => 'Please select a valid property type.',
            'card_image.required' => 'The card image is required.',
            'card_image.image' => 'The card image must be an image file.',
            'card_image.mimes' => 'The card image must be a JPEG, PNG, JPG, or WebP file.',
            'card_image.max' => 'The card image must not exceed 2MB.',
            'gallery.*.image' => 'Gallery images must be image files.',
            'gallery.*.mimes' => 'Gallery images must be JPEG, PNG, JPG, or WebP files.',
            'gallery.*.max' => 'Gallery images must not exceed 2MB.',
            'sort_order.integer' => 'The sort order must be a whole number.',
            'sort_order.min' => 'The sort order must be at least 0.',
        ];
    }

    /**
     * Get custom attributes for validator errors.
     *
     * @return array
     */
    public function attributes(): array
    {
        return [
            'title' => 'property title',
            'short_description' => 'short description',
            'full_description' => 'full description',
            'area' => 'area',
            'bedrooms' => 'number of bedrooms',
            'bathrooms' => 'number of bathrooms',
            'type' => 'property type',
            'card_image' => 'card image',
            'gallery' => 'gallery images',
        ];
    }
} 