"""PostToolUse hook: periodic progress update reminder.

Matcher: * (all tools)

Non-blocking reminder. Uses a counter file at /tmp/peon_progress_{pid}:
1. Increment counter on every tool call
2. Every 20 calls, query task progress via REST API
3. If progress hasn't been updated recently → output systemMessage reminding agent
4. Always exit 0 (never blocks)
"""

import json
import os
import sys
import time


def main() -> None:
    # Use parent process PID as session identifier (Claude CLI PID)
    ppid = os.getppid()
    counter_file = f"/tmp/peon_progress_{ppid}"

    # Read and increment counter
    try:
        count = int(open(counter_file).read().strip().split(":")[0]) + 1
    except (FileNotFoundError, ValueError, IndexError):
        count = 1

    # Write counter with timestamp
    try:
        with open(counter_file, "w") as f:
            f.write(f"{count}:{time.time()}")
    except OSError:
        pass

    # Only check every 20 tool calls
    if count % 20 != 0:
        sys.exit(0)

    # Output a gentle reminder
    response = {
        "systemMessage": (
            "Reminder: Update your task progress with update_task_progress() "
            "to keep stakeholders informed. Aim for frequent progress updates "
            "throughout the task."
        )
    }
    print(json.dumps(response))
    sys.exit(0)


if __name__ == "__main__":
    main()
