"""
BizNode Support — HTML Email Templates
=======================================
Corporate-branded HTML templates for all support email types.
"""

_BASE_CSS = """
body { margin: 0; padding: 0; background-color: #0a0a0a; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
.wrapper { max-width: 640px; margin: 0 auto; background: #0a0a0a; }
.header { background: linear-gradient(135deg, #1a1a2e, #16213e); padding: 24px; text-align: center; border-radius: 8px 8px 0 0; }
.header img { max-height: 60px; }
.header h2 { color: #00d4ff; margin: 12px 0 0; font-size: 18px; font-weight: 600; }
.content { background: #f8f9fa; padding: 32px; color: #333; line-height: 1.7; font-size: 15px; }
.content h3 { color: #1a1a2e; margin-top: 0; }
.content a { color: #00d4ff; text-decoration: none; }
.content a:hover { text-decoration: underline; }
.btn { display: inline-block; background: #00d4ff; color: #fff !important; padding: 12px 28px; border-radius: 6px; text-decoration: none; font-weight: 600; margin: 16px 0; }
.info-box { background: #e8f4fd; border-left: 4px solid #00d4ff; padding: 16px; margin: 16px 0; border-radius: 0 6px 6px 0; }
.warn-box { background: #fff8e1; border-left: 4px solid #ffc107; padding: 16px; margin: 16px 0; border-radius: 0 6px 6px 0; font-size: 13px; color: #555; }
.code { background: #1a1a2e; color: #00d4ff; padding: 12px 16px; border-radius: 6px; font-family: 'Consolas', 'Courier New', monospace; font-size: 13px; margin: 8px 0; display: block; white-space: pre-wrap; }
ul { padding-left: 20px; }
li { margin-bottom: 6px; }
.footer { background: #111; padding: 24px; text-align: center; border-radius: 0 0 8px 8px; }
.footer p { color: #888; font-size: 12px; margin: 4px 0; }
.footer a { color: #00d4ff; text-decoration: none; }
.ticket-badge { display: inline-block; background: #1a1a2e; color: #00d4ff; padding: 4px 12px; border-radius: 4px; font-size: 13px; font-weight: 600; }
"""

_DISCLAIMER = (
    "This email was sent by an automated support system. "
    "BizNode&trade; and 1BZ DZIT DAO LLC accept no responsibility for any data loss "
    "or damages arising from the use of this software. "
    "All information is provided as-is for troubleshooting purposes only."
)


def _wrap(ticket_id: str, subject_hint: str, body_html: str) -> str:
    return f"""<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style>{_BASE_CSS}</style></head>
<body>
<div class="wrapper">
  <div class="header">
    <img src="https://biznode.1bz.biz/logo.png" alt="BizNode">
    <h2>{subject_hint}</h2>
  </div>
  <div class="content">
    <p><span class="ticket-badge">{ticket_id}</span></p>
    {body_html}
  </div>
  <div class="footer">
    <p>BizNode&trade; Support | <a href="mailto:biznodesupport@1bz.biz">biznodesupport@1bz.biz</a> | <a href="https://biznode.1bz.biz">biznode.1bz.biz</a></p>
    <p style="margin-top:12px;font-size:11px;color:#666;">{_DISCLAIMER}</p>
  </div>
</div>
</body>
</html>"""


