<?php

declare(strict_types=1);

namespace App\Http\Requests;

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

class StoreConfigGroupRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    /** @return array<string, array<int, mixed>> */
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'mode' => ['required', Rule::in(['preset', 'build_your_own'])],
            'service_type' => ['nullable', 'required_if:mode,build_your_own', Rule::in(['vps', 'dedicated', 'hosting', 'mysql', 'game', 'backups'])],
            'is_active' => ['boolean'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'plan_ids' => ['nullable', 'array'],
            'plan_ids.*' => ['exists:plans,id'],
            'options' => ['required', 'array', 'min:1'],
            'options.*.name' => ['required', 'string', 'max:255'],
            'options.*.description' => ['nullable', 'string'],
            'options.*.type' => ['required', Rule::in(['dropdown', 'radio', 'quantity', 'checkbox', 'text', 'slider'])],
            'options.*.provisioning_key' => ['nullable', 'string', 'max:100'],
            'options.*.required' => ['boolean'],
            'options.*.is_active' => ['boolean'],
            'options.*.min_qty' => ['nullable', 'integer', 'min:0'],
            'options.*.max_qty' => ['nullable', 'integer', 'min:0'],
            'options.*.step' => ['nullable', 'integer', 'min:1'],
            'options.*.unit_label' => ['nullable', 'string', 'max:50'],
            'options.*.hourly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.monthly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.quarterly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.semi_annual_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.annual_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values' => ['nullable', 'array'],
            'options.*.values.*.label' => ['required', 'string', 'max:255'],
            'options.*.values.*.value' => ['nullable', 'string', 'max:255'],
            'options.*.values.*.hourly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values.*.monthly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values.*.quarterly_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values.*.semi_annual_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values.*.annual_price' => ['nullable', 'numeric', 'min:0'],
            'options.*.values.*.is_default' => ['boolean'],
        ];
    }

    /** @return array<string, string> */
    public function messages(): array
    {
        return [
            'name.required' => 'Config group name is required.',
            'mode.required' => 'Mode is required.',
            'mode.in' => 'Invalid mode selected.',
            'service_type.required_if' => 'Service type is required for Build Your Own mode.',
            'options.required' => 'At least one option is required.',
            'options.min' => 'At least one option is required.',
            'options.*.name.required' => 'Each option must have a name.',
            'options.*.type.required' => 'Each option must have a type.',
            'options.*.type.in' => 'Invalid option type selected.',
            'options.*.values.*.label.required' => 'Each value must have a label.',
        ];
    }
}
