import asyncio
import random
import datetime
from ai import get_ai_response
import logger

last_post_time = None

async def run(bot, scheduler_settings, full_settings):
    global last_post_time
    last_post_time = datetime.datetime.utcnow()

    interval = scheduler_settings["interval_minutes"]
    channel_id = scheduler_settings["channel_id"]
    channel = bot.get_channel(channel_id)
    use_ai = scheduler_settings.get("use_ai", True)

    await bot.wait_until_ready()
    #print("📆 Simple scheduler active.")
    logger.info("📆 Simple scheduler active.")

    while not bot.is_closed():
        now = datetime.datetime.utcnow()
        last_post_time = now

        # Generate or choose message
        if use_ai:
            prompt = "Post a short chaotic or motivational message in the voice of Delta, the RGB catgirl."
            message = get_ai_response(prompt)
        else:
            message = random.choice(scheduler_settings.get("messages", ["Hello from Delta."]))

        await channel.send(message)
        #print(f"📤 [Simple] Sent to #{channel.name}: {message}")
        logger.info(f"📤 [Simple] Sent to #{channel.name}: {message}")

        await asyncio.sleep(interval * 60)
