<?php

namespace App\Http\Requests\Realty;

use App\Enums\PropertyType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Enum;

class PartyPropertyRequest 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 [
            //Property
            'title' => 'sometimes',
            'code' => 'sometimes',
            'location' => 'sometimes',
            'latitud' => 'sometimes',
            'longitud' => 'sometimes',
            'surface' => 'sometimes|numeric',
            'description' => 'sometimes',
            'initial_fee' => 'sometimes|numeric',
            'delivery_date' => 'sometimes',
            'status' => 'sometimes',
            'precio_por_m2' => 'sometimes|numeric',
            'final_price' => 'sometimes|numeric',
            'base_price' => 'sometimes|numeric',
            'property_type' => ['sometimes', new Enum(PropertyType::class)],
            'property_type_id' => 'sometimes|numeric',
            'project_id' => 'sometimes|numeric',

            //conexiones
            'benefits' => 'sometimes|array',
            'features' => [
                'required_if:property_type,' . PropertyType::DEPARTAMENT->name . '|' . PropertyType::HOUSE->name,
            ],
            'land_use' => [
                'required_if:property_type,' . PropertyType::LAND->name,
            ],
            'builded_surface' => [
                'required_if:property_type,' . PropertyType::HOUSE->name,
            ],
            'number_bathrooms' => [
                'required_if:property_type,' . PropertyType::DEPARTAMENT->name . '|' . PropertyType::HOUSE->name,
            ],
            'number_rooms' => [
                'required_if:property_type,' .  PropertyType::OFFICE->name,
            ],
            'number_bedrooms' => [
                'required_if:property_type,' . PropertyType::DEPARTAMENT->name . '|' . PropertyType::HOUSE->name,
            ],
            'number_floors' => [
                'required_if:property_type,' .  PropertyType::HOUSE->name . '|' . PropertyType::OFFICE->name,
            ],
            'floors' => [
                'required_if:property_type,' . PropertyType::DEPARTAMENT->name,
            ],

            //financing
            'financing' => [
                'required',
                'array',
            ],
            'financing.*.price' => 'required|numeric',
            'financing.*.percentage_initial_fee' => 'required|numeric',
            'financing.*.interest' => 'nullable|numeric',
            'financing.*.years_term' => 'nullable|numeric',
            'financing.*.payment_method' => 'required|numeric',

            // payment_plans
            'financing.*.payment_plans' => [
                'nullable',
                'array',
            ],
            'financing.*.payment_plans.*.date' => 'sometimes|date',
            'financing.*.payment_plans.*.amount' => 'sometimes|numeric|min:0',
        ];
    }
}
