# discord_tools/scripts/status_updater.py

import os
import sys
import time
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 DEFAULT_STATUS_UPDATE_INTERVAL
from discord_tools.utils.api_utils import make_discord_request

def update_status(start_datetime):
    """
    Update the Discord status with the time elapsed since start_datetime.
    
    :param start_datetime: The datetime to calculate the elapsed time from
    :return: True if the status was updated successfully, False otherwise
    """
    now = datetime.datetime.now()
    time_diff = now - start_datetime

    days = time_diff.days
    hours, remainder = divmod(time_diff.seconds, 3600)
    minutes, _ = divmod(remainder, 60)

    status_text = f"{days} days, {hours} hours, {minutes} minutes"

    payload = {
        "custom_status": {
            "text": status_text
        }
    }

    response = make_discord_request('PATCH', '/users/@me/settings', json=payload)
    return response is not None

def main():
    start_date = datetime.datetime(2024, 2, 24, 3, 43) 
    update_interval = DEFAULT_STATUS_UPDATE_INTERVAL

    print(f"Starting status updater. Press Ctrl+C to exit.")
    try:
        while True:
            if update_status(start_date):
                print(f"Status updated successfully at {datetime.datetime.now()}")
            else:
                print("Failed to update status")
            time.sleep(update_interval)
    except KeyboardInterrupt:
        print("\nStatus updater stopped.")

if __name__ == "__main__":
    main()