#!/usr/bin/env python3
"""Debug why events aren't being stored - check relay responses."""
import json
import asyncio
import time
from datetime import timedelta
from nostr_sdk import (
    Keys, Client, NostrSigner, Filter, Kind, 
    EventBuilder, Tag, RelayUrl, SingleLetterTag, Alphabet,
    Event, Timestamp
)

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)
    
    # Check SendEventOutput details
    print("=== Sending test event ===")
    tags = [Tag.parse(["h", GROUP_ID])]
    builder = EventBuilder(Kind(9), "debug test")
    builder = builder.tags(tags)
    
    result = await client.send_event_builder(builder)
    print(f"Event ID: {result.id.to_hex()}")
    
    # Inspect all attributes of result
    attrs = [x for x in dir(result) if not x.startswith('_')]
    print(f"Result attributes: {attrs}")
    
    # Try to get success/failure info
    for attr in attrs:
        try:
            val = getattr(result, attr)
            if not callable(val):
                print(f"  {attr} = {val}")
            else:
                # Try calling with no args
                try:
                    r = val()
                    print(f"  {attr}() = {r}")
                except:
                    pass
        except Exception as e:
            print(f"  {attr}: error: {e}")
    
    # Check clock - the relay rejects timestamps >120s in past
    print(f"\n=== Clock check ===")
    now = int(time.time())
    print(f"Local unix time: {now}")
    
    # Try a simple kind 1 text note (NOT NIP-29) to see if relay stores anything
    print("\n=== Sending plain kind 1 text note (non-NIP-29) ===")
    builder2 = EventBuilder(Kind(1), "plain text note test")
    try:
        result2 = await client.send_event_builder(builder2)
        print(f"  Event ID: {result2.id.to_hex()}")
    except Exception as e:
        print(f"  ERROR: {e}")
    
    await asyncio.sleep(2)
    
    # Query for kind 1
    print("\n=== Query kind 1 ===")
    f1 = Filter().kind(Kind(1)).limit(10)
    events = await client.fetch_events(f1, timedelta(seconds=10))
    print(f"  Kind 1 events: {len(events.to_vec())}")
    
    await client.disconnect()

asyncio.run(main())
