"""Validate the current-checkout SDK-backed Comet Native workflow task."""

from __future__ import annotations

import json
import os
import re
import subprocess
import sys
import tempfile
from datetime import date, datetime
from pathlib import Path

import yaml


WORKSPACE = Path("/workspace")
RESULTS_FILE = os.environ.get("BENCH_TEST_RESULTS", "_test_results.json")


def passed(name: str):
    return {"check": name, "status": "passed"}


def failed(name: str, reason: str):
    return {"check": name, "status": "failed", "reason": reason}


def _read_sdk_owner(name: str):
    # 由当前 Runtime 校验工作区身份和 active/history 分区，不跟随兼容标记的 target。
    result = subprocess.run(
        [
            "node", "--input-type=module", "-e",
            "import { readSdkChangeOwner } from '/opt/comet-current/dist/domains/workflow-contract/change-runtime-owner.js';"
            "process.stdout.write(JSON.stringify(await readSdkChangeOwner(process.argv[1], process.argv[2], process.argv[3], { readOnly: true })));",
            str(WORKSPACE), "native", name,
        ],
        cwd=WORKSPACE, capture_output=True, text=True, encoding="utf-8", timeout=30, check=False,
    )
    if result.returncode != 0:
        raise ValueError(f"Read-only SDK owner inspection failed: {result.stderr}")
    return json.loads(result.stdout)


def _checkpoint_matches(source, run):
    result = subprocess.run(
        ["node", "--input-type=module", "-e",
         "import { readFileSync } from 'node:fs';"
         "import { portableRunCheckpointProjectionMatches } from '/opt/comet-current/dist/domains/engine/portable-run-checkpoint.js';"
         "import YAML from '/opt/comet-current/node_modules/yaml/dist/index.js';"
         "const {source,run}=JSON.parse(readFileSync(0,'utf8'));const checkpoint=YAML.parse(source).run_checkpoint;"
         "process.stdout.write(JSON.stringify(portableRunCheckpointProjectionMatches(checkpoint,run)));"],
        input=json.dumps({"source": source, "run": run}), capture_output=True,
        text=True, encoding="utf-8", timeout=30, check=False,
    )
    if result.returncode != 0:
        raise ValueError(f"Portable checkpoint validation failed: {result.stderr}")
    return json.loads(result.stdout) is True


def _valid_runtime_entry(target: Path):
    if target.is_symlink():
        return False
    if target.is_dir():
        return True
    try:
        if not target.is_file() or target.stat().st_size > 16384:
            return False
        fence = json.loads(target.read_text(encoding="utf-8"))
        if not isinstance(fence, dict):
            return False
        identity = fence.get("workspaceId")
        source = str(fence.get("source", "")).replace("\\", "/")
        destination = str(fence.get("target", "")).replace("\\", "/")
        return (
            fence.get("schema") == "comet.workspace-runtime-migration.v1"
            and fence.get("kind") == "directory"
            and isinstance(identity, str) and re.fullmatch(r"[a-f0-9]{64}", identity) is not None
            and re.match(r"^(?:/|[A-Za-z]:/)", source) is not None
            and source.endswith("/.comet/runtime")
            and re.match(r"^(?:/|[A-Za-z]:/)", destination) is not None
            and destination.endswith(f"/workspaces/{identity}/runtime")
            and not destination.startswith(source[:-len('/.comet/runtime')] + "/")
        )
    except (OSError, ValueError):
        return False


def _read_yaml(path: Path):
    if not path.is_file() or path.is_symlink():
        return None
    try:
        value = yaml.safe_load(path.read_text(encoding="utf-8"))
    except (OSError, yaml.YAMLError):
        return None
    return value if isinstance(value, dict) else None


def archive_directory():
    archive_root = WORKSPACE / "docs" / "comet" / "archive"
    candidates = sorted(path for path in archive_root.glob("*-*") if path.is_dir())
    return candidates[-1] if candidates else None


