#!/usr/bin/env python3
import json
import shutil
import subprocess
import sys


CHATGPT_ICON = "/usr/share/icons/hicolor/512x512/apps/chatgpt-desktop.png"


def main() -> int:
    if len(sys.argv) != 2:
        return 0

    try:
        event = json.loads(sys.argv[1])
    except json.JSONDecodeError:
        return 0

    if event.get("type") != "agent-turn-complete":
        return 0

    notify_send = shutil.which("notify-send")
    if notify_send is None:
        return 0

    message = event.get("last-assistant-message") or "Request completed"
    message = " ".join(str(message).split())
    if len(message) > 180:
        message = message[:177] + "..."

    subprocess.run(
        [
            notify_send,
            "--app-name=Codex",
            f"--icon={CHATGPT_ICON}",
            "Codex",
            message,
        ],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        check=False,
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
