"""Tests for Agent config loader."""

from __future__ import annotations

import json
from pathlib import Path

import pytest
import yaml

from src.config_loader import load_agent_config, load_agents
from src.models import AgentConfig


@pytest.fixture()
def agents_dir(tmp_path: Path) -> Path:
    """Create a temporary agents directory with test configs."""
    agent_data = {
        "name": "test_architect",
        "role": "Software Architect",
        "system_prompt": "You are an architect.",
    }
    json_path = tmp_path / "test_architect.json"
    json_path.write_text(json.dumps(agent_data), encoding="utf-8")

    yaml_data = {
        "name": "test_devops",
        "role": "DevOps Engineer",
        "system_prompt": "You are a DevOps engineer.",
    }
    yaml_path = tmp_path / "test_devops.yaml"
    yaml_path.write_text(yaml.dump(yaml_data), encoding="utf-8")

    return tmp_path


class TestLoadAgentConfig:
    """Tests for load_agent_config function."""

    def test_load_json_config(self, agents_dir: Path) -> None:
        config = load_agent_config(agents_dir / "test_architect.json")
        assert config.name == "test_architect"
        assert config.role == "Software Architect"

    def test_load_yaml_config(self, agents_dir: Path) -> None:
        config = load_agent_config(agents_dir / "test_devops.yaml")
        assert config.name == "test_devops"
        assert config.role == "DevOps Engineer"

    def test_load_nonexistent_file_raises(self, tmp_path: Path) -> None:
        with pytest.raises(FileNotFoundError):
            load_agent_config(tmp_path / "nonexistent.json")

    def test_load_unsupported_extension_raises(self, tmp_path: Path) -> None:
        bad_file = tmp_path / "agent.txt"
        bad_file.write_text("{}", encoding="utf-8")
        with pytest.raises(ValueError, match="Unsupported file format"):
            load_agent_config(bad_file)


class TestLoadAgents:
    """Tests for load_agents function."""

    def test_load_multiple_agents(self, agents_dir: Path) -> None:
        agents = load_agents(["test_architect", "test_devops"], agents_dir=agents_dir)
        assert "test_architect" in agents
        assert "test_devops" in agents
        assert isinstance(agents["test_architect"], AgentConfig)

    def test_load_missing_agent_raises(self, agents_dir: Path) -> None:
        with pytest.raises(FileNotFoundError, match="not_here"):
            load_agents(["not_here"], agents_dir=agents_dir)