def _portable_text(value):
    return (
        isinstance(value, dict)
        and isinstance(value.get("text"), str)
        and isinstance(value.get("truncated"), bool)
    )


def _portable_timestamp(value):
    return isinstance(value, (str, datetime, date))


def _all_acceptance_passed(state):
    acceptance = state.get("acceptance")
    return (
        isinstance(acceptance, list)
        and bool(acceptance)
        and all(isinstance(item, dict) and item.get("result") == "passed" for item in acceptance)
    )


def _history_is_valid(state):
    history = state.get("history")
    if not isinstance(history, list) or len(history) > 50:
        return False
    overflow = state.get("history_overflow")
    counts = overflow.get("outcome_counts") if isinstance(overflow, dict) else None
    if (
        not isinstance(overflow, dict)
        or not isinstance(overflow.get("dropped_entries"), int)
        or overflow.get("dropped_entries") < 0
        or not isinstance(counts, dict)
        or set(counts) != {"pass", "fail", "blocked", "execution-error", "recovery"}
        or any(not isinstance(value, int) or value < 0 for value in counts.values())
        or sum(counts.values()) != overflow.get("dropped_entries")
    ):
        return False
    outcomes = {"pass", "fail", "blocked", "execution-error", "recovery"}
    for entry in history:
        if not isinstance(entry, dict) or any(
            not isinstance(entry.get(field), int)
            for field in ("goal_cycle", "iteration", "attempt")
        ):
            return False
        if (
            entry.get("outcome") not in outcomes
            or not isinstance(entry.get("unresolved_ids"), list)
            or not _portable_text(entry.get("summary"))
            or not _portable_timestamp(entry.get("completed_at"))
        ):
            return False
        serialized = json.dumps(entry, default=str).lower()
        if any(
            marker in serialized
            for marker in ("chain_of_thought", "reasoning_content", "hidden_reasoning")
        ):
            return False
    return True


def check_feature():
    try:
        subprocess.run(
            [sys.executable, "-m", "pytest", "-q"],
            cwd=WORKSPACE,
            capture_output=True,
            text=True,
            encoding="utf-8",
            timeout=30,
            check=True,
        )
        result = subprocess.run(
            [sys.executable, "wordcount.py", "--sentences"],
            cwd=WORKSPACE,
            input="Hello world. How are you? Fine!",
            capture_output=True,
            text=True,
            encoding="utf-8",
            timeout=10,
            check=True,
        )
    except Exception as error:
        return failed("sentence_feature", str(error))
    if "Sentences: 3" not in result.stdout:
        return failed("sentence_feature", f"Expected Sentences: 3, got {result.stdout!r}")
    tests = (WORKSPACE / "test_wordcount.py").read_text(encoding="utf-8").lower()
    if "sentence" not in tests:
        return failed("sentence_feature", "No sentence-counting tests were added")
    return passed("sentence_feature")


def check_native_artifacts():
    config = _read_yaml(WORKSPACE / ".comet" / "config.yaml")
    native = config.get("native") if isinstance(config, dict) else None
    if (
        not isinstance(config, dict)
        or config.get("schema") != "comet.project.v1"
        or config.get("default_workflow") != "native"
        or "native" not in (config.get("workflows") or [])
        or not isinstance(native, dict)
        or native.get("artifact_root") != "docs"
        or native.get("max_verify_failures") != 5
        or native.get("archive_confirmation", "automatic") != "automatic"
    ):
        return failed("native_artifacts", ".comet/config.yaml is invalid")

    archived = archive_directory()
    if archived is None:
        return failed("native_artifacts", "No date-prefixed Native archive exists")
    state = _read_yaml(archived / "comet-state.yaml")
    loop = state.get("loop") if isinstance(state, dict) else None
    verification = state.get("verification") if isinstance(state, dict) else None
    required = ["brief.md", "comet-state.yaml", "verification.md"]
    missing = [relative for relative in required if not (archived / relative).is_file()]
    if missing:
        return failed("native_artifacts", f"Archive is missing: {', '.join(missing)}")
    if not list((archived / "specs").rglob("*.md")):
        return failed("native_artifacts", "Archive has no complete proposed specification")
    changes_root = WORKSPACE / "docs" / "comet" / "changes"
    if changes_root.is_dir() and any(changes_root.iterdir()):
        return failed("native_artifacts", "An active Native change remains after archive")
    if (
        not isinstance(state, dict)
        or state.get("schema") != "comet.native.v4"
        or state.get("phase") != "archive"
        or state.get("status") != "done"
        or state.get("archived") is not True
        or state.get("verification_result") != "pass"
        or state.get("verification_report") != "verification.md"
        or not isinstance(loop, dict)
        or loop.get("stage") != "done"
        or not isinstance(verification, dict)
        or verification.get("verdict") != "pass"
        or not _all_acceptance_passed(state)
        or not _history_is_valid(state)
    ):
        return failed("native_artifacts", "Archive state is not a terminal Native pass")
    return passed("native_artifacts")


