#!/usr/bin/env python3
"""Delete bad replies from X account."""
import json
import asyncio
from pathlib import Path
from twikit import Client

COOKIES_PATH = Path(__file__).parent / "x_cookies.json"

def load_cookies():
    raw = json.loads(COOKIES_PATH.read_text())
    return {
        "auth_token": raw.get("auth_token"),
        "ct0": raw.get("ct0"),
        "twid": raw.get("twid")
    }

async def get_my_recent_replies():
    client = Client(language='en-US')
    cookies = load_cookies()
    client.set_cookies(cookies)
    
    # Get my tweets/replies
    print("Fetching recent tweets...")
    tweets = await client.get_user_tweets('cannadealsfl', 'Replies')
    
    bad_replies = []
    for tweet in tweets:
        text = tweet.text.lower()
        # Look for the generic fallback replies
        if any(phrase in text for phrase in ['nice! 👀', 'solid pickup! 🔥', 'been wanting to try that']):
            bad_replies.append({
                'id': tweet.id,
                'text': tweet.text,
                'created_at': tweet.created_at
            })
    
    return bad_replies

async def delete_tweet(tweet_id):
    client = Client(language='en-US')
    cookies = load_cookies()
    client.set_cookies(cookies)
    
    try:
        await client.delete_tweet(tweet_id)
        print(f"✅ Deleted tweet {tweet_id}")
        return True
    except Exception as e:
        print(f"❌ Failed to delete {tweet_id}: {e}")
        return False

async def main():
    # Find bad replies
    bad = await get_my_recent_replies()
    
    if not bad:
        print("No bad replies found")
        return
    
    print(f"\nFound {len(bad)} bad replies:")
    for t in bad:
        print(f"  - {t['created_at']}: {t['text'][:50]}... (ID: {t['id']})")
    
    # Delete them
    print("\nDeleting...")
    for t in bad:
        await delete_tweet(t['id'])

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