"""Prompt loader module for language-specific optimization prompts.

This module provides a unified interface to load prompts based on language.
"""

from __future__ import annotations

from pathlib import Path

PROMPTS_DIR = Path(__file__).parent


def get_system_prompt(language: str, is_async: bool = False) -> str:
    """Load the system prompt for the given language.

    Args:
        language: The programming language (python, javascript, typescript)
        is_async: Whether to load the async variant of the prompt

    Returns:
        The system prompt text

    Raises:
        ValueError: If no prompt exists for the language

    """
    # Normalize language - typescript uses javascript prompts
    prompt_language = "javascript" if language == "typescript" else language

    variant = "async_system_prompt.md" if is_async else "system_prompt.md"
    prompt_file = PROMPTS_DIR / prompt_language / variant

    if not prompt_file.exists():
        raise ValueError(f"No system prompt found for language: {language}")

    return prompt_file.read_text()


def get_user_prompt(language: str, is_async: bool = False) -> str:
    """Load the user prompt for the given language.

    Args:
        language: The programming language (python, javascript, typescript)
        is_async: Whether to load the async variant of the prompt

    Returns:
        The user prompt text

    Raises:
        ValueError: If no prompt exists for the language

    """
    # Normalize language - typescript uses javascript prompts
    prompt_language = "javascript" if language == "typescript" else language

    variant = "async_user_prompt.md" if is_async else "user_prompt.md"
    prompt_file = PROMPTS_DIR / prompt_language / variant

    if not prompt_file.exists():
        raise ValueError(f"No user prompt found for language: {language}")

    return prompt_file.read_text()


def get_available_languages() -> list[str]:
    """Get a list of languages that have prompts available.

    Returns:
        List of language names with available prompts

    """
    languages = []
    for item in PROMPTS_DIR.iterdir():
        if item.is_dir() and (item / "system_prompt.md").exists():
            languages.append(item.name)
    return sorted(languages)
