#!/usr/bin/env python3
"""Get conversation/thread replies"""
import json
import sys
import requests
from requests_oauthlib import OAuth1

def get_auth():
    with open('/home/clawd/secrets/twitter/credentials.json') as f:
        creds = json.load(f)
    oauth1 = creds['oauth1']
    return OAuth1(
        oauth1['consumer_key'],
        oauth1['consumer_secret'],
        oauth1['access_token'],
        oauth1['access_token_secret']
    )

def get_conversation(conversation_id: str, max_results: int = 100) -> dict:
    """Get tweets in a conversation"""
    auth = get_auth()
    url = "https://api.twitter.com/2/tweets/search/recent"
    params = {
        "query": f"conversation_id:{conversation_id}",
        "max_results": max_results,
        "tweet.fields": "author_id,created_at,in_reply_to_user_id",
        "expansions": "author_id",
        "user.fields": "username,name"
    }
    response = requests.get(url, auth=auth, params=params)
    return {"status": response.status_code, "response": response.json()}

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: twitter_conversation.py <conversation_id>")
        sys.exit(1)
    
    result = get_conversation(sys.argv[1])
    print(json.dumps(result, indent=2))
