<?php
declare(strict_types=1);

namespace ROITheme\Admin\SocialShare\Infrastructure\Ui;

use ROITheme\Admin\Infrastructure\Ui\AdminDashboardRenderer;
use ROITheme\Admin\Shared\Infrastructure\Ui\ExclusionFormPartial;

/**
 * FormBuilder para Social Share
 *
 * Responsabilidad:
 * - Generar HTML del formulario de configuracion
 * - Usar Design System (Bootstrap 5)
 * - Cargar valores desde BD via AdminDashboardRenderer
 *
 * @package ROITheme\Admin\SocialShare\Infrastructure\Ui
 */
final class SocialShareFormBuilder
{
    public function __construct(
        private AdminDashboardRenderer $renderer
    ) {}

    public function buildForm(string $componentId): string
    {
        $html = '';

        $html .= $this->buildHeader($componentId);

        $html .= '<div class="row g-3">';

        // Columna izquierda
        $html .= '<div class="col-lg-6">';
        $html .= $this->buildVisibilityGroup($componentId);
        $html .= $this->buildContentGroup($componentId);
        $html .= $this->buildEffectsGroup($componentId);
        $html .= $this->buildTypographyGroup($componentId);
        $html .= $this->buildSpacingGroup($componentId);
        $html .= '</div>';

        // Columna derecha
        $html .= '<div class="col-lg-6">';
        $html .= $this->buildNetworksGroup($componentId);
        $html .= $this->buildColorsGroup($componentId);
        $html .= '</div>';

        $html .= '</div>';

        return $html;
    }

