"""Bandito TUI application."""

from __future__ import annotations

from textual.app import App

from .screens import DashboardScreen, GradingScreen


class BanditoApp(App):
    TITLE = "bandito"
    CSS = """
    Screen {
        background: $surface;
    }

    #dashboard-layout {
        height: 1fr;
    }

    #sidebar {
        width: 36;
        border-right: solid $primary-background;
        padding: 1 2;
    }

    #main-panel {
        padding: 1 2;
    }

    #project-header {
        margin-bottom: 1;
    }

    #stats-row {
        height: auto;
        margin-bottom: 1;
    }

    #stats-row Static {
        width: 1fr;
        padding: 1 2;
        border: round $primary-background;
        margin-right: 1;
    }

    #model-breakdown {
        padding: 1 2;
        border: round $primary-background;
        margin-bottom: 1;
    }

    #span-breakdown {
        padding: 1 2;
        border: round $primary-background;
    }

    .section-title {
        text-style: bold;
        margin-bottom: 1;
        color: $text;
    }

    #grading-layout {
        height: 1fr;
    }

    #trace-sidebar {
        width: 32;
        border-right: solid $primary-background;
        padding: 1;
    }

    #grading-title {
        margin-bottom: 1;
        padding: 0 1;
    }

    #detail-panel {
        padding: 1 2;
    }

    #trace-input {
        margin-bottom: 1;
    }

    #trace-output {
        margin-bottom: 1;
    }

    #trace-summary {
        margin-bottom: 1;
    }

    #span-tree {
        margin-top: 1;
        padding: 1;
        border: round $primary-background;
    }

    #comment-label {
        margin-top: 1;
        padding: 0 1;
    }

    #comment-input {
        margin: 0 1;
    }

    OptionList {
        height: 1fr;
    }
    """

    def __init__(
        self,
        store=None,
        client=None,
        project: str | None = None,
        initial_screen: str = "dashboard",
    ) -> None:
        super().__init__()
        if store is None:
            from ..storage import get_store
            store = get_store(project=project)
        self.store = store
        self.client = client  # BanditoClient for evaluation reads/writes
        self.project = project
        self._initial_screen = initial_screen

    def on_mount(self) -> None:
        if self._initial_screen == "grading":
            if not self.project:
                projects = self.store.list_projects()
                self.project = projects[0] if projects else None
            self.push_screen(GradingScreen())
        else:
            self.push_screen(DashboardScreen())

    def on_key(self, event) -> None:
        if event.key == "r":
            self._refresh_current_view(lite=True)
        elif event.key == "R":
            self._refresh_current_view(lite=False)

    def _refresh_current_view(self, lite: bool = True) -> None:
        """Refresh the current view's data."""
        from ...retrieval import get_retrieval

        if not self.project:
            return

        r = get_retrieval(self.project)
        counts = r.refresh(lite=lite)

        current_screen = self.screen
        if hasattr(current_screen, "refresh"):
            current_screen.refresh()

        self.notify(f"Refreshed: {counts.get('traces', 0)} traces")
