<?php

namespace App\Http\Requests;

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

class AddonRequest 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'],

            'sort_order' => ['required', 'integer', 'min:0'],

            'price' => ['required', 'numeric', 'min:0'],
            'unit' => ['required', 'integer', 'min:1'],
            'currency' => ['required', 'string', 'max:10'],

            'min_guests' => ['nullable', 'integer'],
            'max_guests' => ['nullable', 'integer'],
            'is_allow_select_guests' => ['boolean'],

            'min_quantity' => ['nullable', 'integer'],
            'max_quantity' => ['nullable', 'integer'],
            'is_allow_select_quantity' => ['boolean'],


            'calculation_type' => [
                'required',
                Rule::in([
                    'per_booking',
                    'per_room',
                    'per_room_per_night',
                    'per_night',
                    'per_person',
                    'per_person_per_night',
                    'per_quantity',
                ])
            ],

            'scope' => ['required',  Rule::in(['booking', 'all_rooms', 'room_category', 'room'])],
            'apply_on' => ['required',  Rule::in(['check_in', 'check_out', 'any_day', 'entire_stay', 'dont_apply'])],

            'is_allow_select_days' => ['boolean'],
            'is_variant' => ['boolean'],
            'default_availability' => ['boolean'],
            'is_published' => ['boolean'],

            'is_icon' => ['nullable', 'boolean'],
            'icon_value' => ['nullable', 'string'],

            'tag' => ['nullable', 'string', 'max:255'],

            // Variants validation
            'variants' => 'required_if:is_variant,true|array',

            'variants.*.id' => 'nullable',
            'variants.*.title' => 'required_if:is_variant,true|string|max:255',
            'variants.*.price' => 'required_if:is_variant,true|numeric',
            'variants.*.currency' => 'required_if:is_variant,true|string',
            'variants.*.unit' => 'nullable|integer',
            'variants.*.min_guests' => 'nullable|integer',
            'variants.*.max_guests' => 'nullable|integer',
            'variants.*.description' => 'nullable|string',
            'variants.*.is_icon' => 'nullable|string',
            'variants.*.icon_value' => 'nullable|string',
            'variants.*.default_availability' => 'nullable|boolean',
            'variants.*.is_published' => 'nullable|boolean',

            'images' => ['nullable', 'array'],
            'images.*.id' => ['required', 'integer', 'exists:images,id'],
            'images.*.role' => ['nullable', 'string'],
            'images.*.sort_order' => ['nullable', 'integer'],
        ];
    }
}
