#!/usr/bin/env python3
"""get-fable Stop guard.

The guard combines the human ledger contract with strict `.fable/state.json`
semantics. It blocks stop for open cards, checked cards without substantive
ledger evidence, malformed durable state, stale current-generation evidence,
or substantial work whose durable phase has not reached `complete`.

Unexpected host/runtime errors remain fail-open. Invalid project state is an
explicit workflow condition and therefore blocks substantial completion.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from _fable_common import (  # noqa: E402
    read_hook_input,
    start_dir,
    find_fable_dir,
    ledger_path,
    state_path,
    parse_ledger,
    closed_without_evidence,
    read_state,
    has_fresh_passing_state_evidence,
    has_pending_mutation_debt,
    safe_fable_boundary,
)

MAX_LIST = 12


def block_open_cards(path, open_items):
    shown = open_items[:MAX_LIST]
    lines = "\n".join("    " + item for item in shown)
    if len(open_items) > len(shown):
        lines += "\n    ... and %d more" % (len(open_items) - len(shown))
    sys.stderr.write(
        "[get-fable] BLOCKED stop: %d open ledger card(s) in %s\n%s\n"
        "Finish and verify each card, defer it with a concrete reason, or pause the round for genuinely unrelated work.\n"
        % (len(open_items), path, lines)
    )
    return 2


def block_missing_ledger_evidence(path, bad):
    shown = bad[:MAX_LIST]
    lines = "\n".join("    " + item for item in shown)
    if len(bad) > len(shown):
        lines += "\n    ... and %d more" % (len(bad) - len(shown))
    sys.stderr.write(
        "[get-fable] BLOCKED stop: %d checked card(s) in %s have no substantive ledger evidence:\n%s\n"
        "Append `-- evidence: <command/result or observation>` or uncheck the card and verify it.\n"
        % (len(bad), path, lines)
    )
    return 2


def block_invalid_state(path):
    sys.stderr.write(
        "[get-fable] BLOCKED stop: %s is present but invalid for the current lifecycle schema. "
        "Run `get-fable doctor` and repair or migrate durable state before claiming completion.\n"
        % path
    )
    return 2


def check_anti_slop_blockers(repo_root):
    candidates = [
        os.path.join(repo_root, "target", "release", "fable-anti-slop"),
        os.path.join(repo_root, "target", "debug", "fable-anti-slop"),
        os.path.join(repo_root, "target", "release", "get-fable-native"),
        os.path.join(repo_root, "target", "debug", "get-fable-native"),
    ]
    binary = None
    for c in candidates:
        if os.path.isfile(c) and os.access(c, os.X_OK):
            binary = c
            break
    if not binary:
        import shutil
        binary = shutil.which("fable-anti-slop")

    if not binary:
        return 0

    import subprocess, json
    is_native_cli = os.path.basename(binary).startswith("get-fable-native")
    cmd = [binary, "anti-slop", "scan"] if is_native_cli else [binary, "scan"]
    cmd.extend(["--repo", repo_root, "--base", "HEAD", "--fail-on-blocker", "--json"])

    try:
        proc = subprocess.run(cmd, capture_output=True, text=True, timeout=10)
        if proc.returncode == 2:
            sys.stderr.write("[get-fable] BLOCKED stop: anti-slop engine detected blocker findings in diff!\n")
            try:
                data = json.loads(proc.stdout)
                findings = data.get("findings", [])
                for f in findings:
                    if f.get("severity") == "blocker":
                        sys.stderr.write(
                            "    - [%s] %s in %s: %s\n"
                            % (f.get("id"), f.get("name"), f.get("file"), f.get("evidence"))
                        )
            except Exception:
                if proc.stdout:
                    sys.stderr.write(proc.stdout[:500] + "\n")
            sys.stderr.write("Fix or revert AI coding slop (weak assertions, skipped tests, error swallowing) before completion.\n")
            return 2
    except Exception:
        pass

    return 0


def check_anti_churn_blockers(fable_dir):
    receipts_file = os.path.join(fable_dir, "anti-churn", "receipts.ndjson")
    if not os.path.isfile(receipts_file):
        return 0

    pending_ids = []
    try:
        with open(receipts_file, "r", encoding="utf-8") as f:
            for line in f:
                line = line.strip()
                if not line:
                    continue
                if '"outcome":"pending"' in line or '"outcome": "pending"' in line:
                    try:
                        obj = json.loads(line)
                        if obj.get("outcome") == "pending":
                            pending_ids.append(obj.get("receipt_id", "unknown"))
                    except Exception:
                        pass
    except Exception:
        pass

    if pending_ids:
        sys.stderr.write(
            "[get-fable] BLOCKED stop: %d pending anti-churn reservation(s) left unresolved on ledger (%s).\n"
            "Accept evidence or complete reserved work before closing turn.\n"
            % (len(pending_ids), ", ".join(pending_ids[:3]))
        )
        return 2

    repo_root = os.path.dirname(fable_dir)
    candidates = [
        os.path.join(repo_root, "target", "release", "fable-anti-churn"),
        os.path.join(repo_root, "target", "debug", "fable-anti-churn"),
        os.path.join(repo_root, "target", "release", "get-fable-native"),
        os.path.join(repo_root, "target", "debug", "get-fable-native"),
    ]
    binary = None
    for c in candidates:
        if os.path.isfile(c) and os.access(c, os.X_OK):
            binary = c
            break

    if binary:
        import subprocess
        is_native_cli = os.path.basename(binary).startswith("get-fable-native")
        cmd = (
            [binary, "anti-churn", "verify", "--state", receipts_file]
            if is_native_cli
            else [binary, "verify", "--state", receipts_file]
        )
        try:
            proc = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
            if proc.returncode != 0:
                sys.stderr.write("[get-fable] BLOCKED stop: anti-churn receipts ledger verification failed!\n")
                if proc.stdout:
                    sys.stderr.write(proc.stdout[:500] + "\n")
                if proc.stderr:
                    sys.stderr.write(proc.stderr[:500] + "\n")
                return 2
        except Exception:
            pass

    return 0


def block_state_if_needed(state, fable_dir=None):
    if not isinstance(state, dict) or not state.get("substantial"):
        return 0

    if not has_fresh_passing_state_evidence(state):
        sys.stderr.write(
            "[get-fable] BLOCKED stop: substantial work has no fresh passing completion evidence for the current mutation generation. "
            "Record proof with `get-fable evidence pass <kind> <source> <detail>` after verifying the requested behavior.\n"
        )
        return 2

    if state.get("phase") != "complete":
        sys.stderr.write(
            "[get-fable] BLOCKED stop: substantial work has current verification evidence but durable workflow phase is '%s', not 'complete'. "
            "Finish verification and transition with `get-fable state complete`.\n"
            % state.get("phase")
        )
        return 2

    if fable_dir:
        repo_root = os.path.dirname(fable_dir)
        slop_blocked = check_anti_slop_blockers(repo_root)
        if slop_blocked:
            return slop_blocked
        churn_blocked = check_anti_churn_blockers(fable_dir)
        if churn_blocked:
            return churn_blocked

    return 0


def validated_state_or_block(fable_dir):
    path = state_path(fable_dir)
    if not os.path.isfile(path):
        return None, None
    state = read_state(fable_dir)
    if state is None:
        return None, block_invalid_state(path)
    return state, None


def main():
    data = read_hook_input()
    fable_dir = find_fable_dir(start_dir(data))
    if not fable_dir:
        return 0
    if not safe_fable_boundary(fable_dir):
        sys.stderr.write("[get-fable] BLOCKED stop: unsafe .fable filesystem boundary; repair symlinks or special files before completion.\n")
        return 2
    pending = has_pending_mutation_debt(fable_dir)
    if pending is None:
        return block_invalid_state(state_path(fable_dir))
    if pending:
        sys.stderr.write(
            "[get-fable] BLOCKED stop: workspace mutation debt is pending reconciliation. "
            "Run the next state transaction, then verify the resulting mutation generation.\n"
        )
        return 2
    if data.get("stop_hook_active"):
        return 0

    state, blocked = validated_state_or_block(fable_dir)
    if blocked is not None:
        return blocked

    path = ledger_path(fable_dir)
    if not os.path.isfile(path):
        return block_state_if_needed(state, fable_dir)

    open_items, _has_any, paused = parse_ledger(path)
    if paused:
        return 0
    if open_items:
        return block_open_cards(path, open_items)

    bad = closed_without_evidence(path)
    if bad:
        return block_missing_ledger_evidence(path, bad)

    return block_state_if_needed(state, fable_dir)


if __name__ == "__main__":
    try:
        sys.exit(main())
    except Exception as exc:
        sys.stderr.write("[get-fable] close guard error (ignored): %r\n" % exc)
        sys.exit(0)
