"""Skin renderer: inject CSS variables into the base template."""

from __future__ import annotations

from pathlib import Path

from orxaq.experiences.skins.base import Skin

STATIC_DIR = Path(__file__).parent.parent / "static"


def render_skin(skin: Skin) -> str:
    """Render the dashboard HTML with skin-specific CSS variables.

    Takes the gamma.html base template and replaces the :root CSS
    variables with the skin's CSS variables. Also updates the title
    and adds skin-specific style overrides.
    """
    base_path = STATIC_DIR / "gamma.html"
    html = base_path.read_text(encoding="utf-8")

    # Replace title.
    html = html.replace("Orxaq Gamma Observatory", f"Orxaq {skin.display_name}")

    # Build CSS variable overrides.
    overrides = _build_css_overrides(skin)

    # Inject overrides after the :root block.
    html = _inject_css(html, overrides, skin)

    return html


def _build_css_overrides(skin: Skin) -> str:
    """Build CSS variable override block."""
    lines = [":root {"]
    for key, value in skin.css_variables.items():
        lines.append(f"  {key}: {value};")
    lines.append("}")
    return "\n".join(lines)


def _inject_css(html: str, overrides: str, skin: Skin) -> str:
    """Inject CSS overrides and skin-specific styles."""
    extra_css = ""

    if skin.name == "beta":
        extra_css = _beta_overrides()
    elif skin.name == "alpha":
        extra_css = _alpha_overrides()

    # Insert overrides just before </style>.
    injection = f"\n/* Skin: {skin.name} */\n{overrides}\n{extra_css}\n"
    html = html.replace("</style>", f"{injection}</style>")

    return html


def _beta_overrides() -> str:
    """Terminal-aesthetic CSS overrides for Beta skin."""
    return """
/* Beta: Command Engine overrides */
body { font-family: 'JetBrains Mono', 'Fira Code', monospace; font-size: 12px; }
.card, .tile, .panel, [class*="card"], [class*="tile"] {
  border-radius: 0 !important;
  box-shadow: none !important;
  border: 1px solid #333 !important;
}
h1, h2, h3 { text-transform: uppercase; letter-spacing: 2px; font-size: 0.9em; }
.sidebar { border-right: 1px solid #00ff41; }
.sidebar a { font-family: monospace; }
"""


def _alpha_overrides() -> str:
    """Glassmorphism CSS overrides for Alpha skin."""
    return """
/* Alpha: Glass Cathedral overrides */
body {
  background: linear-gradient(135deg, #1a1025 0%, #0d0a1a 50%, #1a1530 100%);
}
.card, .tile, .panel, [class*="card"], [class*="tile"] {
  background: rgba(255, 255, 255, 0.06) !important;
  backdrop-filter: blur(12px) !important;
  -webkit-backdrop-filter: blur(12px) !important;
  border: 1px solid rgba(255, 255, 255, 0.12) !important;
  border-radius: 16px !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3) !important;
}
.sidebar {
  background: rgba(30, 20, 50, 0.7) !important;
  backdrop-filter: blur(16px) !important;
}
@keyframes ambient-glow {
  0%, 100% { box-shadow: 0 0 20px rgba(155, 89, 182, 0.1); }
  50% { box-shadow: 0 0 30px rgba(155, 89, 182, 0.2); }
}
.main-content { animation: ambient-glow 6s ease-in-out infinite; }
"""
