<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Asistente Virtual · Agilsoft</title>
    <!-- Bootstrap 5 & FontAwesome -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * { box-sizing: border-box; }
        body {
            background-color: #f1f5f9;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
            height: 100vh;
            margin: 0;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .chat-container {
            width: 100%;
            max-width: 540px;
            height: 100vh;
            background: #ffffff;
            display: flex;
            flex-direction: column;
            box-shadow: 0 10px 30px rgba(0,0,0,0.08);
        }
        @media (min-width: 576px) {
            .chat-container {
                height: 90vh;
                border-radius: 20px;
                overflow: hidden;
            }
        }
        .chat-header {
            background: #0284c7;
            color: #ffffff;
            padding: 16px 20px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .chat-body {
            flex: 1;
            padding: 20px;
            overflow-y: auto;
            background: #f8fafc;
        }
        .bubble {
            max-width: 82%;
            padding: 12px 16px;
            border-radius: 16px;
            margin-bottom: 14px;
            font-size: 14.5px;
            line-height: 1.45;
            word-wrap: break-word;
            box-shadow: 0 1px 3px rgba(0,0,0,0.04);
        }
        .bubble-bot {
            background: #ffffff;
            color: #1e293b;
            margin-right: auto;
            border-bottom-left-radius: 4px;
            border: 1px solid #e2e8f0;
        }
        .bubble-user {
            background: #0284c7;
            color: #ffffff;
            margin-left: auto;
            border-bottom-right-radius: 4px;
        }
        .chat-footer {
            padding: 14px 16px;
            background: #ffffff;
            border-top: 1px solid #e2e8f0;
        }
        .suggestion-chip {
            background: #f1f5f9;
            color: #0284c7;
            border: 1px solid #e2e8f0;
            border-radius: 20px;
            padding: 6px 14px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            display: inline-block;
            margin-right: 6px;
            margin-bottom: 6px;
            transition: all 0.15s ease;
        }
        .suggestion-chip:hover {
            background: #0284c7;
            color: #ffffff;
        }
        .viral-badge {
            font-size: 11px;
            color: #94a3b8;
            text-align: center;
            padding-top: 8px;
            text-decoration: none;
            display: block;
        }
        .viral-badge:hover { color: #0284c7; }
    </style>
</head>
<body>

<div class="chat-container">
    <!-- ENCABEZADO -->
    <div class="chat-header">
        <div class="d-flex align-items-center gap-3">
            <div style="width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,0.25); display: flex; align-items: center; justify-content: center; font-size: 22px;">
                🤖
            </div>
            <div>
                <h6 class="fw-bold mb-0 text-white">Asistente Virtual</h6>
                <small class="opacity-75">Agilsoft · En línea</small>
            </div>
        </div>
                                <a href="https://wa.me/50325198239" target="_blank" class="btn btn-success btn-sm fw-bold rounded-pill px-3 shadow-sm" title="Escribir por WhatsApp">
                <i class="fa-brands fa-whatsapp me-1"></i> WhatsApp
            </a>
            </div>

    <!-- CUERPO DE MENSAJES -->
    <div id="chat_body" class="chat-body">
        <div class="bubble bubble-bot">
            Esto es Agilsoft, que vamos a hacer hoy?        </div>
        <div id="suggestions_box" class="mt-2 mb-3"></div>
    </div>

    <!-- PIE DE ENTRADA -->
    <div class="chat-footer">
        <form id="chat_form" onsubmit="enviarMensaje(event)">
            <div class="input-group">
                <input type="text" id="chat_input" class="form-control rounded-pill-start border-end-0 py-2 ps-3" placeholder="Escribe tu mensaje aquí..." required autocomplete="off">
                <button type="submit" id="chat_btn" class="btn btn-primary rounded-pill-end px-4 fw-bold" style="background: #0284c7; border-color: #0284c7;">
                    <i class="fa-solid fa-paper-plane"></i>
                </button>
            </div>
        </form>

                    <a href="https://agilpos.com" target="_blank" class="viral-badge">
                ⚡ Desarrollado con <strong>AgilSoft AI</strong> · Crea tu propio asistente gratis
            </a>
            </div>
</div>

<script>
const API_KEY = 'ai_955c74dbbb20d9dfa25d816a';

function enviarMensaje(e, textoPredefinido = null) {
    if (e) e.preventDefault();
    const input = document.getElementById('chat_input');
    const msg = textoPredefinido || input.value.trim();
    if (!msg) return;

    const chatBody = document.getElementById('chat_body');
    chatBody.innerHTML += `<div class="bubble bubble-user">${escapeHtml(msg)}</div>`;
    input.value = '';
    chatBody.scrollTop = chatBody.scrollHeight;

    const idTyping = 'typing_' + Date.now();
    chatBody.innerHTML += `<div id="${idTyping}" class="bubble bubble-bot text-muted"><i class="fa-solid fa-spinner fa-spin me-2"></i> Escribiendo...</div>`;
    chatBody.scrollTop = chatBody.scrollHeight;

    fetch('api/v1/ai/chat_universal.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-API-KEY': API_KEY },
        body: JSON.stringify({ mensaje: msg, api_key: API_KEY })
    })
    .then(r => r.json())
    .then(d => {
        const typingElem = document.getElementById(idTyping);
        if (typingElem) typingElem.remove();

        if (d.ok) {
            chatBody.innerHTML += `<div class="bubble bubble-bot">${escapeHtml(d.mensaje_ia).replace(/\n/g, '<br>')}</div>`;

            // Sugerencias interactivas
            if (d.sugerencias && d.sugerencias.length > 0) {
                let chips = '<div class="mt-2 mb-3">';
                d.sugerencias.forEach(s => {
                    chips += `<span class="suggestion-chip" onclick="enviarMensaje(null, '${escapeHtml(s)}')">${escapeHtml(s)}</span>`;
                });
                chips += '</div>';
                chatBody.innerHTML += chips;
            }
            chatBody.scrollTop = chatBody.scrollHeight;
        } else {
            chatBody.innerHTML += `<div class="bubble bubble-bot text-danger">Error: ${escapeHtml(d.error)}</div>`;
        }
    }).catch(err => {
        const typingElem = document.getElementById(idTyping);
        if (typingElem) typingElem.remove();
        chatBody.innerHTML += `<div class="bubble bubble-bot text-danger">Problema de conexión con el servidor.</div>`;
    });
}

function escapeHtml(text) {
    const div = document.createElement('div');
    div.innerText = text;
    return div.innerHTML;
}
</script>

</body>
</html>
