#!/usr/bin/env python3
"""Apply SkillOS safe public copy policy everywhere public copy can appear.

This script is intentionally idempotent and broad. It patches source files,
generated files, JSON proof files, and documentation files so every GitHub
Actions deploy reverts the website to the safe "reference workflow" posture.
"""

from __future__ import annotations
import json, re
from pathlib import Path
from typing import Any

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

SKIP_DIRS = {".git", ".venv", "venv", "__pycache__", "node_modules", ".mypy_cache", ".pytest_cache"}
TEXT_EXTS = {".html", ".js", ".md", ".json", ".txt", ".xml", ".css", ".webmanifest"}

SAFE_NOTE = (
    "Important: SkillOS is an open-source reference implementation. Current metrics are "
    "generated by one reproducible reference workflow in a controlled environment. They are "
    "not audited customer results, financial guarantees, investment advice, legal advice, "
    "medical advice, employment guidance, credit guidance, or promises of future outcomes."
)

REGEX_REPLACEMENTS = [
    (r"\bthe wealth[- ]accumulation layer for self[- ]improving ai agents\b", "the work-compounding layer for self-improving AI agents"),
    (r"\bwealth[- ]accumulation layer\b", "work-compounding layer"),
    (r"\bwealth[- ]accumulation proof\b", "reference workflow proof"),
    (r"\bwealth proof\b", "reference workflow proof"),
    (r"\bone real workflow\b", "one concrete reference workflow"),
    (r"\breal workflow gets cheaper, faster, and better\b", "reference workflow improves across cost, time, and quality"),
    (r"\bone workflow gets cheaper, faster, and better\b", "one reference workflow improves across cost, time, and quality"),
    (r"\breal skill releases\b", "tested skill releases"),
    (r"\bskills become margin\b", "skills improve future work"),
    (r"\bwealth[- ]producing capability\b", "work-compounding capability"),
    (r"\bReal workflows\. Real results\. Real compounding\.", "Reference workflow. Reproducible proof. Measurable compounding."),
    (r"\breal results\b", "reproducible proof"),
]

EXACT_REPLACEMENTS = {
    "Work becomes traces. Traces become skills. Skills become margin.": "Work becomes traces. Traces become skills. Skills improve future work.",
    "SkillOS now proves one real workflow gets cheaper, faster, and better as agents do the work and repeated corrections become tested skill releases.": "SkillOS demonstrates, in one concrete reference workflow, how AI-agent work can become traces, traces can become tested skills, and tested skills can improve unit economics.",
    "One workflow improves across real skill releases.": "One reference workflow improves across tested skill releases.",
}

def is_text_file(path: Path) -> bool:
    return path.suffix.lower() in TEXT_EXTS

def iter_public_text_files():
    for path in ROOT.rglob("*"):
        if not path.is_file():
            continue
        rel_parts = path.relative_to(ROOT).parts
        if any(part in SKIP_DIRS for part in rel_parts):
            continue
        if is_text_file(path):
            yield path

def patch_text(text: str) -> str:
    for old, new in EXACT_REPLACEMENTS.items():
        text = text.replace(old, new)
    for pattern, replacement in REGEX_REPLACEMENTS:
        text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)

    # Qualify projected annual savings unless already qualified.
    text = re.sub(
        r"\bprojected annual savings\b(?!\s+under demo assumptions)",
        "projected annual savings under demo assumptions",
        text,
        flags=re.IGNORECASE,
    )

    # Clean accidental duplicates.
    text = re.sub(r"(under demo assumptions)(\s+under demo assumptions)+", r"\1", text, flags=re.IGNORECASE)
    return text

def patch_json_strings(obj: Any) -> Any:
    if isinstance(obj, str):
        return patch_text(obj)
    if isinstance(obj, list):
        return [patch_json_strings(x) for x in obj]
    if isinstance(obj, dict):
        return {k: patch_json_strings(v) for k, v in obj.items()}
    return obj

def patch_file(path: Path) -> bool:
    raw = path.read_text(encoding="utf-8", errors="ignore")
    if path.suffix.lower() == ".json":
        try:
            obj = json.loads(raw)
            new_obj = patch_json_strings(obj)
            if new_obj != obj:
                path.write_text(json.dumps(new_obj, indent=2) + "\n", encoding="utf-8")
                return True
            return False
        except Exception:
            pass

    new = patch_text(raw)

    if path.name == "README.md" and SAFE_NOTE not in new:
        insert = "\n\n## Important public note\n\n" + SAFE_NOTE + "\n"
        if "\n## " in new:
            head, tail = new.split("\n## ", 1)
            new = head + insert + "\n## " + tail
        else:
            new += insert

    if new != raw:
        path.write_text(new, encoding="utf-8")
        return True
    return False

def main():
    changed = []
    for path in iter_public_text_files():
        if patch_file(path):
            changed.append(str(path.relative_to(ROOT)))
    if changed:
        print("Safe public copy applied to:")
        for rel in changed:
            print(f" - {rel}")
    else:
        print("Safe public copy already up to date.")

if __name__ == "__main__":
    main()
