<?php

namespace App\Http\Requests;

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

class UpdatePropertyRequest 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 [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'address' => 'required|string|max:500',
            'latitude' => 'nullable|numeric|between:-90,90',
            'longitude' => 'nullable|numeric|between:-180,180',
            'property_type' => 'required|in:1,2,3,4,5',
            'bedrooms' => 'nullable|integer|min:0|max:20',
            'bathrooms' => 'nullable|integer|min:0|max:20',
            'size_sqm' => 'nullable|integer|min:0',
            'monthly_rent' => 'required|numeric|min:0',
            'deposit_amount' => 'required|numeric|min:0',
            'is_furnished' => 'boolean',
            'status' => 'required|in:1,2,3,4',
            'landlord_id' => 'required|exists:users,id',
            'images' => 'nullable|array|max:10',
            'images.*.image_url' => 'required_with:images|string|max:500',
            'images.*.is_primary' => 'boolean',
            'hostel' => 'nullable|array',
            'hostel.university_nearby' => 'required_with:hostel|string|min:2|max:255',
            'hostel.distance_to_university' => 'required_with:hostel|numeric|min:0|max:1000',
            'hostel.room_sharing' => 'required_with:hostel|integer|in:1,2,3',
            'hostel.gender_restriction' => 'required_with:hostel|integer|in:1,2,3',
            'hostel.meal_plan_available' => 'nullable|boolean',
            'hostel.study_room_available' => 'nullable|boolean',
            'hostel.curfew_time' => 'nullable|date_format:H:i',
        ];
    }
    
    /**
     * Prepare the data for validation
     */
    protected function prepareForValidation()
    {
        // Ensure images is always an array if present
        if ($this->has('images') && !is_array($this->images)) {
            $this->merge([
                'images' => []
            ]);
        }
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'title.required' => 'Property title is required.',
            'address.required' => 'Property address is required.',
            'property_type.required' => 'Property type is required.',
            'property_type.in' => 'Invalid property type selected.',
            'monthly_rent.required' => 'Monthly rent is required.',
            'monthly_rent.min' => 'Monthly rent must be at least 0.',
            'deposit_amount.required' => 'Deposit amount is required.',
            'deposit_amount.min' => 'Deposit amount must be at least 0.',
            'status.required' => 'Property status is required.',
            'status.in' => 'Invalid property status selected.',
            'landlord_id.required' => 'Landlord is required.',
            'landlord_id.exists' => 'Selected landlord does not exist.',
        ];
    }
}