from __future__ import annotations

import json
import shutil
import subprocess
from pathlib import Path

import pytest

from skills._shared.python.skill_testing import assert_skill_contract, repo_root


def skill_root() -> Path:
    return Path(__file__).resolve().parents[1]


def test_workflows_reporting_project_retro_contract() -> None:
    assert_skill_contract(skill_root())


def test_workflows_reporting_project_retro_consumes_installed_repo_retro() -> None:
    text = (skill_root() / "SKILL.md").read_text(encoding="utf-8")

    assert "name: project-retro" in text
    assert "repo-retro report --repo" in text
    assert "command -v repo-retro" in text
    assert "repo-retro --version" in text
    assert "released nils-cli/Homebrew package" in text
    assert "Do not call an unreleased nils-cli checkout wrapper" in text
    assert "No local Python helper fallback" in text


def test_workflows_reporting_project_retro_declares_schema_and_boundaries() -> None:
    text = (skill_root() / "SKILL.md").read_text(encoding="utf-8")

    assert "cli.repo-retro.report.v1" in text
    assert "repo-retro.report.v1" in text
    assert "primary themes or through-lines" in text
    assert "Treat `result.analysis.themes` as deterministic source signals" in text
    assert "primary-theme synthesis" in text
    assert "Weekly is only a window choice, not the workflow identity" in text
    assert "Do not search `$HOME`, memory" in text
    assert "HEURISTIC_SYSTEM records as read-only" in text
    assert "heuristic-error-inbox" in text
    assert "`project-retro` consumes the released `repo-retro` CLI" in text
    assert "`daily-brief` synthesizes external source" in text


def test_workflows_reporting_project_retro_declares_history_and_typed_inputs() -> None:
    text = (skill_root() / "SKILL.md").read_text(encoding="utf-8")

    assert "timeline, learnings, validation, review, incidents, and decisions" in text
    assert "agent-out project --topic project-retro --mkdir" in text
    assert "--history-dir <dir> --write" in text
    assert "Default to stdout and no writes" in text
    assert "Do not commit generated retros" in text


def test_workflows_reporting_project_retro_is_listed_in_catalogs() -> None:
    root = repo_root()
    readme = (root / "README.md").read_text(encoding="utf-8")
    skills_readme = (root / "skills" / "README.md").read_text(encoding="utf-8")
    tooling = (root / "docs" / "runbooks" / "skills" / "TOOLING_INDEX_V2.md").read_text(encoding="utf-8")

    assert "[project-retro](./skills/workflows/reporting/project-retro/)" in readme
    assert "workflows/reporting/project-retro/" in skills_readme
    assert "repo-retro report" in tooling
    assert "weekly-retro/scripts/weekly-retro.sh" not in tooling


def test_workflows_reporting_project_retro_installed_cli_smoke() -> None:
    cli = shutil.which("repo-retro")
    if cli is None:
        pytest.skip("repo-retro is not installed on PATH")

    help_proc = subprocess.run([cli, "--help"], text=True, capture_output=True, check=True)
    assert "Generate source-grounded repository retrospectives" in help_proc.stdout

    proc = subprocess.run(
        [
            cli,
            "report",
            "--repo",
            str(repo_root()),
            "--from",
            "2099-01-01",
            "--to",
            "2099-01-02",
            "--mode",
            "team",
            "--format",
            "json",
        ],
        text=True,
        capture_output=True,
        check=True,
    )
    payload = json.loads(proc.stdout)
    assert payload["schema_version"] == "cli.repo-retro.report.v1"
    result = payload.get("result") or payload["data"]
    assert result["schema"] == "repo-retro.report.v1"
    assert result["mode"] == "team"
    assert result["history"]["write"] is False