def welcome_template(ticket_id: str) -> str:
    body = """
    <h3>Welcome to BizNode&trade; Support</h3>
    <p>Thank you for reaching out. We have received your request and created a support ticket for you.</p>

    <div class="info-box">
      <strong>Installation Requirements</strong>
      <ul>
        <li>Python 3.10 or higher</li>
        <li>Docker Desktop (for Qdrant vector database)</li>
        <li>Ollama (local LLM engine)</li>
        <li>PostgreSQL or SQLite (database backend)</li>
        <li>8 GB RAM minimum (16 GB recommended)</li>
      </ul>
    </div>

    <h3>Quick Setup Guide</h3>
    <ol>
      <li>Download BizNode from the link below</li>
      <li>Extract the ZIP file to a folder (e.g. <code>C:\\BizNode</code>)</li>
      <li>Open a terminal in that folder</li>
      <li>Install dependencies: <span class="code">pip install -r requirements.txt</span></li>
      <li>Start Docker Desktop and wait for it to be ready</li>
      <li>Pull the Ollama model: <span class="code">ollama pull qwen3.5:4b</span></li>
      <li>Run the setup wizard: <span class="code">python service_manager.py start</span></li>
      <li>Open the dashboard at <a href="http://localhost:7777">http://localhost:7777</a></li>
    </ol>

    <p style="text-align:center;">
      <a class="btn" href="https://biznode.1bz.biz/downloads.php">Download BizNode</a>
    </p>

    <div class="warn-box">
      Please report any additional issues within <strong>24 hours</strong>.
      If we do not hear back, this ticket will be automatically closed.
    </div>
    """
    return _wrap(ticket_id, "Support Ticket Created", body)


def request_logs_template(ticket_id: str) -> str:
    body = """
    <h3>We Need Your Logs to Help</h3>
    <p>Thank you for the additional details. To diagnose your issue accurately, we need to examine your installation logs.</p>

    <div class="info-box">
      <strong>How to send logs:</strong>
      <ol>
        <li>Open your BizNode Dashboard at <a href="http://localhost:7777">http://localhost:7777</a></li>
        <li>Click the <strong>Support</strong> button in the top menu</li>
        <li>Click <strong>"Send Logs to Support"</strong></li>
        <li>Your logs will be securely sent to our support team</li>
      </ol>
    </div>

    <div class="warn-box">
      <strong>Privacy Notice:</strong> Pressing the Support button will send your installation logs
      for examination. No personal data or sensitive information will be shared.
      BizNode&trade; and 1BZ DZIT DAO LLC accept no responsibility for any data loss.
    </div>

    <p>Once we receive your logs, our AI will analyze them and respond with specific solutions.</p>
    """
    return _wrap(ticket_id, "Logs Required", body)


def ai_response_template(ticket_id: str, analysis: str) -> str:
    safe = analysis.replace("\n", "<br>")
    body = f"""
    <h3>Analysis Results</h3>
    <p>Our AI support system has analyzed your issue. Here are the findings and recommended steps:</p>

    <div class="info-box">
      {safe}
    </div>

    <p>If this does not resolve your issue, please reply to this email with any new error messages or additional details.</p>

    <div class="warn-box">
      This ticket will be automatically closed after <strong>24 hours</strong> of no activity.
      Reply to keep it open.
    </div>
    """
    return _wrap(ticket_id, "Support Analysis", body)


def auto_close_template(ticket_id: str) -> str:
    body = """
    <h3>Ticket Closed</h3>
    <p>This support ticket has been automatically closed after 24 hours of inactivity.</p>

    <p>We hope your issue has been resolved. If you still need assistance, simply reply to this
    email and a new ticket will be created for you.</p>

    <p>Thank you for choosing BizNode&trade;.</p>

    <div class="info-box">
      <strong>Helpful Resources:</strong>
      <ul>
        <li><a href="https://biznode.1bz.biz/downloads.php">Downloads &amp; Updates</a></li>
        <li><a href="https://biznode.1bz.biz">BizNode Website</a></li>
      </ul>
    </div>
    """
    return _wrap(ticket_id, "Ticket Closed", body)


def resolved_template(ticket_id: str) -> str:
    body = """
    <h3>Issue Resolved</h3>
    <p>Your support ticket has been marked as <strong>resolved</strong>.</p>

    <p>If the issue reoccurs or you have any other questions, feel free to reply to this email
    and we will reopen your ticket immediately.</p>

    <p>Thank you for using BizNode&trade;. We appreciate your business.</p>
    """
    return _wrap(ticket_id, "Ticket Resolved", body)
