"""Targeted CLI coverage tests for uncovered human-readable print paths.

Covers:
- Lines 59-67: _cmd_status text path
- Lines 97-101: _cmd_credit_ontology text path (no --export, no --json)
- Lines 482-577: _cmd_credit_regulatory_export
- Lines 1113-1121: _cmd_providers_health_check text path
- Lines 1185-1190: _cmd_providers_list text path
- Lines 1372-1430: _cmd_model_diff text path
- Lines 1517-1529: _cmd_export_dag csv/dot/mermaid paths
"""

from __future__ import annotations

import json

import pytest

from orxaq.credit.ontology import build_credit_ontology
from orxaq.experiences.cli import main
from orxaq.kernel.checkpoint import CheckpointStore, create_checkpoint


@pytest.fixture(scope="module")
def _checkpoint_store(tmp_path_factory):
    """Create a checkpoint store with two identical checkpoints."""
    store_dir = tmp_path_factory.mktemp("checkpoints")
    store = CheckpointStore(store_dir)
    dag = build_credit_ontology()
    cp1 = create_checkpoint(dag, "credit", "v1")
    cp2 = create_checkpoint(dag, "credit", "v2")
    store.save(cp1)
    store.save(cp2)
    return store_dir


@pytest.mark.filterwarnings("ignore")
class TestCLIUncoveredPaths:
    def test_status_text_output(self, capsys: pytest.CaptureFixture) -> None:
        """L59-67: _cmd_status text (non-JSON) path."""
        rc = main(["status"])
        out = capsys.readouterr().out
        assert rc == 0
        assert "Orxaq Status" in out

    def test_credit_ontology_text(self, capsys: pytest.CaptureFixture) -> None:
        """L97-101: _cmd_credit_ontology else branch (no --export, no --json)."""
        rc = main(["credit", "ontology"])
        out = capsys.readouterr().out
        assert rc == 0
        assert "Credit Ontology" in out

    def test_providers_health_text(self, capsys: pytest.CaptureFixture) -> None:
        """L1113-1121: _cmd_providers_health_check text path."""
        rc = main(["providers", "check"])
        out = capsys.readouterr().out
        assert rc == 0
        assert "Provider Health" in out

    def test_providers_list_text(self, capsys: pytest.CaptureFixture) -> None:
        """L1185-1190: _cmd_providers_list text path."""
        rc = main(["providers", "list"])
        out = capsys.readouterr().out
        assert rc == 0
        assert "Available Models" in out

    def test_model_diff_identical_text(
        self, _checkpoint_store, capsys: pytest.CaptureFixture
    ) -> None:
        """L1372-1430: _cmd_model_diff text path with identical checkpoints."""
        rc = main(
            [
                "model",
                "diff",
                "--store",
                str(_checkpoint_store),
                "--name",
                "credit",
                "--v1",
                "v1",
                "--v2",
                "v2",
            ]
        )
        out = capsys.readouterr().out
        assert rc == 0
        assert "Diff" in out or "Identical" in out or "credit" in out

    def test_export_dag_csv(self, capsys: pytest.CaptureFixture) -> None:
        """L1517-1521: _cmd_export_dag csv format path."""
        rc = main(["export", "dag", "--format", "csv"])
        capsys.readouterr()
        assert rc == 0

    def test_export_dag_dot(self, capsys: pytest.CaptureFixture) -> None:
        """L1522-1525: _cmd_export_dag dot format path."""
        rc = main(["export", "dag", "--format", "dot"])
        out = capsys.readouterr().out
        assert rc == 0
        assert "digraph" in out.lower() or "->" in out

    def test_export_dag_mermaid(self, capsys: pytest.CaptureFixture) -> None:
        """L1526-1529: _cmd_export_dag mermaid format path."""
        rc = main(["export", "dag", "--format", "mermaid"])
        out = capsys.readouterr().out
        assert rc == 0
        assert len(out) > 0

    def test_regulatory_export_json(self, capsys: pytest.CaptureFixture) -> None:
        """L482-577: _cmd_credit_regulatory_export with --json."""
        rc = main(["--json", "credit", "regulatory-export"])
        out = capsys.readouterr().out
        assert rc == 0
        payload = json.loads(out)
        assert "as_of_date" in payload

    def test_regulatory_export_csv(self, capsys: pytest.CaptureFixture) -> None:
        """L560-566: _cmd_credit_regulatory_export csv format path."""
        rc = main(["credit", "regulatory-export", "--format", "csv"])
        capsys.readouterr()
        assert rc == 0
