from dobby.core.prompt_builder import PromptBuilder
from dobby.models import InfraConfig, Project, Task


def test_basic_prompt():
    task = Task(project="spare", description="Add warmup scheduler", exit_criteria="PR ready")
    project = Project(name="spare", repo="/path/to/spare", context="RL framework")
    prompt = PromptBuilder.build(task, project)
    assert "spare" in prompt
    assert "Add warmup scheduler" in prompt
    assert "PR ready" in prompt
    assert "DOBBY_RESULT" in prompt


def test_prompt_with_infra():
    task = Task(project="spare", description="Run training", exit_criteria="Launch and confirm")
    project = Project(
        name="spare", repo="/path",
        infra=[InfraConfig(name="gpu-1", type="ssh", host="user@gpu", conda_env="/env/spare")],
    )
    prompt = PromptBuilder.build(task, project, infra_name="gpu-1")
    assert "user@gpu" in prompt
    assert "/env/spare" in prompt
    assert "nohup" in prompt.lower() or "background" in prompt.lower()


def test_prompt_with_plan():
    task = Task(
        project="spare",
        description="Refactor module",
        plan=["Step 1: rename file", "Step 2: update imports", "Step 3: add tests"],
    )
    project = Project(name="spare", repo="/path")
    prompt = PromptBuilder.build(task, project)
    assert "Step 1" in prompt
    assert "Step 3" in prompt


def test_prompt_with_checkpoint():
    task = Task(project="spare", description="Risky refactor", checkpoint=True)
    project = Project(name="spare", repo="/path")
    prompt = PromptBuilder.build(task, project)
    assert (
        "checkpoint" in prompt.lower()
        or "approval" in prompt.lower()
        or "confirm" in prompt.lower()
    )


def test_prompt_with_user_preferences():
    task = Task(project="personal", description="Check unreplied emails")
    project = Project(name="personal", repo="/path")
    prefs = [
        {"key": "pref:email-reply-policy", "value": "Never auto-reply to emails"},
        {"key": "pref:priority-contacts", "value": "advisor@example.com"},
    ]
    prompt = PromptBuilder.build(task, project, user_preferences=prefs)
    assert "User Preferences" in prompt
    assert "Never auto-reply" in prompt
    assert "email-reply-policy" in prompt
    assert "priority-contacts" in prompt


def test_prompt_with_mcp_services():
    task = Task(project="personal", description="Check calendar")
    project = Project(name="personal", repo="/path")
    prompt = PromptBuilder.build(task, project)
    # Default MCP services are always injected
    assert "Available External Services" in prompt
    assert "slack" in prompt.lower()
    assert "gmail" in prompt.lower()


def test_prompt_with_custom_mcp_services():
    task = Task(project="personal", description="Check calendar")
    project = Project(name="personal", repo="/path")
    custom = [("custom-tool", "A custom tool for testing")]
    prompt = PromptBuilder.build(task, project, mcp_services=custom)
    assert "custom-tool" in prompt
    assert "gmail" not in prompt.lower()


def test_prompt_no_preferences():
    task = Task(project="spare", description="Fix a bug")
    project = Project(name="spare", repo="/path")
    prompt = PromptBuilder.build(task, project, user_preferences=None)
    assert "User Preferences" not in prompt


def test_gpu_phase2_parse_prompt():
    prompt = PromptBuilder.build_gpu_parse_prompt(
        log_content="Epoch 3/3 loss=0.342 acc=0.871",
        task_description="Run SPARE training 3 epochs",
    )
    assert "0.342" in prompt
    assert "DOBBY_RESULT" in prompt
