#!/usr/bin/env python3
"""Preview what the okf-memory Read hook would do for a file, without Claude Code.

    bin/okf-memory-preview FILE [--project DIR] [--max-bytes N] [--memory-dir DIR] [--raw]

Simulates the PostToolUse hook after a full Read of FILE. Side effect: like
the real hook, this creates or refreshes the file's memory. Exits 1 when
Claude would see the file's real content (nothing replaced).
"""

from __future__ import annotations

import argparse
import json
import os
import subprocess
import sys
from pathlib import Path

HOOK = Path(__file__).resolve().parent / "../scripts" / "okf_memory.py"


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("file", help="file Claude would Read")
    parser.add_argument("--project", default=".", help="project root (default: cwd)")
    parser.add_argument("--max-bytes", help="CLAUDE_PLUGIN_OPTION_MAX_BYTES")
    parser.add_argument("--memory-dir", help="CLAUDE_PLUGIN_OPTION_MEMORY_DIR")
    parser.add_argument("--raw", action="store_true", help="print the JSON the hook emits")
    args = parser.parse_args()

    project = Path(args.project).expanduser().resolve()
    target = Path(args.file).expanduser().resolve()
    if not target.is_file():
        print(f"no such file: {target}", file=sys.stderr)
        return 2

    env = dict(os.environ, CLAUDE_PROJECT_DIR=str(project))
    for flag, key in ((args.max_bytes, "MAX_BYTES"), (args.memory_dir, "MEMORY_DIR")):
        if flag is not None:
            env[f"CLAUDE_PLUGIN_OPTION_{key}"] = str(flag)

    content = target.read_text(encoding="utf-8", errors="replace")
    n_lines = content.count("\n") + 1
    proc = subprocess.run(
        [sys.executable, str(HOOK)],
        input=json.dumps({
            "cwd": str(project),
            "hook_event_name": "PostToolUse",
            "tool_name": "Read",
            "tool_input": {"file_path": str(target)},
            "tool_response": {"type": "text", "file": {
                "filePath": str(target), "content": content,
                "numLines": n_lines, "startLine": 1, "totalLines": n_lines}},
        }),
        capture_output=True, text=True, env=env,
    )
    if proc.stderr:
        print("\n".join(f"  warning: {line}" for line in proc.stderr.strip().splitlines()),
              file=sys.stderr)
    if not proc.stdout.strip():
        print(f"Claude would see the real content of {target}.", file=sys.stderr)
        return 1
    if args.raw:
        print(proc.stdout.rstrip())
        return 0

    shown = json.loads(proc.stdout)["hookSpecificOutput"]["updatedToolOutput"]["file"]["content"]
    served, source = len(shown), target.stat().st_size
    print(f"\n{'=' * 72}\nserved {served:,} chars (~{served // 4:,} tokens) "
          f"instead of {source:,} bytes (~{source // 4:,} tokens)\n{'=' * 72}\n")
    print(shown)
    return 0


if __name__ == "__main__":
    sys.exit(main())
