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

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

agents_source_root = Path(sys.argv[1])      # <repo>/agents
skills_source_root = Path(sys.argv[2])      # <repo>/skills
runtime_root = Path(sys.argv[3])            # <repo>/runtime
validation_mode = sys.argv[4]
errors = []


def check(source_path: Path, target_path: Path) -> None:
    if not target_path.is_file():
        errors.append(f"missing build-output asset: {target_path}")
        return
    if validation_mode == "match" and target_path.read_bytes() != source_path.read_bytes():
        errors.append(f"build-output asset does not match source: {target_path}")


# 1. Worker agent files: agents/workers/*-worker.md -> runtime/agents/workers/*-worker.md
#
workers_source = agents_source_root / "workers"
workers_target = runtime_root / "agents" / "workers"
if not workers_source.is_dir():
    errors.append(f"missing agents/workers source directory: {workers_source}")
else:
    for source_path in sorted(workers_source.glob("*.md")):
        check(source_path, workers_target / source_path.name)

# 2. Lead contract + internal lead resources: prompts/lead/* + prompts/coding-preflight/*
#    -> runtime/prompts/*. These ship as runtime resources (okstra install copies
#    runtime/prompts -> ~/.okstra/prompts), no longer as agent skills.
prompts_source_root = agents_source_root.parent / "prompts"
for sub in ("lead", "coding-preflight"):
    sub_root = prompts_source_root / sub
    if not sub_root.is_dir():
        errors.append(f"missing prompts source directory: {sub_root}")
        continue
    for source_path in sorted(sub_root.rglob("*.md")):
        relative = source_path.relative_to(prompts_source_root)
        check(source_path, runtime_root / "prompts" / relative)

# 3. Skill packages: skills/<name>/SKILL.md -> runtime/skills/<name>/SKILL.md
if not skills_source_root.is_dir():
    errors.append(f"missing skills source directory: {skills_source_root}")
else:
    for skill_dir in sorted(p for p in skills_source_root.iterdir() if p.is_dir()):
        for source_path in sorted(skill_dir.rglob("*.md")):
            relative = source_path.relative_to(skills_source_root)
            check(source_path, runtime_root / "skills" / relative)

if errors:
    for error in errors:
        print(error, file=sys.stderr)
    sys.exit(1)
