# discord_tools/scripts/chat_fetcher.py

import os
import sys
import json
import time
from datetime import datetime

# Add the parent directory to the Python path
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(script_dir))
sys.path.insert(0, project_root)

from discord_tools.config.settings import DISCORD_TOKEN, DATA_DIR, MAX_MESSAGES_PER_REQUEST
from discord_tools.utils.api_utils import make_discord_request

def fetch_all_messages(channel_id):
    """
    Fetch all messages from a specific channel.
    
    :param channel_id: The ID of the channel to fetch messages from
    :return: List of all fetched messages
    """
    all_messages = []
    last_message_id = None
    
    while True:
        endpoint = f'/channels/{channel_id}/messages?limit={MAX_MESSAGES_PER_REQUEST}'
        if last_message_id:
            endpoint += f'&before={last_message_id}'
        
        response = make_discord_request('GET', endpoint)
        
        if not response:
            print(f"Failed to fetch messages. Stopping.")
            break
        
        new_messages = response.json()
        if not new_messages:
            break
        
        all_messages.extend(new_messages)
        print(f"Fetched {len(all_messages)} messages so far...")
        
        last_message_id = new_messages[-1]['id']
        time.sleep(1)  # To avoid hitting rate limits
    
    return all_messages

def format_messages(messages):
    """
    Format the messages for export.
    
    :param messages: List of message objects
    :return: List of formatted message dictionaries
    """
    return [
        {
            'id': msg['id'],
            'content': msg['content'],
            'author': msg['author']['username'],
            'timestamp': msg['timestamp'],
            'attachments': [att['url'] for att in msg.get('attachments', [])],
            'embeds': msg.get('embeds', [])
        } for msg in messages
    ]

def export_to_json(messages, channel_id):
    """
    Export the formatted messages to a JSON file.
    
    :param messages: List of formatted message dictionaries
    :param channel_id: The ID of the channel the messages are from
    """
    formatted_messages = format_messages(messages)
    
    filename = f'channel_{channel_id}_export_{datetime.now().strftime("%Y%m%d_%H%M%S")}.json'
    filepath = os.path.join(DATA_DIR, filename)
    
    with open(filepath, 'w', encoding='utf-8') as f:
        json.dump(formatted_messages, f, ensure_ascii=False, indent=4)
    
    print(f"Exported {len(formatted_messages)} messages to {filepath}")

def main():
    channel_id = input("Enter the channel ID to fetch messages from: ").strip()
    
    if not channel_id.isdigit():
        print("Invalid channel ID. Please enter a numeric ID.")
        return

    print("Starting to fetch messages...")
    messages = fetch_all_messages(channel_id)
    print(f"Finished fetching messages. Total messages: {len(messages)}")
    
    export_to_json(messages, channel_id)

if __name__ == "__main__":
    main()