#!/usr/bin/env python3
"""PostToolUse (Read) hook: a Read of a memory is logged, and a history, uncertain or stale memory is named."""

from __future__ import annotations

import json
import os
import sys

import ownpast_hook


def main():
    try:
        event = json.load(sys.stdin)
        path = (event.get("tool_input") or {}).get("file_path") or ""
    except (ValueError, AttributeError):
        return
    if not path.endswith(".md"):
        return
    cwd = event.get("cwd") or os.getcwd()
    absolute = os.path.realpath(os.path.join(cwd, os.path.expanduser(path)))
    found = ownpast_hook.call("/api/v1/memory/outcome/read",
                            {"session": str(event.get("session_id", "")), "path": absolute})
    note = found and found.get("text")
    if note:
        print(json.dumps({"hookSpecificOutput": {"hookEventName": "PostToolUse", "additionalContext": note}}))


if __name__ == "__main__":
    main()