    private function buildHeader(string $componentId): string
    {
        $html = '<div class="rounded p-4 mb-4 shadow text-white" ';
        $html .= 'style="background: linear-gradient(135deg, #0E2337 0%, #1e3a5f 100%); border-left: 4px solid #FF8600;">';
        $html .= '    <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">';
        $html .= '        <div>';
        $html .= '            <h3 class="h4 mb-1 fw-bold">';
        $html .= '                <i class="bi bi-share me-2" style="color: #FF8600;"></i>';
        $html .= '                Configuracion de Compartir en Redes';
        $html .= '            </h3>';
        $html .= '            <p class="mb-0 small" style="opacity: 0.85;">';
        $html .= '                Botones para compartir contenido en redes sociales';
        $html .= '            </p>';
        $html .= '        </div>';
        $html .= '        <button type="button" class="btn btn-sm btn-outline-light btn-reset-defaults" data-component="social-share">';
        $html .= '            <i class="bi bi-arrow-counterclockwise me-1"></i>';
        $html .= '            Restaurar valores por defecto';
        $html .= '        </button>';
        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildVisibilityGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-toggle-on me-2" style="color: #FF8600;"></i>';
        $html .= '            Visibilidad';
        $html .= '        </h5>';

        // is_enabled
        $enabled = $this->renderer->getFieldValue($componentId, 'visibility', 'is_enabled', true);
        $html .= $this->buildSwitch('socialShareEnabled', 'Activar componente', 'bi-power', $enabled);

        // show_on_desktop
        $showOnDesktop = $this->renderer->getFieldValue($componentId, 'visibility', 'show_on_desktop', true);
        $html .= $this->buildSwitch('socialShareShowOnDesktop', 'Mostrar en escritorio', 'bi-display', $showOnDesktop);

        // show_on_mobile
        $showOnMobile = $this->renderer->getFieldValue($componentId, 'visibility', 'show_on_mobile', true);
        $html .= $this->buildSwitch('socialShareShowOnMobile', 'Mostrar en movil', 'bi-phone', $showOnMobile);

        // =============================================
        // Checkboxes de visibilidad por tipo de página
        // Grupo especial: _page_visibility
        // =============================================
        $html .= '        <hr class="my-3">';
        $html .= '        <p class="small fw-semibold mb-2">';
        $html .= '            <i class="bi bi-eye me-1" style="color: #FF8600;"></i>';
        $html .= '            Mostrar en tipos de pagina';
        $html .= '        </p>';

        $showOnHome = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_home', true);
        $showOnPosts = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_posts', true);
        $showOnPages = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_pages', true);
        $showOnArchives = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_archives', false);
        $showOnSearch = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_search', false);

        $html .= '        <div class="row g-2">';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('socialShareVisibilityHome', 'Home', 'bi-house', $showOnHome);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('socialShareVisibilityPosts', 'Posts', 'bi-file-earmark-text', $showOnPosts);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('socialShareVisibilityPages', 'Paginas', 'bi-file-earmark', $showOnPages);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('socialShareVisibilityArchives', 'Archivos', 'bi-archive', $showOnArchives);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('socialShareVisibilitySearch', 'Busqueda', 'bi-search', $showOnSearch);
        $html .= '            </div>';
        $html .= '        </div>';

        // =============================================
        // Reglas de exclusion avanzadas
        // Grupo especial: _exclusions (Plan 99.11)
        // =============================================
        $exclusionPartial = new ExclusionFormPartial($this->renderer);
        $html .= $exclusionPartial->render($componentId, 'socialShare');

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildContentGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-card-text me-2" style="color: #FF8600;"></i>';
        $html .= '            Contenido';
        $html .= '        </h5>';

        // show_label
        $showLabel = $this->renderer->getFieldValue($componentId, 'content', 'show_label', true);
        $html .= $this->buildSwitch('socialShareShowLabel', 'Mostrar etiqueta', 'bi-tag', $showLabel);

        // label_text
        $labelText = $this->renderer->getFieldValue($componentId, 'content', 'label_text', 'Compartir:');
        $html .= '        <div class="mb-0 mt-3">';
        $html .= '            <label for="socialShareLabelText" class="form-label small mb-1 fw-semibold">Texto etiqueta</label>';
        $html .= '            <input type="text" id="socialShareLabelText" class="form-control form-control-sm" ';
        $html .= '                   value="' . esc_attr($labelText) . '">';
        $html .= '        </div>';

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildNetworksGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-globe me-2" style="color: #FF8600;"></i>';
        $html .= '            Redes Sociales';
        $html .= '        </h5>';
        $html .= '        <p class="small text-muted mb-3">Configura las redes sociales y sus URLs</p>';

        // Facebook
        $showFacebook = $this->renderer->getFieldValue($componentId, 'networks', 'show_facebook', true);
        $facebookUrl = $this->renderer->getFieldValue($componentId, 'networks', 'facebook_url', '');
        $html .= $this->buildNetworkField('socialShareFacebook', 'socialShareFacebookUrl', 'Facebook', 'bi-facebook', $showFacebook, $facebookUrl, 'https://facebook.com/tu-pagina');

        // Instagram
        $showInstagram = $this->renderer->getFieldValue($componentId, 'networks', 'show_instagram', true);
        $instagramUrl = $this->renderer->getFieldValue($componentId, 'networks', 'instagram_url', '');
        $html .= $this->buildNetworkField('socialShareInstagram', 'socialShareInstagramUrl', 'Instagram', 'bi-instagram', $showInstagram, $instagramUrl, 'https://instagram.com/tu-perfil');

        // LinkedIn
        $showLinkedin = $this->renderer->getFieldValue($componentId, 'networks', 'show_linkedin', true);
        $linkedinUrl = $this->renderer->getFieldValue($componentId, 'networks', 'linkedin_url', '');
        $html .= $this->buildNetworkField('socialShareLinkedin', 'socialShareLinkedinUrl', 'LinkedIn', 'bi-linkedin', $showLinkedin, $linkedinUrl, 'https://linkedin.com/in/tu-perfil');

        // WhatsApp
        $showWhatsapp = $this->renderer->getFieldValue($componentId, 'networks', 'show_whatsapp', true);
        $whatsappNumber = $this->renderer->getFieldValue($componentId, 'networks', 'whatsapp_number', '');
        $html .= $this->buildNetworkField('socialShareWhatsapp', 'socialShareWhatsappNumber', 'WhatsApp', 'bi-whatsapp', $showWhatsapp, $whatsappNumber, '521234567890');

        // X (Twitter)
        $showTwitter = $this->renderer->getFieldValue($componentId, 'networks', 'show_twitter', true);
        $twitterUrl = $this->renderer->getFieldValue($componentId, 'networks', 'twitter_url', '');
        $html .= $this->buildNetworkField('socialShareTwitter', 'socialShareTwitterUrl', 'X (Twitter)', 'bi-twitter-x', $showTwitter, $twitterUrl, 'https://x.com/tu-perfil');

        // Email
        $showEmail = $this->renderer->getFieldValue($componentId, 'networks', 'show_email', true);
        $emailAddress = $this->renderer->getFieldValue($componentId, 'networks', 'email_address', '');
        $html .= $this->buildNetworkField('socialShareEmail', 'socialShareEmailAddress', 'Email', 'bi-envelope', $showEmail, $emailAddress, 'contacto@tudominio.com');

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildNetworkField(string $switchId, string $urlId, string $label, string $icon, mixed $checked, string $urlValue, string $placeholder): string
    {
        // Normalizar valor booleano desde BD
        $checked = $checked === true || $checked === '1' || $checked === 1;

        $html = '        <div class="mb-3 p-2 rounded" style="background-color: #f8f9fa;">';

        // Switch
        $html .= '            <div class="form-check form-switch">';
        $html .= sprintf(
            '                <input class="form-check-input" type="checkbox" id="%s" %s>',
            esc_attr($switchId),
            $checked ? 'checked' : ''
        );
        $html .= sprintf(
            '                <label class="form-check-label small fw-semibold" for="%s">',
            esc_attr($switchId)
        );
        $html .= sprintf('                    <i class="bi %s me-1" style="color: #FF8600;"></i>', esc_attr($icon));
        $html .= sprintf('                    %s', esc_html($label));
        $html .= '                </label>';
        $html .= '            </div>';

        // URL Input
        $html .= sprintf(
            '            <input type="text" id="%s" class="form-control form-control-sm mt-2" value="%s" placeholder="%s">',
            esc_attr($urlId),
            esc_attr($urlValue),
            esc_attr($placeholder)
        );

        $html .= '        </div>';

        return $html;
    }

    private function buildColorsGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-palette me-2" style="color: #FF8600;"></i>';
        $html .= '            Colores';
        $html .= '        </h5>';

        // Colores generales
        $html .= '        <p class="small fw-semibold mb-2">General</p>';
        $html .= '        <div class="row g-2 mb-3">';

        $labelColor = $this->renderer->getFieldValue($componentId, 'colors', 'label_color', '#6c757d');
        $html .= $this->buildColorPicker('socialShareLabelColor', 'Etiqueta', $labelColor);

        $borderTopColor = $this->renderer->getFieldValue($componentId, 'colors', 'border_top_color', '#dee2e6');
        $html .= $this->buildColorPicker('socialShareBorderTopColor', 'Borde superior', $borderTopColor);

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-3">';

        $buttonBackground = $this->renderer->getFieldValue($componentId, 'colors', 'button_background', '#ffffff');
        $html .= $this->buildColorPicker('socialShareButtonBg', 'Fondo botones', $buttonBackground);

        $html .= '        </div>';

        // Colores por red social
        $html .= '        <p class="small fw-semibold mb-2">Redes Sociales</p>';
        $html .= '        <div class="row g-2 mb-3">';

        $facebookColor = $this->renderer->getFieldValue($componentId, 'colors', 'facebook_color', '#0d6efd');
        $html .= $this->buildColorPicker('socialShareFacebookColor', 'Facebook', $facebookColor);

        $instagramColor = $this->renderer->getFieldValue($componentId, 'colors', 'instagram_color', '#dc3545');
        $html .= $this->buildColorPicker('socialShareInstagramColor', 'Instagram', $instagramColor);

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-3">';

        $linkedinColor = $this->renderer->getFieldValue($componentId, 'colors', 'linkedin_color', '#0dcaf0');
        $html .= $this->buildColorPicker('socialShareLinkedinColor', 'LinkedIn', $linkedinColor);

        $whatsappColor = $this->renderer->getFieldValue($componentId, 'colors', 'whatsapp_color', '#198754');
        $html .= $this->buildColorPicker('socialShareWhatsappColor', 'WhatsApp', $whatsappColor);

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-0">';

        $twitterColor = $this->renderer->getFieldValue($componentId, 'colors', 'twitter_color', '#212529');
        $html .= $this->buildColorPicker('socialShareTwitterColor', 'X (Twitter)', $twitterColor);

        $emailColor = $this->renderer->getFieldValue($componentId, 'colors', 'email_color', '#6c757d');
        $html .= $this->buildColorPicker('socialShareEmailColor', 'Email', $emailColor);

        $html .= '        </div>';

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildTypographyGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-fonts me-2" style="color: #FF8600;"></i>';
        $html .= '            Tipografia';
        $html .= '        </h5>';

        $html .= '        <div class="row g-2 mb-0">';

        // label_font_size
        $labelFontSize = $this->renderer->getFieldValue($componentId, 'typography', 'label_font_size', '1rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareLabelFontSize" class="form-label small mb-1 fw-semibold">Tamano etiqueta</label>';
        $html .= '                <input type="text" id="socialShareLabelFontSize" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($labelFontSize) . '">';
        $html .= '            </div>';

        // icon_font_size
        $iconFontSize = $this->renderer->getFieldValue($componentId, 'typography', 'icon_font_size', '1rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareIconFontSize" class="form-label small mb-1 fw-semibold">Tamano iconos</label>';
        $html .= '                <input type="text" id="socialShareIconFontSize" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($iconFontSize) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildSpacingGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-arrows-move me-2" style="color: #FF8600;"></i>';
        $html .= '            Espaciado';
        $html .= '        </h5>';

        $html .= '        <div class="row g-2 mb-3">';

        // container_margin_top
        $containerMarginTop = $this->renderer->getFieldValue($componentId, 'spacing', 'container_margin_top', '3rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareMarginTop" class="form-label small mb-1 fw-semibold">Margen superior</label>';
        $html .= '                <input type="text" id="socialShareMarginTop" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($containerMarginTop) . '">';
        $html .= '            </div>';

        // container_margin_bottom
        $containerMarginBottom = $this->renderer->getFieldValue($componentId, 'spacing', 'container_margin_bottom', '3rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareMarginBottom" class="form-label small mb-1 fw-semibold">Margen inferior</label>';
        $html .= '                <input type="text" id="socialShareMarginBottom" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($containerMarginBottom) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-3">';

        // container_padding_top
        $containerPaddingTop = $this->renderer->getFieldValue($componentId, 'spacing', 'container_padding_top', '1.5rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialSharePaddingTop" class="form-label small mb-1 fw-semibold">Padding superior</label>';
        $html .= '                <input type="text" id="socialSharePaddingTop" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($containerPaddingTop) . '">';
        $html .= '            </div>';

        // container_padding_bottom
        $containerPaddingBottom = $this->renderer->getFieldValue($componentId, 'spacing', 'container_padding_bottom', '1.5rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialSharePaddingBottom" class="form-label small mb-1 fw-semibold">Padding inferior</label>';
        $html .= '                <input type="text" id="socialSharePaddingBottom" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($containerPaddingBottom) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-3">';

        // label_margin_bottom
        $labelMarginBottom = $this->renderer->getFieldValue($componentId, 'spacing', 'label_margin_bottom', '1rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareLabelMarginBottom" class="form-label small mb-1 fw-semibold">Margen etiqueta</label>';
        $html .= '                <input type="text" id="socialShareLabelMarginBottom" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($labelMarginBottom) . '">';
        $html .= '            </div>';

        // buttons_gap
        $buttonsGap = $this->renderer->getFieldValue($componentId, 'spacing', 'buttons_gap', '0.5rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareButtonsGap" class="form-label small mb-1 fw-semibold">Espacio botones</label>';
        $html .= '                <input type="text" id="socialShareButtonsGap" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($buttonsGap) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-0">';

        // button_padding
        $buttonPadding = $this->renderer->getFieldValue($componentId, 'spacing', 'button_padding', '0.25rem 0.5rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareButtonPadding" class="form-label small mb-1 fw-semibold">Padding botones</label>';
        $html .= '                <input type="text" id="socialShareButtonPadding" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($buttonPadding) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildEffectsGroup(string $componentId): string
    {
        $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
        $html .= '    <div class="card-body">';
        $html .= '        <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
        $html .= '            <i class="bi bi-magic me-2" style="color: #FF8600;"></i>';
        $html .= '            Efectos Visuales';
        $html .= '        </h5>';

        $html .= '        <div class="row g-2 mb-3">';

        // border_top_width
        $borderTopWidth = $this->renderer->getFieldValue($componentId, 'visual_effects', 'border_top_width', '1px');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareBorderTopWidth" class="form-label small mb-1 fw-semibold">Grosor borde sup.</label>';
        $html .= '                <input type="text" id="socialShareBorderTopWidth" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($borderTopWidth) . '">';
        $html .= '            </div>';

        // button_border_width
        $buttonBorderWidth = $this->renderer->getFieldValue($componentId, 'visual_effects', 'button_border_width', '2px');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareButtonBorderWidth" class="form-label small mb-1 fw-semibold">Grosor borde btn</label>';
        $html .= '                <input type="text" id="socialShareButtonBorderWidth" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($buttonBorderWidth) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-3">';

        // button_border_radius
        $buttonBorderRadius = $this->renderer->getFieldValue($componentId, 'visual_effects', 'button_border_radius', '0.375rem');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareButtonBorderRadius" class="form-label small mb-1 fw-semibold">Radio botones</label>';
        $html .= '                <input type="text" id="socialShareButtonBorderRadius" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($buttonBorderRadius) . '">';
        $html .= '            </div>';

        // transition_duration
        $transitionDuration = $this->renderer->getFieldValue($componentId, 'visual_effects', 'transition_duration', '0.3s');
        $html .= '            <div class="col-6">';
        $html .= '                <label for="socialShareTransitionDuration" class="form-label small mb-1 fw-semibold">Duracion transicion</label>';
        $html .= '                <input type="text" id="socialShareTransitionDuration" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($transitionDuration) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '        <div class="row g-2 mb-0">';

        // hover_box_shadow
        $hoverBoxShadow = $this->renderer->getFieldValue($componentId, 'visual_effects', 'hover_box_shadow', '0 4px 12px rgba(0, 0, 0, 0.15)');
        $html .= '            <div class="col-12">';
        $html .= '                <label for="socialShareHoverBoxShadow" class="form-label small mb-1 fw-semibold">Sombra hover</label>';
        $html .= '                <input type="text" id="socialShareHoverBoxShadow" class="form-control form-control-sm" ';
        $html .= '                       value="' . esc_attr($hoverBoxShadow) . '">';
        $html .= '            </div>';

        $html .= '        </div>';

        $html .= '    </div>';
        $html .= '</div>';

        return $html;
    }

    private function buildSwitch(string $id, string $label, string $icon, mixed $checked): string
    {
        // Normalizar valor booleano desde BD
        $checked = $checked === true || $checked === '1' || $checked === 1;

        $html = '        <div class="mb-2">';
        $html .= '            <div class="form-check form-switch">';
        $html .= sprintf(
            '                <input class="form-check-input" type="checkbox" id="%s" %s>',
            esc_attr($id),
            $checked ? 'checked' : ''
        );
        $html .= sprintf(
            '                <label class="form-check-label small" for="%s">',
            esc_attr($id)
        );
        $html .= sprintf('                    <i class="bi %s me-1" style="color: #FF8600;"></i>', esc_attr($icon));
        $html .= sprintf('                    <strong>%s</strong>', esc_html($label));
        $html .= '                </label>';
        $html .= '            </div>';
        $html .= '        </div>';

        return $html;
    }

    private function buildColorPicker(string $id, string $label, string $value): string
    {
        $html = '            <div class="col-6">';
        $html .= sprintf(
            '                <label class="form-label small fw-semibold">%s</label>',
            esc_html($label)
        );
        $html .= '                <div class="input-group input-group-sm">';
        $html .= sprintf(
            '                    <input type="color" class="form-control form-control-color" id="%s" value="%s">',
            esc_attr($id),
            esc_attr($value)
        );
        $html .= sprintf(
            '                    <span class="input-group-text" id="%sValue">%s</span>',
            esc_attr($id),
            esc_html(strtoupper($value))
        );
        $html .= '                </div>';
        $html .= '            </div>';

        return $html;
    }

    private function buildPageVisibilityCheckbox(string $id, string $label, string $icon, mixed $checked): string
    {
        $checked = $checked === true || $checked === '1' || $checked === 1;

        $html = '                <div class="form-check form-check-checkbox mb-2">';
        $html .= sprintf(
            '                    <input class="form-check-input" type="checkbox" id="%s" %s>',
            esc_attr($id),
            $checked ? 'checked' : ''
        );
        $html .= sprintf(
            '                    <label class="form-check-label small" for="%s">',
            esc_attr($id)
        );
        $html .= sprintf('                        <i class="bi %s me-1" style="color: #FF8600;"></i>', esc_attr($icon));
        $html .= sprintf('                        %s', esc_html($label));
        $html .= '                    </label>';
        $html .= '                </div>';

        return $html;
    }
}
