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

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

project_root = Path(sys.argv[1])
expected_task_key = sys.argv[2]
task_manifest_relative_path = sys.argv[3]
expected_reference_relative_path = sys.argv[4]
expected_task_catalog_relative_path = sys.argv[5]
errors = []

task_manifest_path = project_root / task_manifest_relative_path
if not task_manifest_path.is_file():
    errors.append(f"task manifest is missing: {task_manifest_path}")
else:
    task_manifest = json.loads(task_manifest_path.read_text())

    if task_manifest.get("taskKey") != expected_task_key:
        errors.append("task manifest does not contain the expected task key")

    if task_manifest.get("referenceExpectationsPath") != expected_reference_relative_path:
        errors.append("task manifest does not expose referenceExpectationsPath")

    if task_manifest.get("taskCatalogPath") != expected_task_catalog_relative_path:
        errors.append("task manifest does not expose taskCatalogPath")

    if (
        task_manifest.get("artifacts", {}).get("referenceExpectationsPath")
        != expected_reference_relative_path
    ):
        errors.append("task manifest artifacts do not expose referenceExpectationsPath")

    timeline_relative_path = task_manifest.get("historyTimelinePath", "")
    if not timeline_relative_path:
        errors.append("task manifest is missing historyTimelinePath")
    else:
        timeline_path = project_root / timeline_relative_path
        if not timeline_path.is_file():
            errors.append(f"timeline file is missing: {timeline_path}")
        else:
            timeline = json.loads(timeline_path.read_text())
            runs = timeline.get("runs", [])
            latest_run = None
            if isinstance(runs, list):
                for item in reversed(runs):
                    if isinstance(item, dict):
                        latest_run = item
                        break
            if latest_run is None:
                errors.append("timeline does not contain a latest run entry")
            else:
                run_manifest_relative_path = latest_run.get("runManifestPath", "")
                if not run_manifest_relative_path:
                    errors.append("latest timeline entry is missing runManifestPath")
                else:
                    run_manifest_path = project_root / run_manifest_relative_path
                    if not run_manifest_path.is_file():
                        errors.append(f"latest run manifest is missing: {run_manifest_path}")
                    else:
                        run_manifest = json.loads(run_manifest_path.read_text())
                        if run_manifest.get("taskKey") != expected_task_key:
                            errors.append("run manifest does not contain the expected task key")
                        if (
                            run_manifest.get("referenceExpectationsPath")
                            != expected_reference_relative_path
                        ):
                            errors.append(
                                "run manifest does not expose referenceExpectationsPath"
                            )
                        if (
                            run_manifest.get("taskCatalogPath")
                            != expected_task_catalog_relative_path
                        ):
                            errors.append("run manifest does not expose taskCatalogPath")

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