"""Tests for type_generator module."""

from __future__ import annotations

from pathlib import Path

import pytest

from prompteer.type_generator import (
    TypeStubGenerator,
    get_default_value,
    get_python_type,
)


class TestGetPythonType:
    """Tests for get_python_type function."""

    def test_string_type(self) -> None:
        """Test string type mapping."""
        assert get_python_type("str") == "str"

    def test_int_type(self) -> None:
        """Test int type mapping."""
        assert get_python_type("int") == "int"

    def test_float_type(self) -> None:
        """Test float type mapping."""
        assert get_python_type("float") == "float"

    def test_bool_type(self) -> None:
        """Test bool type mapping."""
        assert get_python_type("bool") == "bool"

    def test_number_type(self) -> None:
        """Test number type mapping to Union."""
        assert get_python_type("number") == "Union[int, float]"

    def test_any_type(self) -> None:
        """Test any type mapping."""
        assert get_python_type("any") == "Any"

    def test_unknown_type(self) -> None:
        """Test unknown type defaults to Any."""
        assert get_python_type("unknown") == "Any"


class TestGetDefaultValue:
    """Tests for get_default_value function."""

    def test_string_default(self) -> None:
        """Test string default value."""
        assert get_default_value("str") == '""'

    def test_int_default(self) -> None:
        """Test int default value."""
        assert get_default_value("int") == "0"

    def test_float_default(self) -> None:
        """Test float default value."""
        assert get_default_value("float") == "0.0"

    def test_bool_default(self) -> None:
        """Test bool default value."""
        assert get_default_value("bool") == "False"


class TestTypeStubGenerator:
    """Tests for TypeStubGenerator class."""

    def test_scan_directory(self) -> None:
        """Test directory scanning."""
        generator = TypeStubGenerator(Path("tests/fixtures/prompts"))
        structure = generator.scan_directory(Path("tests/fixtures/prompts"))

        assert "simple" in structure
        assert "with-variables" in structure
        assert "with-metadata" in structure
        assert "nested" in structure

    def test_generate_type_stub(self, tmp_path: Path) -> None:
        """Test type stub generation."""
        generator = TypeStubGenerator(Path("tests/fixtures/prompts"))
        output_path = tmp_path / "test.pyi"

        generator.generate_type_stub(output_path)

        assert output_path.exists()
        content = output_path.read_text()

        # Check header
        assert "Auto-generated type stubs" in content
        assert "DO NOT EDIT THIS FILE MANUALLY" in content

        # Check imports
        assert "from typing import Any" in content

        # Check main class
        assert "class Prompteer:" in content

    def test_generated_stub_has_correct_types(self, tmp_path: Path) -> None:
        """Test that generated stub has correct type annotations."""
        generator = TypeStubGenerator(Path("tests/fixtures/prompts"))
        output_path = tmp_path / "test.pyi"

        generator.generate_type_stub(output_path)
        content = output_path.read_text()

        # Should have methods for prompt files
        assert "def simple" in content or "def simple(" in content
        assert "def withVariables" in content or "def withVariables(" in content

        # Metadata prompt should have typed parameters
        if "def withMetadata" in content:
            # Check for type annotations
            assert "int" in content or "float" in content
