"""Contracts for the tracked AIWG workspace deployment manifest."""

from __future__ import annotations

import json
from datetime import datetime
from pathlib import Path

import yaml


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


def _load_json(path: str) -> dict:
    return json.loads((ROOT / path).read_text(encoding="utf-8"))


def _calendar_version(value: str) -> tuple[int, int, int]:
    parts = value.split(".")
    assert len(parts) == 3
    return tuple(int(part) for part in parts)


def _assert_utc_timestamp(value: str) -> None:
    assert value.endswith("Z")
    parsed = datetime.fromisoformat(value.removesuffix("Z") + "+00:00")
    assert parsed.utcoffset() is not None


def test_aiwg_manifest_requires_current_codex_surface():
    config = _load_json(".aiwg/aiwg.config")
    installation = config["installed"]["all"]

    assert _calendar_version(installation["version"]) >= (2026, 8, 15)
    assert installation["source"] == "bundled"
    assert installation["deployedTo"]["codex"] == {
        "agents": 0,
        "commands": 50,
        "skills": 32,
        "rules": 2,
    }
    _assert_utc_timestamp(installation["installedAt"])


def test_bt6_maintainer_installation_matches_project_plugin():
    config = _load_json(".aiwg/aiwg.config")
    plugin = _load_json(".aiwg/plugins/bt6-maintainer/manifest.json")
    installation = config["installed"]["bt6-maintainer"]

    assert installation["version"] == plugin["version"]
    assert installation["source"] == "project-local"
    assert installation["localPath"] == ".aiwg/plugins/bt6-maintainer/"
    assert installation["localType"] == plugin["type"]
    assert installation["manifestVersion"] == plugin["manifestVersion"]
    assert installation["deployedTo"]["codex"] == {
        "agents": 5,
        "commands": 0,
        "skills": 7,
        "rules": 1,
    }
    assert "skills/bt6-release-validation/SKILL.md" in installation["artifactHashes"]
    assert (
        "skills/bt6-release-validation/SKILL.md"
        in installation["deployedArtifactHashes"]["codex"]
    )
    _assert_utc_timestamp(installation["installedAt"])


def test_workspace_authority_and_delivery_permissions_remain_explicit():
    config = _load_json(".aiwg/aiwg.config")

    assert config["providers"] == ["codex"]
    assert config["workspace"]["name"] == "bt6-obliteratus-maintenance"
    repositories = {repository["name"]: repository for repository in config["repos"]}
    assert repositories["obliteratus"]["allowed"] == [
        "read",
        "write",
        "commit",
        "push",
        "issue-comment",
        "service-action",
    ]
    assert repositories["bt6-aiwg-plugins"]["allowed"] == ["read"]
    assert config["delivery"] == {
        "mode": "pr-required",
        "default_branch": "main",
        "committer": {
            "name": "Joseph Magly",
            "email": "1159087+jmagly@users.noreply.github.com",
        },
        "signing": {
            "format": "openpgp",
            "key": "0117DAAA677A5BF2",
            "fingerprint": "62297562B1C7053088F405DB0117DAAA677A5BF2",
            "enforce": "commits",
        },
        "require_signed_commits": True,
        "require_ci_green": True,
        "auto_close_issues": True,
        "issue_comment_on_cycle": True,
        "force_push_policy": "never",
    }
    assert config["remotes"]["primary"] == "origin"
    assert config["remotes"]["issue_tracker"] == "origin"
    assert config["remotes"]["ci"] == "origin"


def test_delivery_profile_preserves_signed_canonical_history():
    profile = yaml.safe_load(
        (ROOT / ".aiwg" / "bt6-maintainer.yaml").read_text(encoding="utf-8"),
    )

    assert profile["delivery"]["defaultMergeMethod"] == "merge"
    assert profile["delivery"]["allowedMergeMethods"] == ["merge"]


def test_release_profile_enforces_cpu_coverage_without_collecting_hardware_gates():
    profile = yaml.safe_load(
        (ROOT / ".aiwg" / "bt6-maintainer.yaml").read_text(encoding="utf-8"),
    )
    commands = "\n".join(profile["validation"]["full"])

    assert "--cov-branch" in commands
    assert "--min-line 75" in commands
    assert "--min-branch 60" in commands
    assert "scripts/check_quality_policy.py" in commands
    assert "scripts/check_conditional_policy.py" in commands
    assert "scripts/check_test_risk_map.py" in commands
    for marker in ("gpu", "mps", "mlx", "network", "download", "remote", "operator_ui"):
        assert f"not {marker}" in commands

    assert profile["validation"]["qualityPolicy"]["fullSuiteTrigger"] == (
        "release-readiness-and-tagged-validation"
    )
    assert profile["releaseEvidence"] == {
        "hashAlgorithm": "sha256",
        "provenanceFormat": "slsa-v1",
        "attestationFormat": "in-toto",
        "signingMode": "sigstore-keyless",
        "sbomFormat": "cyclonedx",
        "artifactType": "source-zip",
        "snapshotOnce": True,
        "verifyBeforePromotion": True,
    }