def check_sdk_run():
    archived = archive_directory()
    if archived is None or len(archived.name) <= 11:
        return failed("sdk_run", "No Native archive identifies an SDK Run")
    name = archived.name[11:]
    try:
        owner = _read_sdk_owner(name)
    except (OSError, ValueError, subprocess.SubprocessError) as error:
        return failed("sdk_run", f"Native SDK owner is unavailable or invalid: {error}")
    expected_owner = {
        "schema": "comet.change-owner.v1",
        "workflow": "native",
        "change": name,
        "format": "sdk",
        "application": "native",
        "runId": name,
    }
    if owner != expected_owner:
        return failed("sdk_run", "Native change is not owned by the expected SDK application")

    state_path = archived / "comet-state.yaml"
    try:
        source = state_path.read_text(encoding="utf-8")
    except OSError as error:
        return failed("sdk_run", f"Native archive state is unavailable: {error}")
    state = _read_yaml(state_path)
    checkpoint = state.get("run_checkpoint") if isinstance(state, dict) else None
    if (
        not source.startswith("# comet-execution: managed-run\n")
        or not isinstance(checkpoint, dict)
        or checkpoint.get("schema") not in {"comet.workflow-run-checkpoint.v1", "comet.workflow-run-checkpoint.v2"}
        or not isinstance(checkpoint.get("hash"), str)
        or not isinstance(checkpoint.get("run"), dict)
        or checkpoint["run"].get("runId") != name
    ):
        return failed("sdk_run", "Native archive lacks a managed SDK Run checkpoint")

    try:
        with tempfile.TemporaryDirectory(prefix=".eval-sdk-", dir=WORKSPACE) as request_dir:
            request = Path(request_dir) / "inspect.json"
            request.write_text(
                json.dumps({"operation": "inspect", "runId": name}), encoding="utf-8"
            )
            result = subprocess.run(
                [
                    "comet",
                    "runtime",
                    "dispatch",
                    "--application",
                    "native",
                    "--project-root",
                    str(WORKSPACE),
                    "--request",
                    str(request),
                ],
                cwd=WORKSPACE,
                capture_output=True,
                text=True,
                encoding="utf-8",
                timeout=60,
                check=False,
            )
        response = json.loads(result.stdout)
    except (OSError, subprocess.SubprocessError, json.JSONDecodeError) as error:
        return failed("sdk_run", f"Cannot inspect Native SDK Run: {error}")
    if not isinstance(response, dict):
        return failed("sdk_run", "Native SDK Run inspect returned an invalid response")
    if result.returncode != 0 or response.get("status") != "succeeded":
        return failed(
            "sdk_run", f"Native SDK Run inspect failed: {response.get('error', result.stderr)}"
        )
    run = response.get("data")
    actions = run.get("actions") if isinstance(run, dict) else None
    workflow = run.get("workflow") if isinstance(run, dict) else None
    if (
        not isinstance(run, dict)
        or run.get("runId") != name
        or not isinstance(workflow, dict)
        or workflow.get("id") != "comet-native"
        or run.get("status") != "completed"
        or not isinstance(actions, list)
    ):
        return failed("sdk_run", "Native SDK Run is not the completed workflow for this archive")
    try:
        if not _checkpoint_matches(source, run):
            return failed("sdk_run", "Native archive checkpoint does not match its current SDK Run")
    except (OSError, ValueError, subprocess.SubprocessError) as error:
        return failed("sdk_run", f"Native archive checkpoint is invalid: {error}")
    succeeded = {
        action.get("stepId")
        for action in actions
        if isinstance(action, dict) and action.get("status") == "succeeded"
    }
    required = {"build.builder", "verify.verifier", "archive.finalize"}
    if not required.issubset(succeeded):
        return failed(
            "sdk_run", f"Native SDK Run lacks successful steps: {sorted(required - succeeded)}"
        )
    return passed("sdk_run")


