"""
Welcome Screen - The Portal to The Grid.

"The Grid. A digital frontier."
"""

from typing import Optional, TYPE_CHECKING

if TYPE_CHECKING:
    from core.grid import Grid


WELCOME_BANNER = """
╔═══════════════════════════════════════════════════════════════════════════╗
║                                                                           ║
║  ████████╗██╗  ██╗███████╗     ██████╗ ██████╗ ██╗██████╗                 ║
║  ╚══██╔══╝██║  ██║██╔════╝    ██╔════╝ ██╔══██╗██║██╔══██╗                ║
║     ██║   ███████║█████╗      ██║  ███╗██████╔╝██║██║  ██║                ║
║     ██║   ██╔══██║██╔══╝      ██║   ██║██╔══██╗██║██║  ██║                ║
║     ██║   ██║  ██║███████╗    ╚██████╔╝██║  ██║██║██████╔╝                ║
║     ╚═╝   ╚═╝  ╚═╝╚══════╝     ╚═════╝ ╚═╝  ╚═╝╚═╝╚═════╝                ║
║                                                                           ║
║                     « A Digital Frontier »                                ║
║                                                                           ║
"""


def render_welcome(grid: Optional["Grid"] = None) -> str:
    """
    Render the welcome screen.

    Args:
        grid: Optional existing Grid instance for status display

    Returns:
        Formatted welcome screen string
    """
    WIDTH = 75  # Inner width (between ║ chars)
    lines = [WELCOME_BANNER.strip()]

    # Status section
    lines.append("╠" + "═" * WIDTH + "╣")
    lines.append("║" + " " * WIDTH + "║")

    if grid:
        # Show existing Grid status
        energy_bar = grid.get_energy_bar(20)
        energy_pct = f"{grid.energy_percentage():.0f}%"
        status_line = f"   GRID STATUS         {energy_bar}  {energy_pct} Energy"
        lines.append("║" + status_line + " " * (WIDTH - len(status_line)) + "║")
        prog_line = f"   Active Programs     {len(grid.active_programs)}"
        lines.append("║" + prog_line + " " * (WIDTH - len(prog_line)) + "║")
        clust_line = f"   Clusters            {len(grid.clusters)}"
        lines.append("║" + clust_line + " " * (WIDTH - len(clust_line)) + "║")
        cyc_line = f"   Cycles completed    {grid.cycles_completed}"
        lines.append("║" + cyc_line + " " * (WIDTH - len(cyc_line)) + "║")
    else:
        # Show empty Grid status
        lines.append("║   GRID STATUS         ████████████████████  100% Energy     ║")
        lines.append("║   Active Programs     0                                     ║")
        lines.append("║   Clusters            0                                     ║")
        lines.append("║   Cycles completed    0                                     ║")

    lines.append("║" + " " * WIDTH + "║")

    # Input prompt section
    lines.append("╠" + "═" * WIDTH + "╣")
    lines.append("║" + " " * WIDTH + "║")
    lines.append("║   What would you like to build?                                 ║")
    lines.append("║   ▸ _                                                           ║")
    lines.append("║" + " " * WIDTH + "║")
    lines.append("║   Examples:                                                     ║")
    lines.append("║   • \"a REST API for user authentication\"                        ║")
    lines.append("║   • \"a CLI tool that converts markdown to PDF\"                  ║")
    lines.append("║   • \"fix the bug in my login form\"                              ║")
    lines.append("║" + " " * WIDTH + "║")
    lines.append("╚" + "═" * WIDTH + "╝")

    return "\n".join(lines)


def render_minimal_welcome() -> str:
    """Render a minimal welcome banner."""
    return """
╔═══════════════════════════════════════════════════════════════════╗
║     THE GRID  « A Digital Frontier »                              ║
╚═══════════════════════════════════════════════════════════════════╝
    """.strip()


def render_goodbye() -> str:
    """Render goodbye message."""
    return """
╔═══════════════════════════════════════════════════════════════════════════════╗
║                                                                               ║
║                         End of Line                                           ║
║                                                                               ║
║                  The Grid thanks you, User.                                   ║
║                                                                               ║
╚═══════════════════════════════════════════════════════════════════════════════╝
    """.strip()
