#!/usr/bin/env python3
"""Validate the zero-touch-orchestrator Agent Skill without dependencies."""

from __future__ import annotations

import json
from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parents[1]

REQUIRED = [
    "SKILL.md",
    "SOURCE.md",
    "agents/openai.yaml",
    "references/agent-contract.md",
    "references/operating-model.md",
    "references/risk-policy.md",
    "references/task-intent.schema.json",
    "references/execution-report.schema.json",
    "evals/evals.json",
]


def fail(message: str) -> None:
    print(f"FAIL: {message}")
    raise SystemExit(1)


def main() -> int:
    missing = [path for path in REQUIRED if not (ROOT / path).is_file()]
    if missing:
        fail("missing files: " + ", ".join(missing))

    skill = (ROOT / "SKILL.md").read_text(encoding="utf-8")
    if not skill.startswith("---\n"):
        fail("SKILL.md lacks frontmatter")
    frontmatter_end = skill.find("\n---\n", 4)
    if frontmatter_end < 0:
        fail("SKILL.md frontmatter is unterminated")
    frontmatter = skill[4:frontmatter_end]
    for key in ("name:", "description:"):
        if key not in frontmatter:
            fail(f"SKILL.md frontmatter lacks {key[:-1]}")
    if "name: zero-touch-orchestrator" not in frontmatter:
        fail("unexpected skill name")
    if len(skill.splitlines()) > 500:
        fail("SKILL.md exceeds the recommended 500 lines")

    for schema_name in (
        "references/task-intent.schema.json",
        "references/execution-report.schema.json",
    ):
        schema = json.loads((ROOT / schema_name).read_text(encoding="utf-8"))
        if schema.get("type") != "object" or "$schema" not in schema:
            fail(f"invalid JSON Schema: {schema_name}")

    evals = json.loads((ROOT / "evals/evals.json").read_text(encoding="utf-8"))
    if evals.get("skill_name") != "zero-touch-orchestrator":
        fail("eval skill_name mismatch")
    cases = evals.get("evals")
    if not isinstance(cases, list) or len(cases) < 3:
        fail("at least three eval cases are required")
    for case in cases:
        for key in ("id", "prompt", "expected_output", "files"):
            if key not in case:
                fail(f"eval case lacks {key}")

    agent = (ROOT / "agents/openai.yaml").read_text(encoding="utf-8")
    for key in ("display_name:", "short_description:", "default_prompt:"):
        if key not in agent:
            fail(f"agents/openai.yaml lacks {key[:-1]}")

    print(f"PASS: {len(REQUIRED) + 1} required files validated")
    print(f"PASS: {len(cases)} eval cases validated")
    return 0


if __name__ == "__main__":
    sys.exit(main())
