<?php

declare(strict_types=1);

namespace Koneko\VuexyAdmin\Application\UI\Livewire\Settings\EnvironmentVars;

use Illuminate\Validation\Rule;
use Koneko\VuexyAdmin\Models\Setting;
use Koneko\VuexyAdmin\Support\Livewire\Components\Form\AbstractFormOffCanvasComponent;

/**
 * Class EnvironmentVarsOffCanvasForm
 *
 * Componente Livewire para gestionar parámetros globales del sistema.
 * Permite almacenar configuraciones personalizadas y valores múltiples tipos.
 *
 * @package Koneko\VuexyAdmin\Application\UI\Livewire\Settings
 */
class EnvironmentVarsOffCanvasForm extends AbstractFormOffCanvasComponent
{
    public $id, $key, $module, $user_id,
        $value_string, $value_integer, $value_boolean,
        $value_float, $value_text;

    public $confirmDeletion;

    protected $casts = [
        'value_boolean' => 'boolean',
        'value_integer' => 'integer',
        'value_float'   => 'float',
    ];

    protected $listeners = [
        'editGlobalSetting' => 'loadFormModel',
        'confirmDeletionGlobalSetting' => 'loadFormModelForDeletion',
    ];

    protected function model(): string
    {
        return Setting::class;
    }

    protected function fields(): array
    {
        return [
            'key', 'module', 'user_id',
            'value_string', 'value_integer', 'value_boolean',
            'value_float', 'value_text'
        ];
    }

    protected function defaults(): array
    {
        return [
            'module' => 'general',
        ];
    }

    protected function focusOnOpen(): string
    {
        return 'key';
    }

    protected function dynamicRules(string $mode): array
    {
        if ($mode === 'delete') {
            return ['confirmDeletion' => 'accepted'];
        }

        $uniqueRule = Rule::unique('settings', 'key')
            ->where(fn ($q) => $q
                ->where('user_id', $this->user_id)
                ->where('module', $this->module)
            );

        if ($mode === 'edit') {
            $uniqueRule = $uniqueRule->ignore($this->id);
        }

        return [
            'key'           => ['required', 'string', $uniqueRule],
            'module'        => ['nullable', 'string', 'max:96'],
            'user_id'       => ['nullable', 'integer', 'exists:users,id'],
            'value_string'  => ['nullable', 'string', 'max:255'],
            'value_integer' => ['nullable', 'integer'],
            'value_boolean' => ['nullable', 'boolean'],
            'value_float'   => ['nullable', 'numeric'],
            'value_text'    => ['nullable', 'string'],
        ];
    }

    protected function attributes(): array
    {
        return [
            'key' => 'clave de configuración',
            'module' => 'categoría',
        ];
    }

    protected function messages(): array
    {
        return [
            'key.required' => 'La clave del parámetro es obligatoria.',
            'key.unique'   => 'Ya existe una configuración con esta clave en esa categoría.',
        ];
    }

    protected function viewPath(): string
    {
        return 'vuexy-admin::livewire.settings.environment-vars.offcanvas-form';
    }
}
