"""fixtures-01 — 셸 시나리오에서 꺼낸 단언 블록.

호출부: validators/lib/fixtures.sh:99
규칙 R1 은 실행될 소스를 문자열/heredoc 이 아니라 실파일에 두게 한다.
이 파일의 내용은 꺼내기 전과 같다 — 옮기기만 했다.
"""
from pathlib import Path
import json
import sys

project_root = Path(sys.argv[1])
task_manifest_path = project_root / sys.argv[2]
target_worker_id = sys.argv[3]
task_manifest = json.loads(task_manifest_path.read_text())
team_state_path = project_root / task_manifest["teamStatePath"]
team_state = json.loads(team_state_path.read_text())

target_worker = None
for worker in team_state.get("workers", []):
    if isinstance(worker, dict) and worker.get("workerId") == target_worker_id:
        target_worker = worker
        break

if target_worker is None:
    raise SystemExit(f"worker not found in team-state: {target_worker_id}")

prompt_relative = str(target_worker.get("promptPath", "")).strip()
if not prompt_relative:
    raise SystemExit(f"worker promptPath is missing: {target_worker_id}")

prompt_path = project_root / prompt_relative
prompt_path.parent.mkdir(parents=True, exist_ok=True)
artifacts = task_manifest.get("artifacts", {})
analysis_packet_path = (
    str(artifacts.get("analysisPacketPath", "")).strip()
    if isinstance(artifacts, dict)
    else ""
)
prompt_path.write_text(
    "\n".join(
        [
            "**Worker Error Contract Path:** /okstra/templates/worker-error-contract.md",
            f"**Errors log path:** {project_root}/run/logs/errors.jsonl",
            f"**Errors sidecar path:** {project_root}/run/worker-results/"
            f"{target_worker_id}-errors.json",
            f"Assigned worker prompt history path: {prompt_relative}",
            "**Prompt Delivery Mode:** eager-include",
            f"**Model:** Fixture worker, {target_worker.get('modelExecutionValue', '')}",
            f"- Primary analysis packet: `{analysis_packet_path}`",
            "",
            "# Initial Analysis Prompt",
            f"Task Key: {task_manifest.get('taskKey', '')}",
            "Validation fixture prompt body.",
        ]
    )
    + "\n"
)