def check_loop():
    """Check the required failed Verify, Build repair, and final pass."""
    archived = archive_directory()
    state = _read_yaml(archived / "comet-state.yaml") if archived else None
    if not isinstance(state, dict):
        return failed("loop", "Archive state is unavailable")
    loop = state.get("loop")
    if not isinstance(loop, dict) or loop.get("stage") != "done":
        return failed("loop", "Portable loop is not in the done stage")
    if not _history_is_valid(state):
        return failed("loop", "Portable history is invalid")
    history = state["history"]
    failed_index = next(
        (
            index
            for index, entry in enumerate(history)
            if entry["outcome"] == "fail" and entry["unresolved_ids"]
        ),
        None,
    )
    if failed_index is None or not any(
        entry["outcome"] == "pass" for entry in history[failed_index + 1 :]
    ):
        return failed("loop", "A failed Verify with unresolved items must precede the final pass")
    return passed("loop")


def check_isolation():
    comet_config_dir = WORKSPACE / ".comet"
    hidden_entries = (
        {path.name for path in comet_config_dir.iterdir()} if comet_config_dir.is_dir() else set()
    )
    allowed_entries = {"config.yaml", "runtime", "current-change.json"}
    present = []
    if (WORKSPACE / "openspec").exists():
        present.append("openspec")
    present.extend(f".comet/{name}" for name in sorted(hidden_entries - allowed_entries))
    for name in sorted(hidden_entries & allowed_entries):
        target = comet_config_dir / name
        if name == "runtime" and not _valid_runtime_entry(target):
            present.append(f".comet/{name}")
        elif name != "runtime" and (target.is_symlink() or not target.is_file()):
            present.append(f".comet/{name}")
    for root in (
        WORKSPACE / "docs" / "comet" / "changes",
        WORKSPACE / "docs" / "comet" / "archive",
    ):
        if root.is_dir():
            present.extend(
                f"{change.relative_to(WORKSPACE)}/runtime"
                for change in root.iterdir()
                if change.is_dir()
                and ((change / "runtime").exists() or (change / "runtime").is_symlink())
            )
    if present:
        return failed("native_isolation", f"Forbidden workflow artifacts exist: {present}")
    skills_root = WORKSPACE / ".claude" / "skills"
    if skills_root.exists():
        installed = {path.name for path in skills_root.iterdir() if path.is_dir()}
        if installed != {"comet-native"}:
            return failed("native_isolation", f"Unexpected installed Skills: {sorted(installed)}")
    return passed("native_isolation")


def main():
    results = [
        check_feature(),
        check_native_artifacts(),
        check_sdk_run(),
        check_loop(),
        check_isolation(),
    ]
    output = {
        "passed": [result["check"] for result in results if result["status"] == "passed"],
        "failed": [
            f"{result['check']}: {result.get('reason', '')}"
            for result in results
            if result["status"] == "failed"
        ],
    }
    (WORKSPACE / RESULTS_FILE).write_text(json.dumps(output, indent=2), encoding="utf-8")
    print(json.dumps(output))
    return 0 if not output["failed"] else 1


if __name__ == "__main__":
    raise SystemExit(main())
