from fastapi import APIRouter
from fastapi.responses import HTMLResponse

router = APIRouter(tags=["installation"])


@router.get("/freshworks/app", response_class=HTMLResponse)
def app_page() -> str:
    return _page_html()


@router.get("/freshworks/app/install", response_class=HTMLResponse)
def install_page() -> str:
    return _page_html()


def _page_html() -> str:
    return """
<!doctype html>
<html lang="es">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>MacroBase Freshchat API</title>
  <style>
    body {
      margin: 0;
      font-family: Arial, sans-serif;
      color: #172b4d;
      background: #f6f8fb;
      line-height: 1.5;
    }
    main {
      max-width: 920px;
      margin: 0 auto;
      padding: 40px 22px;
    }
    h1 { margin: 0 0 10px; font-size: 32px; }
    h2 { margin-top: 30px; font-size: 22px; }
    code, pre {
      background: #eef2f7;
      border: 1px solid #d8dee9;
      border-radius: 6px;
    }
    code { padding: 2px 5px; }
    pre { padding: 14px; overflow-x: auto; }
    .panel {
      background: white;
      border: 1px solid #dce3ee;
      border-radius: 8px;
      padding: 22px;
      margin-top: 18px;
    }
    .ok { color: #0f7b45; font-weight: 700; }
    .warn { color: #9a5b00; font-weight: 700; }
  </style>
</head>
<body>
  <main>
    <h1>MacroBase Freshchat API</h1>
    <p>Integracion backend para Freshchat/Freshworks con menus interactivos, botones, listas y flujos de encuesta.</p>

    <div class="panel">
      <p class="ok">Estado esperado: API publicada y lista para recibir eventos de Freshchat.</p>
      <p>Healthcheck: <code>/health</code></p>
      <p>Eventos: <code>/freshchat/events/conversation</code> y <code>/freshchat/events/message</code></p>
    </div>

    <h2>Uso Recomendado</h2>
    <p>
      Para botones reales en WhatsApp se debe instalar la Custom App privada de Freshworks incluida
      en el proyecto. La Custom App escucha eventos de conversacion y reenvia el payload completo a esta API.
    </p>

    <h2>Variables Requeridas En FastAPI</h2>
    <pre>APP_ENV=production
FRESHCHAT_API_TOKEN=token-interno
FRESHWORKS_API_BASE_URL=https://api.freshchat.com
FRESHWORKS_API_TOKEN=token-oficial-freshchat
FRESHWORKS_DEFAULT_ACTOR_TYPE=bot
FRESHWORKS_ASSUME_IDENTITY=false</pre>

    <h2>Custom App</h2>
    <p>Subir el archivo <code>freshworks_custom_app.zip</code> en Freshworks Developer Portal como Custom App.</p>
    <p>Parametros de instalacion:</p>
    <pre>api_base_url = URL publica de esta API
api_token = FRESHCHAT_API_TOKEN</pre>

    <h2>External App</h2>
    <p class="warn">
      External App sirve como ficha o pagina de instalacion. No reemplaza la Custom App si necesitas escuchar
      eventos y enviar botones automaticos.
    </p>
  </main>
</body>
</html>
"""
