# discord_tools/scripts/dm_opener.py

import os
import sys

# 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.utils.api_utils import make_discord_request
from discord_tools.config.settings import ERROR_MESSAGES

def open_dm(recipient_id):
    """
    Opens a Discord DM channel using the recipient's user ID.
    
    :param recipient_id: The user ID of the recipient
    :return: The channel ID of the created DM, or None if the request failed
    """
    url = "/users/@me/channels"
    data = {
        "recipient_id": recipient_id
    }

    response = make_discord_request('POST', url, json=data)

    if response:
        channel_data = response.json()
        channel_id = channel_data["id"]
        print(f"DM channel created successfully: {channel_id}")
        return channel_id
    else:
        print(ERROR_MESSAGES["api_error"])
        return None

def main():
    recipient_id = input("Enter the recipient's user ID: ")
    channel_id = open_dm(recipient_id)
    
    if channel_id:
        print(f"Opened channel: {channel_id}")
    else:
        print("Failed to open DM channel.")

if __name__ == "__main__":
    main()