"""HTML header, navigation, and footer sections."""

from datetime import datetime
from typing import Dict

from .._css import REPORT_CSS
from .._helpers import safe


def generate_html_header(title: str) -> str:
    """Generate HTML header with CSS styling."""
    return f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{safe(title)}</title>
    <style>{REPORT_CSS}
    </style>
</head>
<body>
    <header>
        <h1>{safe(title)}</h1>
        <p class="subtitle">Generated on {datetime.now().strftime("%Y-%m-%d %H:%M")}</p>
    </header>
"""


def generate_nav_section(data: Dict) -> str:
    """Generate navigation section."""
    config = data.get("config", {})
    selection_enabled = config.get("model", {}).get("selection", {}).get("enabled", True)

    sections = [
        ("summary", "Summary"),
        ("methods", "Methods"),
        ("quality", "Data Quality"),
    ]
    if selection_enabled:
        sections.append(("model", "Model Selection"))
    sections.extend(
        [
            ("clusters", "Phenotypes"),
            ("classification", "Classification"),
            ("stability", "Stability"),
            ("validation", "Validation"),
            ("outcomes", "Outcomes"),
            ("survival", "Survival"),
            ("multistate", "Multistate"),
            ("features", "Features"),
        ]
    )
    if data.get("external_validation_results"):
        sections.append(("external_validation", "External Validation"))

    links = " ".join([f'<li><a href="#{id}">{name}</a></li>' for id, name in sections])
    return f"""
    <nav>
        <ul>{links}</ul>
    </nav>
"""


def generate_html_footer() -> str:
    """Generate HTML footer."""
    return f"""
    <footer>
        <p>Generated by PhenoCluster | {datetime.now().strftime("%Y-%m-%d %H:%M")}</p>
        <p>Latent Class / Profile Analysis for Clinical Phenotype Discovery</p>
    </footer>
</body>
</html>
"""
