"""Tests for D4 — integration into existing commands (≥8 tests)."""
from __future__ import annotations

from pathlib import Path

import pytest
from typer.testing import CliRunner

from agentkit_cli.main import app
from agentkit_cli.doctor import check_context_sync, run_doctor
from agentkit_cli.migrate import MigrateEngine, FORMAT_AGENTS_MD, FORMAT_CLAUDE_MD, FORMAT_LLMSTXT, _make_gen_stamp

runner = CliRunner()
engine = MigrateEngine()

AGENTS_SAMPLE = """\
# My Project

## Session Startup
Always read this.

## Safety
Keep private.
"""


class TestDoctorContextSyncCheck:
    def test_check_returns_pass_no_files(self, tmp_path):
        result = check_context_sync(tmp_path)
        assert result.id == "context.sync"
        assert result.status == "pass"

    def test_check_returns_pass_when_in_sync(self, tmp_path):
        (tmp_path / "AGENTS.md").write_text(AGENTS_SAMPLE)
        r_claude = engine.convert(AGENTS_SAMPLE, FORMAT_AGENTS_MD, FORMAT_CLAUDE_MD)
        (tmp_path / "CLAUDE.md").write_text(r_claude.content)
        r_llms = engine.convert(AGENTS_SAMPLE, FORMAT_AGENTS_MD, FORMAT_LLMSTXT)
        (tmp_path / "llms.txt").write_text(r_llms.content)
        result = check_context_sync(tmp_path)
        assert result.status == "pass"

    def test_check_returns_warn_when_stale(self, tmp_path):
        (tmp_path / "AGENTS.md").write_text(AGENTS_SAMPLE)
        r_claude = engine.convert(AGENTS_SAMPLE, FORMAT_AGENTS_MD, FORMAT_CLAUDE_MD)
        (tmp_path / "CLAUDE.md").write_text(r_claude.content)
        # Modify source → stale
        (tmp_path / "AGENTS.md").write_text(AGENTS_SAMPLE + "\n## New Section\nContent.\n")
        result = check_context_sync(tmp_path)
        assert result.status == "warn"
        assert "context.sync" == result.id

    def test_check_appears_in_doctor_report(self, tmp_path):
        report = run_doctor(tmp_path)
        ids = [c.id for c in report.checks]
        assert "context.sync" in ids


class TestLlmstxtSyncFrom:
    def test_sync_from_agents_md(self, tmp_path):
        (tmp_path / "AGENTS.md").write_text(AGENTS_SAMPLE)
        result = runner.invoke(app, [
            "llmstxt", str(tmp_path), "--sync-from", "agents-md"
        ])
        assert result.exit_code == 0
        assert (tmp_path / "llms.txt").exists()
        content = (tmp_path / "llms.txt").read_text()
        assert "generated by agentkit migrate" in content

    def test_sync_from_claude_md(self, tmp_path):
        (tmp_path / "CLAUDE.md").write_text("# Project\n\n## Overview\nA tool.\n")
        result = runner.invoke(app, [
            "llmstxt", str(tmp_path), "--sync-from", "claude-md"
        ])
        assert result.exit_code == 0
        assert (tmp_path / "llms.txt").exists()

    def test_sync_from_missing_source_exits_1(self, tmp_path):
        result = runner.invoke(app, [
            "llmstxt", str(tmp_path), "--sync-from", "agents-md"
        ])
        assert result.exit_code == 1

    def test_sync_from_invalid_format_exits_1(self, tmp_path):
        (tmp_path / "AGENTS.md").write_text(AGENTS_SAMPLE)
        result = runner.invoke(app, [
            "llmstxt", str(tmp_path), "--sync-from", "unknown-format"
        ])
        assert result.exit_code == 1
