#!/usr/bin/env python3
"""Verify X/Twitter posts are actually live."""

import asyncio
import json
import sys
from pathlib import Path

try:
    from twikit import Client
except ImportError:
    print("ERROR: twikit not installed")
    sys.exit(1)

COOKIES_PATH = Path("/workspace/scripts/x_cookies.json")


async def verify_tweet(tweet_id: str) -> bool:
    """Verify a tweet exists and is public."""
    if not COOKIES_PATH.exists():
        print(f"❌ Cookies not found: {COOKIES_PATH}")
        return False
    
    with open(COOKIES_PATH) as f:
        cookies = json.load(f)
    
    client = Client('en-US')
    client.set_cookies({
        'auth_token': cookies.get('auth_token'),
        'ct0': cookies.get('ct0'),
    })
    
    try:
        # Try to fetch the tweet
        tweet = await client.get_tweet_by_id(tweet_id)
        if tweet and hasattr(tweet, 'text'):
            print(f"✅ VERIFIED — Tweet exists")
            print(f"   Text: {tweet.text[:60]}...")
            return True
    except Exception as e:
        print(f"❌ NOT FOUND — {e}")
        return False
    
    return False


async def main():
    if len(sys.argv) < 2:
        print("Usage: verify_post.py <tweet_id>")
        sys.exit(1)
    
    tweet_id = sys.argv[1]
    success = await verify_tweet(tweet_id)
    sys.exit(0 if success else 1)


if __name__ == "__main__":
    asyncio.run(main())
