# commands/actions.py
import os
import asyncio
import globals
from utils.moods import load_mood_description
from utils.scheduled import send_bedtime_reminder


def set_mood(new_mood):
    path = os.path.join("moods", f"{new_mood}.txt")
    if os.path.exists(path):
        globals.CURRENT_MOOD = load_mood_description(new_mood)
        globals.CURRENT_MOOD_NAME = new_mood
        return True
    return False


def reset_mood():
    globals.CURRENT_MOOD_NAME = "neutral"
    globals.CURRENT_MOOD = load_mood_description("neutral")


def check_mood():
    return globals.CURRENT_MOOD_NAME


def calm_miku():
    globals.FORCED_ANGRY_UNTIL = None
    if globals.ANGRY_WAKEUP_TIMER and not globals.ANGRY_WAKEUP_TIMER.done():
        globals.ANGRY_WAKEUP_TIMER.cancel()
        globals.ANGRY_WAKEUP_TIMER = None


def reset_conversation(user_id):
    globals.conversation_history[str(user_id)].clear()


async def force_sleep(set_sleep_state=None):
    globals.CURRENT_MOOD_NAME = "asleep"
    globals.CURRENT_MOOD = load_mood_description("asleep")
    globals.PREVIOUS_MOOD_NAME = "sleepy"
    globals.IS_SLEEPING = True

    if set_sleep_state:
        await set_sleep_state(True)

    await asyncio.sleep(3600)

    globals.IS_SLEEPING = False
    if set_sleep_state:
        await set_sleep_state(False)

    reset_mood()


async def wake_up(set_sleep_state=None):
    reset_mood()
    globals.PREVIOUS_MOOD_NAME = "asleep"
    globals.IS_SLEEPING = False

    if set_sleep_state:
        await set_sleep_state(False)


async def send_bedtime_now():
    await send_bedtime_reminder()
