"""
Main entry point for running ScalarGrad as a module.

Usage: python -m scalargrad
"""

from . import __version__, __author__
from .config import logger
from .theme import get_console, get_current_theme_name  # noqa: F401


def main():
    """Main function for command-line interface."""
    from rich.panel import Panel
    from rich.table import Table
    from rich.syntax import Syntax
    from rich.columns import Columns
    from rich import box

    con = get_console()
    theme = get_current_theme_name()

    # ── Banner ─────────────────────────────────────────────────────────────
    banner_text = (
        f"[header]ScalarGrad[/header]\n"
        f"[subheader]A Minimal Scalar-Valued Autograd Engine[/subheader]\n\n"
        f"[label]Version:[/label]   [value]{__version__}[/value]\n"
        f"[label]Author:[/label]    [value]{__author__}[/value]\n"
        f"[label]Theme:[/label]     [value]{theme}[/value]"
    )
    con.print(Panel(
        banner_text,
        title="[banner_border]>>> ScalarGrad <<<[/banner_border]",
        subtitle=f"[info]https://github.com/THAMIZH-ARASU/scalargrad[/info]",
        border_style="banner_border",
        padding=(1, 4),
    ))

    # ── Quick-start code snippet ────────────────────────────────────────────
    quick_start = """\
from scalargrad import Scalar

a = Scalar(2.0)
b = Scalar(3.0)
c = a * b + b ** 2   # build computation graph
c.backward()          # run autodiff
print(f"dc/da = {a.grad}, dc/db = {b.grad}")
"""
    con.print(
        Panel(
            Syntax(quick_start, "python", theme="monokai", line_numbers=False),
            title="[subheader]Quick Start[/subheader]",
            border_style="table_border",
            padding=(0, 2),
        )
    )

    # ── Usage table ─────────────────────────────────────────────────────────
    tbl = Table(
        title="[table_header]Resources[/table_header]",
        box=box.ROUNDED,
        border_style="table_border",
        header_style="table_header",
        show_lines=False,
        expand=False,
    )
    tbl.add_column("Resource", style="label", no_wrap=True)
    tbl.add_column("Path / URL", style="value")
    tbl.add_row("Basic examples",   "examples/basic_operations.py")
    tbl.add_row("Neural network",   "examples/neural_network.py")
    tbl.add_row("Documentation",    "https://github.com/THAMIZH-ARASU/scalargrad#readme")
    tbl.add_row("Issue tracker",    "https://github.com/THAMIZH-ARASU/scalargrad/issues")

    themes_tbl = Table(
        title="[table_header]Built-in Themes[/table_header]",
        box=box.ROUNDED,
        border_style="table_border",
        header_style="table_header",
        show_lines=False,
        expand=False,
    )
    themes_tbl.add_column("Name",        style="label")
    themes_tbl.add_column("Description", style="info")
    themes_tbl.add_row("default",   "Blue / green sci-fi (dark terminal)")
    themes_tbl.add_row("dark",      "Purple / cyan electro")
    themes_tbl.add_row("cyberpunk", "Neon magenta / yellow")
    themes_tbl.add_row("light",     "Muted tones for bright terminals")
    themes_tbl.add_row("minimal",   "Bold / italic only, no colour")

    con.print(Columns([tbl, themes_tbl], equal=False, expand=False))
    con.print()

    # Switch theme hint
    con.print(
        "[info]Tip:[/info] Switch themes with "
        "[code]import scalargrad; scalargrad.set_theme(\"cyberpunk\")[/code]"
    )
    con.print()

    logger.info("[success]ScalarGrad loaded successfully![/success]")


if __name__ == "__main__":
    main()

