"""Evil mode routes."""

from fastapi import APIRouter
from fastapi.responses import JSONResponse
import globals
from routes.models import EvilMoodSetRequest
from utils.logger import get_logger

logger = get_logger('api')

router = APIRouter()


@router.get("/evil-mode")
def get_evil_mode_status():
    """Get current evil mode status"""
    from utils.evil_mode import is_evil_mode, get_current_evil_mood
    evil_mode = is_evil_mode()
    if evil_mode:
        mood, mood_desc = get_current_evil_mood()
        return {
            "evil_mode": True,
            "mood": mood,
            "description": mood_desc,
            "available_moods": globals.EVIL_AVAILABLE_MOODS
        }
    return {
        "evil_mode": False,
        "mood": None,
        "description": None,
        "available_moods": globals.EVIL_AVAILABLE_MOODS
    }


@router.post("/evil-mode/enable")
def enable_evil_mode():
    """Enable evil mode"""
    from utils.evil_mode import apply_evil_mode_changes

    if globals.EVIL_MODE:
        return {"status": "ok", "message": "Evil mode is already enabled", "evil_mode": True}

    if globals.client and globals.client.loop and globals.client.loop.is_running():
        globals.client.loop.create_task(apply_evil_mode_changes(globals.client))
        return {"status": "ok", "message": "Evil mode enabled", "evil_mode": True}
    else:
        return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})


@router.post("/evil-mode/disable")
def disable_evil_mode():
    """Disable evil mode"""
    from utils.evil_mode import revert_evil_mode_changes

    if not globals.EVIL_MODE:
        return {"status": "ok", "message": "Evil mode is already disabled", "evil_mode": False}

    if globals.client and globals.client.loop and globals.client.loop.is_running():
        globals.client.loop.create_task(revert_evil_mode_changes(globals.client))
        return {"status": "ok", "message": "Evil mode disabled", "evil_mode": False}
    else:
        return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})


@router.post("/evil-mode/toggle")
def toggle_evil_mode():
    """Toggle evil mode on/off"""
    from utils.evil_mode import apply_evil_mode_changes, revert_evil_mode_changes

    if not globals.client or not globals.client.loop or not globals.client.loop.is_running():
        return JSONResponse(status_code=503, content={"status": "error", "message": "Discord client not ready"})

    if globals.EVIL_MODE:
        globals.client.loop.create_task(revert_evil_mode_changes(globals.client))
        return {"status": "ok", "message": "Evil mode disabled", "evil_mode": False}
    else:
        globals.client.loop.create_task(apply_evil_mode_changes(globals.client))
        return {"status": "ok", "message": "Evil mode enabled", "evil_mode": True}


@router.get("/evil-mode/mood")
def get_evil_mood():
    """Get current evil mood"""
    from utils.evil_mode import get_current_evil_mood
    mood, mood_desc = get_current_evil_mood()
    return {
        "mood": mood,
        "description": mood_desc,
        "available_moods": globals.EVIL_AVAILABLE_MOODS
    }


@router.post("/evil-mode/mood")
def set_evil_mood_endpoint(data: EvilMoodSetRequest):
    """Set evil mood"""
    from utils.evil_mode import set_evil_mood, is_valid_evil_mood, update_all_evil_nicknames

    if not is_valid_evil_mood(data.mood):
        return JSONResponse(status_code=400, content={
            "status": "error",
            "message": f"Mood '{data.mood}' not recognized. Available evil moods: {', '.join(globals.EVIL_AVAILABLE_MOODS)}"
        })

    success = set_evil_mood(data.mood)
    if success:
        # Update nicknames if evil mode is active
        if globals.EVIL_MODE and globals.client and globals.client.loop and globals.client.loop.is_running():
            globals.client.loop.create_task(update_all_evil_nicknames(globals.client))
        return {"status": "ok", "new_mood": data.mood}

    return JSONResponse(status_code=500, content={"status": "error", "message": "Failed to set evil mood"})
