"""Tests for quber.prompts.loader module."""

import sys
from pathlib import Path

import pytest

if sys.version_info >= (3, 11):
    pass
else:
    pass

from quber.prompts.loader import load_prompt


def test_load_prompt_system_key():
    """Test loading system prompt from table_inference.toml."""
    prompt = load_prompt("table_inference", "system")

    assert isinstance(prompt, str)
    assert len(prompt) > 0
    assert "analyzing tables" in prompt.lower()


def test_load_prompt_returns_non_empty_string():
    """Test that loaded prompt is not empty."""
    prompt = load_prompt("table_inference", "system")

    assert prompt.strip()
    assert len(prompt.strip()) > 50


def test_load_prompt_file_not_found():
    """Test that FileNotFoundError is raised for non-existent file."""
    with pytest.raises(FileNotFoundError) as exc_info:
        load_prompt("nonexistent_processor", "system")

    assert "Prompt file not found" in str(exc_info.value)


def test_load_prompt_key_not_found():
    """Test that KeyError is raised for non-existent key."""
    with pytest.raises(KeyError) as exc_info:
        load_prompt("table_inference", "nonexistent_key")

    assert "Prompt key 'nonexistent_key' not found" in str(exc_info.value)
    assert "Available keys:" in str(exc_info.value)


def test_load_prompt_custom_prompts_dir(tmp_path: Path):
    """Test loading prompt from custom directory."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        """
[prompts.system]
text = "Test prompt content"
"""
    )

    prompt = load_prompt("test_processor", "system", prompts_dir=custom_dir)

    assert prompt == "Test prompt content"


def test_load_prompt_strips_whitespace(tmp_path: Path):
    """Test that loaded prompt has whitespace stripped."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        '''
[prompts.test]
text = """
    Content with whitespace
    """
'''
    )

    prompt = load_prompt("test_processor", "test", prompts_dir=custom_dir)

    assert "Content with whitespace" in prompt


def test_load_prompt_empty_content_raises_error(tmp_path: Path):
    """Test that ValueError is raised for empty prompt content."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        """
[prompts.empty]
text = "   "
"""
    )

    with pytest.raises(ValueError) as exc_info:
        load_prompt("test_processor", "empty", prompts_dir=custom_dir)

    assert "Prompt content is empty" in str(exc_info.value)


def test_load_prompt_multiple_keys(tmp_path: Path):
    """Test loading different keys from same file."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        """
[prompts.system]
text = "System prompt"

[prompts.user]
text = "User prompt"
"""
    )

    system_prompt = load_prompt("test_processor", "system", prompts_dir=custom_dir)
    user_prompt = load_prompt("test_processor", "user", prompts_dir=custom_dir)

    assert system_prompt == "System prompt"
    assert user_prompt == "User prompt"


def test_load_prompt_default_prompts_dir():
    """Test that default prompts_dir is package directory."""
    prompt = load_prompt("table_inference", "system")

    assert isinstance(prompt, str)
    assert len(prompt) > 0


def test_load_prompt_invalid_toml_structure(tmp_path: Path):
    """Test handling of invalid TOML structure."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        """
[invalid_section]
text = "Invalid structure"
"""
    )

    with pytest.raises(KeyError):
        load_prompt("test_processor", "system", prompts_dir=custom_dir)


def test_load_prompt_missing_text_field(tmp_path: Path):
    """Test handling of missing 'text' field in prompt."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        """
[prompts.system]
description = "Missing text field"
"""
    )

    with pytest.raises(KeyError):
        load_prompt("test_processor", "system", prompts_dir=custom_dir)


def test_load_prompt_multiline_content(tmp_path: Path):
    """Test loading multiline prompt content."""
    custom_dir = tmp_path / "prompts"
    custom_dir.mkdir()

    test_toml = custom_dir / "test_processor.toml"
    test_toml.write_text(
        '''
[prompts.multiline]
text = """
Line 1
Line 2
Line 3
"""
'''
    )

    prompt = load_prompt("test_processor", "multiline", prompts_dir=custom_dir)

    assert "Line 1" in prompt
    assert "Line 2" in prompt
    assert "Line 3" in prompt
