"""Tests for the post-install getting-started guidance panel."""

from __future__ import annotations

from unittest.mock import MagicMock

import pytest
from rich.panel import Panel

from ilum.cli.guidance import GuidanceContext, _suggest_module, show_getting_started
from ilum.cli.output import IlumConsole


@pytest.fixture()
def console() -> IlumConsole:
    c = IlumConsole()
    c._console = MagicMock()
    return c


def _panel_text(console: IlumConsole) -> str:
    """Extract the text content from the Panel passed to console._console.print."""
    panel = console._console.print.call_args[0][0]
    assert isinstance(panel, Panel)
    # Panel stores its content in the .renderable attribute
    return str(panel.renderable)


class TestShowGettingStarted:
    def test_panel_shown_with_module_count(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui", "jupyter"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        console._console.print.assert_called_once()
        assert "3 modules" in _panel_text(console)

    def test_connect_header(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="my-release",
            namespace="ns",
            command="connect",
        )
        show_getting_started(console, ctx)
        text = _panel_text(console)
        assert "Connected to release" in text
        assert "my-release" in text

    def test_install_header(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "Ilum is running" in _panel_text(console)

    def test_always_shows_access_open(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="quickstart",
        )
        show_getting_started(console, ctx)
        assert "ilum access open" in _panel_text(console)

    def test_always_shows_status_and_doctor(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        text = _panel_text(console)
        assert "ilum status" in text
        assert "ilum doctor" in text

    def test_always_shows_logs(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "ilum logs core" in _panel_text(console)

    def test_always_shows_docs_link(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "ilum.cloud/docs/cli/overview" in _panel_text(console)

    def test_always_shows_module_list(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "ilum module list" in _panel_text(console)

    def test_suggests_enable_when_modules_missing(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "ilum module enable" in _panel_text(console)

    def test_web_service_shown_for_jupyter(self, console: IlumConsole) -> None:
        ctx = GuidanceContext(
            modules=["core", "ui", "jupyter"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        assert "Jupyter" in _panel_text(console)

    def test_suppressed_when_quiet(self, console: IlumConsole) -> None:
        console.quiet = True
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        # panel() is a no-op when quiet, so print should not be called
        console._console.print.assert_not_called()

    def test_suppressed_for_json_output(self, console: IlumConsole) -> None:
        console.output_format = "json"
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        console._console.print.assert_not_called()

    def test_suppressed_for_yaml_output(self, console: IlumConsole) -> None:
        console.output_format = "yaml"
        ctx = GuidanceContext(
            modules=["core", "ui"],
            release="ilum",
            namespace="default",
            command="install",
        )
        show_getting_started(console, ctx)
        console._console.print.assert_not_called()


class TestSuggestModule:
    def test_suggests_jupyter_first(self) -> None:
        assert _suggest_module({"core", "ui"}) == "jupyter"

    def test_suggests_airflow_when_jupyter_enabled(self) -> None:
        assert _suggest_module({"core", "ui", "jupyter"}) == "airflow"

    def test_empty_when_all_enabled(self) -> None:
        all_suggested = {"jupyter", "airflow", "monitoring", "superset", "mlflow", "trino"}
        assert _suggest_module(all_suggested) == ""
