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

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

brief_path = Path(sys.argv[1])
reference_path = Path(sys.argv[2])
expected_task_key = sys.argv[3]
brief_lines = brief_path.read_text().splitlines()
section_map = {
    "Configuration References and Expected Values": "config",
    "Deployment Manifests and Expected Values": "deployment",
}
captured = {"config": [], "deployment": []}
current_section = None

for line in brief_lines:
    if line.startswith("## "):
        current_section = section_map.get(line[3:].strip())
        continue
    if current_section and line.strip():
        captured[current_section].append(line)

errors = []

if not reference_path.is_file():
    errors.append(f"reference expectations file is missing: {reference_path}")
    reference_content = ""
else:
    reference_content = reference_path.read_text()

for line in captured["config"] + captured["deployment"]:
    if line not in reference_content:
        errors.append(f"reference expectations file is missing brief line: {line}")

if f"- Task Key: `{expected_task_key}`" not in reference_content:
    errors.append("reference expectations file is missing the task key header")

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