#!/usr/bin/env python3
"""Verify messages in the Timmy Time NIP-29 group."""
import json
import asyncio
from datetime import timedelta
from nostr_sdk import (
    Keys, Client, NostrSigner, Filter, Kind, 
    RelayUrl, SingleLetterTag, Alphabet
)

RELAY_URL = "wss://alexanderwhitestone.com/relay"
KEYS_FILE = "/Users/apayne/.timmy/nostr/agent_keys.json"
GROUP_ID = "timmy-time"

with open(KEYS_FILE) as f:
    all_keys = json.load(f)

async def main():
    keys = Keys.parse(all_keys["timmy"]["hex_sec"])
    signer = NostrSigner.keys(keys)
    client = Client(signer)
    await client.add_relay(RelayUrl.parse(RELAY_URL))
    await client.connect()
    await asyncio.sleep(3)
    
    f = Filter().kind(Kind(9)).custom_tag(
        SingleLetterTag.lowercase(Alphabet.H),
        GROUP_ID
    )
    events = await client.fetch_events(f, timedelta(seconds=10))
    
    pub_to_name = {data["hex_pub"]: name for name, data in all_keys.items()}
    
    event_list = events.to_vec()
    print(f"Found {len(event_list)} messages in group '{GROUP_ID}':\n")
    for event in event_list:
        author_hex = event.author().to_hex()
        agent = pub_to_name.get(author_hex, f"unknown({author_hex[:12]})")
        print(f"  [{agent}]: {event.content()}")
    
    await client.disconnect()
    
    if len(event_list) >= 6:
        print(f"\nAll agents confirmed in group. Relay: {RELAY_URL}")
    else:
        print(f"\nOnly {len(event_list)} messages found. Expected 6.")

asyncio.run(main())
