"""Template management component.

Provides:
- Template library browser (list of available templates)
- Apply template button
- Save current view as template (name + description)
- Active template indicator
- Session auto-save status
"""

from __future__ import annotations

from typing import Any

import dash_bootstrap_components as dbc
from dash import dcc, html

from impakt.web.state import AppState


def build_template_panel(app_state: AppState) -> dbc.Card:
    """Build the template management panel."""
    template_names = app_state.template_names
    active = app_state.active_template

    # Active template indicator
    active_display = html.Div(
        [
            html.Span("Active: ", style={"fontSize": "11px", "color": "#666"}),
            html.Span(
                active.name if active else "None",
                style={"fontSize": "11px", "fontWeight": "bold"},
            ),
            html.Span(
                f" v{active.version}" if active else "",
                style={"fontSize": "10px", "color": "#999"},
            ),
        ],
        className="mb-2",
    )

    return dbc.Card(
        [
            dbc.CardHeader("Templates", className="fw-bold py-2"),
            dbc.CardBody(
                [
                    active_display,
                    # Template library browser
                    dbc.Label("Library", size="sm", className="mb-1"),
                    dbc.Select(
                        id="template-library-select",
                        options=[{"label": n, "value": n} for n in template_names],
                        placeholder="Select a template...",
                        size="sm",
                        className="mb-2",
                        style={"fontSize": "12px"},
                    ),
                    dbc.ButtonGroup(
                        [
                            dbc.Button(
                                "Apply",
                                id="apply-template-btn",
                                color="primary",
                                size="sm",
                                style={"fontSize": "11px"},
                                disabled=not template_names,
                            ),
                            dbc.Button(
                                "Delete",
                                id="delete-template-btn",
                                color="outline-danger",
                                size="sm",
                                style={"fontSize": "11px"},
                                disabled=not template_names,
                            ),
                        ],
                        className="mb-3 w-100",
                    ),
                    html.Hr(style={"margin": "8px 0"}),
                    # Save current view as template
                    dbc.Label("Save Current View", size="sm", className="mb-1"),
                    dbc.Input(
                        id="save-template-name",
                        placeholder="Template name",
                        size="sm",
                        className="mb-1",
                        style={"fontSize": "12px"},
                    ),
                    dbc.Textarea(
                        id="save-template-desc",
                        placeholder="Description (optional)",
                        size="sm",
                        className="mb-2",
                        style={"fontSize": "12px", "height": "50px"},
                    ),
                    dbc.Button(
                        "Save as Template",
                        id="save-template-btn",
                        color="success",
                        size="sm",
                        className="w-100 mb-2",
                        style={"fontSize": "11px"},
                    ),
                    html.Div(id="template-status", className="mt-1"),
                ],
                className="py-2",
            ),
        ],
        className="mb-2",
    )
