<?php
declare(strict_types=1);

namespace ROITheme\Admin\Footer\Infrastructure\Ui;

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

/**
 * FormBuilder para Footer
 *
 * RESPONSABILIDAD: Generar formulario de configuracion del Footer
 *
 * @package ROITheme\Admin\Footer\Infrastructure\Ui
 */
final class FooterFormBuilder
{
    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->buildWidget1Group($componentId);
        $html .= $this->buildWidget2Group($componentId);
        $html .= $this->buildWidget3Group($componentId);
        $html .= $this->buildNewsletterGroup($componentId);
        $html .= '</div>';

        // Columna derecha
        $html .= '<div class="col-lg-6">';
        $html .= $this->buildFooterBottomGroup($componentId);
        $html .= $this->buildColorsGroup($componentId);
        $html .= $this->buildSpacingGroup($componentId);
        $html .= $this->buildEffectsGroup($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-layout-text-window-reverse me-2" style="color: #FF8600;"></i>';
        $html .= '                Configuracion de Footer';
        $html .= '            </h3>';
        $html .= '            <p class="mb-0 small" style="opacity: 0.85;">';
        $html .= '                Footer con menus de navegacion y newsletter';
        $html .= '            </p>';
        $html .= '        </div>';
        $html .= '        <button type="button" class="btn btn-sm btn-outline-light btn-reset-defaults" data-component="footer">';
        $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>';

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

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

        $showOnMobile = $this->renderer->getFieldValue($componentId, 'visibility', 'show_on_mobile', true);
        $html .= $this->buildSwitch('footerShowOnMobile', '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', true);
        $showOnSearch = $this->renderer->getFieldValue($componentId, '_page_visibility', 'show_on_search', true);

        $html .= '        <div class="row g-2">';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('footerVisibilityHome', 'Home', 'bi-house', $showOnHome);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('footerVisibilityPosts', 'Posts', 'bi-file-earmark-text', $showOnPosts);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('footerVisibilityPages', 'Paginas', 'bi-file-earmark', $showOnPages);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('footerVisibilityArchives', 'Archivos', 'bi-archive', $showOnArchives);
        $html .= '            </div>';
        $html .= '            <div class="col-md-4">';
        $html .= $this->buildPageVisibilityCheckbox('footerVisibilitySearch', '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, 'footer');

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

        return $html;
    }

    private function buildWidget1Group(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-list-ul me-2" style="color: #FF8600;"></i>';
        $html .= '            Widget 1 (Menu)';
        $html .= '        </h5>';

        $visible = $this->renderer->getFieldValue($componentId, 'widget_1', 'widget_1_visible', true);
        $html .= $this->buildSwitch('footerWidget1Visible', 'Mostrar Widget 1', 'bi-eye', $visible);

        $title = $this->renderer->getFieldValue($componentId, 'widget_1', 'widget_1_title', 'Recursos');
        $html .= $this->buildTextInput('footerWidget1Title', 'Titulo', 'bi-type', $title);

        $html .= '        <div class="alert alert-info small mb-0 mt-2">';
        $html .= '            <i class="bi bi-info-circle me-1"></i>';
        $html .= '            El contenido se gestiona desde <strong>Apariencia &gt; Menus &gt; Footer Menu 1</strong>';
        $html .= '        </div>';

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

        return $html;
    }

    private function buildWidget2Group(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-list-ul me-2" style="color: #FF8600;"></i>';
        $html .= '            Widget 2 (Menu)';
        $html .= '        </h5>';

        $visible = $this->renderer->getFieldValue($componentId, 'widget_2', 'widget_2_visible', true);
        $html .= $this->buildSwitch('footerWidget2Visible', 'Mostrar Widget 2', 'bi-eye', $visible);

        $title = $this->renderer->getFieldValue($componentId, 'widget_2', 'widget_2_title', 'Soporte');
        $html .= $this->buildTextInput('footerWidget2Title', 'Titulo', 'bi-type', $title);

        $html .= '        <div class="alert alert-info small mb-0 mt-2">';
        $html .= '            <i class="bi bi-info-circle me-1"></i>';
        $html .= '            El contenido se gestiona desde <strong>Apariencia &gt; Menus &gt; Footer Menu 2</strong>';
        $html .= '        </div>';

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

        return $html;
    }

    private function buildWidget3Group(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-list-ul me-2" style="color: #FF8600;"></i>';
        $html .= '            Widget 3 (Menu)';
        $html .= '        </h5>';

        $visible = $this->renderer->getFieldValue($componentId, 'widget_3', 'widget_3_visible', true);
        $html .= $this->buildSwitch('footerWidget3Visible', 'Mostrar Widget 3', 'bi-eye', $visible);

        $title = $this->renderer->getFieldValue($componentId, 'widget_3', 'widget_3_title', 'Empresa');
        $html .= $this->buildTextInput('footerWidget3Title', 'Titulo', 'bi-type', $title);

        $html .= '        <div class="alert alert-info small mb-0 mt-2">';
        $html .= '            <i class="bi bi-info-circle me-1"></i>';
        $html .= '            El contenido se gestiona desde <strong>Apariencia &gt; Menus &gt; Footer Menu 3</strong>';
        $html .= '        </div>';

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

        return $html;
    }

    private function buildNewsletterGroup(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-envelope-paper me-2" style="color: #FF8600;"></i>';
        $html .= '            Newsletter';
        $html .= '        </h5>';

        $visible = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_visible', true);
        $html .= $this->buildSwitch('footerNewsletterVisible', 'Mostrar Newsletter', 'bi-eye', $visible);

        $title = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_title', 'Suscribete al Newsletter');
        $html .= $this->buildTextInput('footerNewsletterTitle', 'Titulo', 'bi-type', $title);

        $description = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_description', 'Recibe las ultimas actualizaciones.');
        $html .= $this->buildTextarea('footerNewsletterDescription', 'Descripcion', 'bi-text-paragraph', $description);

        $placeholder = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_email_placeholder', 'Email');
        $html .= $this->buildTextInput('footerNewsletterPlaceholder', 'Placeholder email', 'bi-input-cursor', $placeholder);

        $buttonText = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_button_text', 'Suscribirse');
        $html .= $this->buildTextInput('footerNewsletterButtonText', 'Texto boton', 'bi-cursor', $buttonText);

        $webhookUrl = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_webhook_url', '');
        $html .= $this->buildTextarea('footerNewsletterWebhookUrl', 'URL del Webhook', 'bi-link-45deg', $webhookUrl);

        $successMsg = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_success_message', 'Gracias por suscribirte!');
        $html .= $this->buildTextInput('footerNewsletterSuccessMessage', 'Mensaje exito', 'bi-check-circle', $successMsg);

        $errorMsg = $this->renderer->getFieldValue($componentId, 'newsletter', 'newsletter_error_message', 'Error al suscribirse.');
        $html .= $this->buildTextInput('footerNewsletterErrorMessage', 'Mensaje error', 'bi-x-circle', $errorMsg);

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

        return $html;
    }

    private function buildFooterBottomGroup(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-c-circle me-2" style="color: #FF8600;"></i>';
        $html .= '            Pie de Footer';
        $html .= '        </h5>';

        $copyright = $this->renderer->getFieldValue($componentId, 'footer_bottom', 'copyright_text', date('Y') . ' Todos los derechos reservados.');
        $html .= $this->buildTextInput('footerCopyrightText', 'Texto copyright', 'bi-c-circle', $copyright);

        $html .= '        <div class="alert alert-secondary small mb-0 mt-2">';
        $html .= '            <i class="bi bi-info-circle me-1"></i>';
        $html .= '            El simbolo &copy; se agrega automaticamente';
        $html .= '        </div>';

        $html .= '    </div>';
        $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>';

        $bgColor = $this->renderer->getFieldValue($componentId, 'colors', 'bg_color', '#212529');
        $html .= $this->buildColorInput('footerBgColor', 'Fondo footer', $bgColor);

        $textColor = $this->renderer->getFieldValue($componentId, 'colors', 'text_color', '#ffffff');
        $html .= $this->buildColorInput('footerTextColor', 'Color texto', $textColor);

        $titleColor = $this->renderer->getFieldValue($componentId, 'colors', 'title_color', '#ffffff');
        $html .= $this->buildColorInput('footerTitleColor', 'Color titulos', $titleColor);

        $linkColor = $this->renderer->getFieldValue($componentId, 'colors', 'link_color', '#ffffff');
        $html .= $this->buildColorInput('footerLinkColor', 'Color links', $linkColor);

        $linkHoverColor = $this->renderer->getFieldValue($componentId, 'colors', 'link_hover_color', '#FF8600');
        $html .= $this->buildColorInput('footerLinkHoverColor', 'Color links hover', $linkHoverColor);

        $buttonBgColor = $this->renderer->getFieldValue($componentId, 'colors', 'button_bg_color', '#0d6efd');
        $html .= $this->buildColorInput('footerButtonBgColor', 'Fondo boton', $buttonBgColor);

        $buttonTextColor = $this->renderer->getFieldValue($componentId, 'colors', 'button_text_color', '#ffffff');
        $html .= $this->buildColorInput('footerButtonTextColor', 'Texto boton', $buttonTextColor);

        $buttonHoverBg = $this->renderer->getFieldValue($componentId, 'colors', 'button_hover_bg', '#0b5ed7');
        $html .= $this->buildColorInput('footerButtonHoverBg', 'Fondo boton hover', $buttonHoverBg);

        $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-expand me-2" style="color: #FF8600;"></i>';
        $html .= '            Espaciado';
        $html .= '        </h5>';

        $paddingY = $this->renderer->getFieldValue($componentId, 'spacing', 'padding_y', '3rem');
        $html .= $this->buildTextInput('footerPaddingY', 'Padding vertical', 'bi-arrows-vertical', $paddingY);

        $marginTop = $this->renderer->getFieldValue($componentId, 'spacing', 'margin_top', '0');
        $html .= $this->buildTextInput('footerMarginTop', 'Margen superior', 'bi-arrow-up', $marginTop);

        $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-stars me-2" style="color: #FF8600;"></i>';
        $html .= '            Efectos Visuales';
        $html .= '        </h5>';

        $inputRadius = $this->renderer->getFieldValue($componentId, 'visual_effects', 'input_border_radius', '6px');
        $html .= $this->buildTextInput('footerInputBorderRadius', 'Radio input', 'bi-square', $inputRadius);

        $buttonRadius = $this->renderer->getFieldValue($componentId, 'visual_effects', 'button_border_radius', '6px');
        $html .= $this->buildTextInput('footerButtonBorderRadius', 'Radio boton', 'bi-square', $buttonRadius);

        $transition = $this->renderer->getFieldValue($componentId, 'visual_effects', 'transition_duration', '0.3s');
        $html .= $this->buildTextInput('footerTransitionDuration', 'Duracion transicion', 'bi-hourglass', $transition);

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

        return $html;
    }

    // Helper methods
    private function buildSwitch(string $id, string $label, string $icon, $value): string
    {
        $checked = $value === true || $value === '1' || $value === 1 ? 'checked' : '';

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

        return $html;
    }

    private function buildTextInput(string $id, string $label, string $icon, mixed $value): string
    {
        $value = $this->normalizeStringValue($value);

        $html = '        <div class="mb-3">';
        $html .= '            <label for="' . esc_attr($id) . '" class="form-label small mb-1 fw-semibold">';
        $html .= '                <i class="bi ' . esc_attr($icon) . ' me-1" style="color: #FF8600;"></i>';
        $html .= '                ' . esc_html($label);
        $html .= '            </label>';
        $html .= '            <input type="text" class="form-control form-control-sm" id="' . esc_attr($id) . '" value="' . esc_attr($value) . '">';
        $html .= '        </div>';

        return $html;
    }

    private function buildPasswordInput(string $id, string $label, string $icon, mixed $value): string
    {
        $value = $this->normalizeStringValue($value);

        $html = '        <div class="mb-3">';
        $html .= '            <label for="' . esc_attr($id) . '" class="form-label small mb-1 fw-semibold">';
        $html .= '                <i class="bi ' . esc_attr($icon) . ' me-1" style="color: #FF8600;"></i>';
        $html .= '                ' . esc_html($label);
        $html .= '            </label>';
        $html .= '            <input type="password" class="form-control form-control-sm" id="' . esc_attr($id) . '" value="' . esc_attr($value) . '">';
        $html .= '            <div class="form-text small">URL oculta por seguridad</div>';
        $html .= '        </div>';

        return $html;
    }

    private function buildTextarea(string $id, string $label, string $icon, mixed $value): string
    {
        $value = $this->normalizeStringValue($value);

        $html = '        <div class="mb-3">';
        $html .= '            <label for="' . esc_attr($id) . '" class="form-label small mb-1 fw-semibold">';
        $html .= '                <i class="bi ' . esc_attr($icon) . ' me-1" style="color: #FF8600;"></i>';
        $html .= '                ' . esc_html($label);
        $html .= '            </label>';
        $html .= '            <textarea class="form-control form-control-sm" id="' . esc_attr($id) . '" rows="2">' . esc_textarea($value) . '</textarea>';
        $html .= '        </div>';

        return $html;
    }

    private function buildColorInput(string $id, string $label, mixed $value): string
    {
        $value = $this->normalizeStringValue($value);

        $html = '        <div class="mb-2 d-flex align-items-center gap-2">';
        $html .= '            <input type="color" class="form-control form-control-color" id="' . esc_attr($id) . '" value="' . esc_attr($value) . '" style="width: 40px; height: 30px;">';
        $html .= '            <label for="' . esc_attr($id) . '" class="form-label mb-0 small">' . esc_html($label) . '</label>';
        $html .= '        </div>';

        return $html;
    }

    /**
     * Normaliza un valor a string para inputs de formulario
     *
     * El repositorio convierte '0' a false y '1' a true automáticamente,
     * pero para campos de texto necesitamos el valor original como string.
     */
    private function normalizeStringValue(mixed $value): string
    {
        if ($value === false) {
            return '0';
        }
        if ($value === true) {
            return '1';
        }
        return (string) $value;
    }

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

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

        return $html;
    }
}
