"""ORCH-41: tests for resolve_agent_effort + effort validation + flag assembly.

Mirrors test_resolve_agent_model's 4-level priority for the --effort lever, and
adds:
  - validation: a value outside {low,medium,high,xhigh,max} is dropped -> ""
  - flag assembly: --model / --effort / --fallback-model are present/absent in
    the built command exactly when the resolved value is non-empty.
"""
import os
import tempfile

import pytest

os.environ.setdefault("ORCH_DB_PATH",
                      os.path.join(tempfile.gettempdir(), "test_orch41_effort.db"))
os.environ.setdefault("ORCH_GITEA_TOKEN", "test-token")
os.environ.setdefault("ORCH_PLANE_API_TOKEN", "test-token")

from src.agents.launcher import (
    resolve_agent_effort, resolve_agent_model, VALID_EFFORTS,
)
from src.config import settings
from src import projects as P
from src.projects import ProjectConfig, reload_projects

ORCH_PLANE_ID = "8da6aa25-a60e-44d6-a1e2-d8ae59aa7d6a"


@pytest.fixture(autouse=True)
def _clean_settings(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_default", "high")
    for a in ("analyst", "architect", "developer", "reviewer"):
        monkeypatch.setattr(settings, f"agent_effort_{a}", "high")
    for a in ("tester", "deployer"):
        monkeypatch.setattr(settings, f"agent_effort_{a}", "medium")
    monkeypatch.setattr(P.settings, "projects_json", "")
    reload_projects()
    yield
    reload_projects()


def _install_registry(monkeypatch, agent_efforts):
    reg = [ProjectConfig(
        plane_project_id=ORCH_PLANE_ID, repo="orchestrator",
        work_item_prefix="ORCH", name="orchestrator",
        agent_efforts=agent_efforts,
    )]
    monkeypatch.setattr(P, "PROJECTS", reg)
    monkeypatch.setattr(P, "_BY_PLANE_ID", {p.plane_project_id: p for p in reg})
    monkeypatch.setattr(P, "_BY_REPO", {p.repo: p for p in reg})


# ---- default split ----------------------------------------------------------
def test_default_split():
    assert resolve_agent_effort("developer") == "high"
    assert resolve_agent_effort("architect") == "high"
    assert resolve_agent_effort("tester") == "medium"
    assert resolve_agent_effort("deployer") == "medium"


# ---- level 4: nothing -> "" -------------------------------------------------
def test_no_config_returns_empty(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_default", "")
    monkeypatch.setattr(settings, "agent_effort_tester", "")
    assert resolve_agent_effort("tester") == ""


# ---- level 2: per-agent env beats default -----------------------------------
def test_per_agent_env(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_tester", "low")
    assert resolve_agent_effort("tester") == "low"


# ---- level 1: project override wins -----------------------------------------
def test_project_override(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_developer", "high")
    _install_registry(monkeypatch, {"developer": "xhigh"})
    assert resolve_agent_effort("developer", ORCH_PLANE_ID) == "xhigh"
    assert resolve_agent_effort("developer") == "high"


# ---- validation: invalid value dropped --------------------------------------
def test_invalid_default_dropped(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_developer", "")
    monkeypatch.setattr(settings, "agent_effort_default", "turbo")
    assert resolve_agent_effort("developer") == ""


def test_invalid_env_dropped(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_reviewer", "ultra")
    assert resolve_agent_effort("reviewer") == ""


def test_invalid_project_override_dropped(monkeypatch):
    _install_registry(monkeypatch, {"developer": "bogus"})
    assert resolve_agent_effort("developer", ORCH_PLANE_ID) == ""


def test_all_valid_efforts_pass(monkeypatch):
    monkeypatch.setattr(settings, "agent_effort_developer", "")
    for e in VALID_EFFORTS:
        monkeypatch.setattr(settings, "agent_effort_default", e)
        assert resolve_agent_effort("developer") == e


# ---- flag assembly (mirror of launcher cmd construction) --------------------
def _build_flags(model, effort, fb):
    model_flag = f"--model {model} " if model else ""
    effort_flag = f"--effort {effort} " if effort else ""
    fb_flag = f"--fallback-model {fb} " if fb else ""
    return f"{model_flag}{effort_flag}{fb_flag}"


def test_flags_present_when_configured(monkeypatch):
    monkeypatch.setattr(settings, "agent_fallback_model", "claude-sonnet-4-6")
    model = resolve_agent_model("developer")
    effort = resolve_agent_effort("developer")
    fb = settings.agent_fallback_model
    flags = _build_flags(model, effort, fb)
    assert "--model claude-opus-4-8 " in flags
    assert "--effort high " in flags
    assert "--fallback-model claude-sonnet-4-6 " in flags


def test_flags_absent_when_empty(monkeypatch):
    monkeypatch.setattr(settings, "agent_model_default", "")
    monkeypatch.setattr(settings, "agent_model_developer", "")
    monkeypatch.setattr(settings, "agent_effort_default", "")
    monkeypatch.setattr(settings, "agent_effort_developer", "")
    monkeypatch.setattr(settings, "agent_fallback_model", "")
    model = resolve_agent_model("developer")
    effort = resolve_agent_effort("developer")
    fb = settings.agent_fallback_model
    flags = _build_flags(model, effort, fb)
    assert flags == ""
    assert "--model" not in flags
    assert "--effort" not in flags
    assert "--fallback-model" not in flags
