#!/usr/bin/env python3
"""Verify CARBON's public harness category without renaming its framework layer."""

from __future__ import annotations

import json
import sys
from pathlib import Path


PLUGIN = Path(__file__).resolve().parents[1]
VERSION = "1.32.20"
PRIMARY = "first AI Agentic Verification Harness"


def require(condition: bool, message: str) -> None:
    if not condition:
        raise AssertionError(message)


def read(path: Path) -> str:
    require(path.is_file(), f"missing required file: {path}")
    return path.read_text(encoding="utf-8")


def main() -> None:
    readme = read(PLUGIN / "README.md")
    skill = read(PLUGIN / "skills" / "carbon" / "SKILL.md")
    server = read(PLUGIN / "server" / "index.js")
    commands = read(PLUGIN / "COMMANDS.md")

    require(PRIMARY in readme, "README is missing the primary harness definition")
    require("## Why CARBON is a harness" in readme, "README is missing the why-harness explanation")
    require("specialized software-testing harness" in readme, "README does not bound CARBON relative to the host agent runtime")
    require("first AI Agentic Verification Harness" in skill, "main CARBON skill is missing the harness identity")
    require("first AI Agentic Verification Harness" in server, "MCP help still lacks harness positioning")
    require("first AI Agentic\nVerification Harness" in commands, "command reference is missing the harness route description")
    require("## Why it is agentic" in readme, "README is missing the agentic explanation")

    manifests = [
        PLUGIN / "package.json",
        PLUGIN / "plugin.json",
        PLUGIN / ".codex-plugin" / "plugin.json",
        PLUGIN / ".claude-plugin" / "plugin.json",
        PLUGIN / ".cursor-plugin" / "plugin.json",
        PLUGIN / "server" / "package.json",
    ]
    for manifest_path in manifests:
        manifest = json.loads(read(manifest_path))
        require(str(manifest["version"]).split("+")[0] == VERSION, f"version drift in {manifest_path}")
        if "description" in manifest:
            require("harness" in manifest["description"].lower(), f"public description is not harness-first: {manifest_path}")

    require("framework prepare" in commands, "internal framework CLI contract was accidentally renamed")
    require((PLUGIN / "runtime" / "carbon" / "references" / "automatic-framework.md").is_file(), "framework-layer documentation was removed")
    require((PLUGIN / "runtime" / "carbon" / "scripts" / "carbon_framework.py").is_file(), "framework-layer runtime was removed")

    if len(sys.argv) > 1:
        marketing = Path(sys.argv[1]).resolve()
        pages = {
            "docs.html": ("Harness map", "The first AI Agentic Verification Harness."),
            "brand.html": ("first AI Agentic Verification Harness",),
            "carbon-beta.html": ("AI Agentic Verification Harness",),
            "install-carbon.html": ("AI Agentic Verification Harness",),
            "index.html": (PRIMARY,),
            "carbon-social-card.svg": ("AI AGENTIC", "VERIFICATION HARNESS"),
        }
        legacy = ("CARBON test framework", "the test framework for AI coding agents")
        for name, required_phrases in pages.items():
            content = read(marketing / name)
            for phrase in required_phrases:
                require(phrase in content, f"{name} is missing: {phrase}")
            for phrase in legacy:
                require(phrase.lower() not in content.lower(), f"{name} still contains legacy category language: {phrase}")
        require((marketing / "carbon-architecture-overview-harness.svg").is_file(), "marketing architecture visual is missing its harness diagram")

    print("CARBON harness positioning contract: PASS")


if __name__ == "__main__":
    main()
