"""Tests for CLI commands - help text and basic invocations."""

import pytest
from click.testing import CliRunner

from ai_org.cli import cli


@pytest.fixture
def runner():
    return CliRunner()


@pytest.mark.unit
class TestCommandHelp:
    """Every command group and subcommand should show help without error."""

    @pytest.mark.parametrize(
        "args",
        [
            ["--help"],
            ["account", "--help"],
            ["account", "list", "--help"],
            ["account", "create", "--help"],
            ["billing", "--help"],
            ["billing", "setup", "--help"],
            ["billing", "link", "--help"],
            ["config", "--help"],
            ["config", "init", "--help"],
            ["config", "show", "--help"],
            ["config", "set", "--help"],
            ["dns", "--help"],
            ["dns", "delegate", "--help"],
            ["ou", "--help"],
            ["ou", "list", "--help"],
            ["ou", "get", "--help"],
            ["sso", "--help"],
            ["sso", "assign", "--help"],
            ["sso", "list", "--help"],
            ["stackset", "--help"],
            ["stackset", "status", "--help"],
            ["stackset", "list", "--help"],
        ],
    )
    def test_help_exits_zero(self, runner, args):
        result = runner.invoke(cli, args)
        assert result.exit_code == 0, f"Failed for {args}: {result.output}"

    @pytest.mark.parametrize(
        "group",
        ["account", "billing", "config", "dns", "ou", "sso", "stackset"],
    )
    def test_group_help_contains_commands(self, runner, group):
        result = runner.invoke(cli, [group, "--help"])
        assert result.exit_code == 0
        assert "Commands:" in result.output or "Usage:" in result.output


@pytest.mark.unit
class TestConfigCommands:
    def test_config_show(self, runner, tmp_path):
        config_file = tmp_path / ".ai-org.env"
        config_file.write_text("AWS_PROFILE=test-profile\n")
        result = runner.invoke(
            cli,
            ["config", "show"],
            obj={
                "profile": None,
                "region": "us-east-1",
                "debug": False,
                "json": False,
                "output": _make_output(),
            },
        )
        # Should not crash -- may show defaults or file contents
        assert result.exit_code == 0

    def test_config_set(self, runner, tmp_path, monkeypatch):
        config_file = tmp_path / ".ai-org.env"
        monkeypatch.setattr(
            "ai_org.commands.config.ConfigManager", lambda: _MockConfigManager(config_file)
        )
        result = runner.invoke(
            cli,
            ["config", "set", "AWS_PROFILE", "my-profile"],
            obj={
                "profile": None,
                "region": "us-east-1",
                "debug": False,
                "json": False,
                "output": _make_output(),
            },
        )
        assert result.exit_code == 0


class _MockConfigManager:
    """Minimal ConfigManager mock for config command tests."""

    def __init__(self, path=None):
        self.config_path = path
        self._data = {}

    def load(self):
        return {"AWS_PROFILE": "default"}

    def get(self, key, default=None):
        return self._data.get(key, default)

    def set(self, key, value):
        self._data[key] = value

    def exists(self):
        return True


def _make_output():
    from ai_org.utils.output import OutputFormatter

    return OutputFormatter()
