"""Regression coverage for issue #1820: no-agent cron edits do not require prompts."""

from __future__ import annotations

from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
PANELS_JS = (ROOT / "static" / "panels.js").read_text()


def _function_body(name: str) -> str:
    marker = f"function {name}("
    start = PANELS_JS.find(marker)
    assert start != -1, f"{name} not found"
    paren = PANELS_JS.find("(", start)
    assert paren != -1, f"{name} params not found"
    depth = 0
    for idx in range(paren, len(PANELS_JS)):
        ch = PANELS_JS[idx]
        if ch == "(":
            depth += 1
        elif ch == ")":
            depth -= 1
            if depth == 0:
                brace = PANELS_JS.find("{", idx)
                break
    else:
        raise AssertionError(f"{name} params did not terminate")
    assert brace != -1, f"{name} body not found"
    depth = 0
    for idx in range(brace, len(PANELS_JS)):
        ch = PANELS_JS[idx]
        if ch == "{":
            depth += 1
        elif ch == "}":
            depth -= 1
            if depth == 0:
                return PANELS_JS[brace + 1 : idx]
    raise AssertionError(f"{name} body did not terminate")


def test_open_cron_edit_plumbs_no_agent_and_script_to_form():
    body = _function_body("openCronEdit")
    assert "no_agent: !!job.no_agent" in body
    assert "script: job.script || ''" in body


def test_no_agent_form_hides_prompt_and_shows_readonly_script_path():
    body = _function_body("_renderCronForm")
    assert "no_agent" in body and "script" in body
    assert "const isNoAgent = !!no_agent;" in body
    assert "const promptBlock = isNoAgent ? '' :" in body
    assert 'id="cronFormScript"' in body
    assert "readonly autocomplete=\"off\"" in body
    assert "cron_script_path_hint" in body
    assert "const skillsBlock = isNoAgent ? '' :" in body
    assert "if (!isNoAgent) _renderCronSkillTags();" in body


def test_save_cron_form_keeps_agent_prompt_required_but_skips_no_agent_edits():
    body = _function_body("saveCronForm")
    assert "const isNoAgent = !!(_cronPreFormDetail && _cronPreFormDetail.no_agent);" in body
    assert "if(!isNoAgent && !prompt)" in body
    assert "cron_prompt_required" in body
    assert "if (!isNoAgent) updates.prompt = prompt;" in body


def test_no_agent_detail_displays_mode_and_script():
    body = _function_body("_renderCronDetail")
    assert "const isNoAgent = _isCronScriptJob(job);" in body
    assert "isNoAgent ? _cronScriptCardHtml(job) : _cronAgentPromptCardHtml(job)" in body
    assert "isNoAgent ? _cronScriptJobBannerHtml() : ''" in body
    assert "cronJobMode" in body
    assert "cron-mode-badge ${isNoAgent ? 'script' : 'agent'}" in body
