"""Tests for init and decompose UX improvements (Stream B)."""

import json
from pathlib import Path
from unittest.mock import patch

from click.testing import CliRunner

from loom.cli import cli


def test_decompose_default_scan_root(tmp_path):
    """Decompose defaults --scan-root to current directory."""
    # We can't run decompose without full infra, but we can verify the CLI parameter defaults
    runner = CliRunner()
    # Use --help to verify the option text
    result = runner.invoke(cli, ["decompose", "--help"])
    assert result.exit_code == 0
    assert "default: current directory" in result.output


def test_decompose_no_scan_flag(tmp_path):
    """--no-scan flag is available."""
    runner = CliRunner()
    result = runner.invoke(cli, ["decompose", "--help"])
    assert result.exit_code == 0
    assert "--no-scan" in result.output


def test_agents_md_has_multi_agent_framing(tmp_path, monkeypatch):
    """AGENTS.md template includes multi-agent workflow explanation."""
    monkeypatch.chdir(tmp_path)
    runner = CliRunner()
    result = runner.invoke(cli, ["init", "--name", "test-project"])
    assert result.exit_code == 0

    agents_md = (tmp_path / "AGENTS.md").read_text()
    assert "multi-agent orchestration" in agents_md.lower()
    assert "decompose" in agents_md.lower()
    assert "claim" in agents_md.lower()
    assert "Quick Start" in agents_md
    assert "Worker Agent" in agents_md or "worker" in agents_md.lower()


def test_claude_md_has_orchestration_framing(tmp_path, monkeypatch):
    """CLAUDE.md snippet includes orchestration framing paragraph."""
    monkeypatch.chdir(tmp_path)
    runner = CliRunner()
    result = runner.invoke(cli, ["init", "--name", "test-project"])
    assert result.exit_code == 0

    claude_md = (tmp_path / "CLAUDE.md").read_text()
    assert "multi-agent orchestration" in claude_md.lower()
    assert "task graph" in claude_md.lower()


def test_agents_md_has_doctor_command(tmp_path, monkeypatch):
    """AGENTS.md CLI table includes loom doctor."""
    monkeypatch.chdir(tmp_path)
    runner = CliRunner()
    result = runner.invoke(cli, ["init", "--name", "test-project"])
    assert result.exit_code == 0

    agents_md = (tmp_path / "AGENTS.md").read_text()
    assert "loom doctor" in agents_md
