# globals.py
import os
import discord
from collections import deque
from apscheduler.schedulers.asyncio import AsyncIOScheduler

scheduler = AsyncIOScheduler()

DISCORD_BOT_TOKEN = os.getenv("DISCORD_BOT_TOKEN")

# Autonomous V2 Debug Mode (set to True to see detailed decision logging)
AUTONOMOUS_DEBUG = os.getenv("AUTONOMOUS_DEBUG", "false").lower() == "true"

# Voice Chat Debug Mode (set to True for manual commands and text notifications)
# When False (field deployment), voice chat operates silently without command notifications
VOICE_DEBUG_MODE = os.getenv("VOICE_DEBUG_MODE", "false").lower() == "true"

# Llama.cpp server settings (via llama-swap)
LLAMA_URL = os.getenv("LLAMA_URL", "http://llama-swap:8080")
LLAMA_AMD_URL = os.getenv("LLAMA_AMD_URL", "http://llama-swap-amd:8080")  # Secondary AMD GPU
TEXT_MODEL = os.getenv("TEXT_MODEL", "llama3.1")
VISION_MODEL = os.getenv("VISION_MODEL", "vision")
EVIL_TEXT_MODEL = os.getenv("EVIL_TEXT_MODEL", "darkidol")  # Uncensored model for evil mode
JAPANESE_TEXT_MODEL = os.getenv("JAPANESE_TEXT_MODEL", "swallow")  # Llama 3.1 Swallow model for Japanese
OWNER_USER_ID = int(os.getenv("OWNER_USER_ID", "209381657369772032"))  # Bot owner's Discord user ID for reports
PREFER_AMD_GPU = os.getenv("PREFER_AMD_GPU", "false").lower() == "true"  # Runtime-overridable via config API

# Cheshire Cat AI integration (Phase 3)
CHESHIRE_CAT_URL = os.getenv("CHESHIRE_CAT_URL", "http://cheshire-cat:80")
USE_CHESHIRE_CAT = os.getenv("USE_CHESHIRE_CAT", "true").lower() == "true"  # Default enabled for memory system
CHESHIRE_CAT_API_KEY = os.getenv("CHESHIRE_CAT_API_KEY", "")  # Empty = no auth
CHESHIRE_CAT_TIMEOUT = int(os.getenv("CHESHIRE_CAT_TIMEOUT", "120"))  # Seconds

# Language mode for Miku (english or japanese)
LANGUAGE_MODE = "english"  # Can be "english" or "japanese"

# Set up Discord client
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.presences = True
client = discord.Client(intents=intents)

# Note: llama-swap handles model loading/unloading automatically
# No need to track current_model anymore

KINDNESS_KEYWORDS = [
    "thank you", "love you", "luv u", "you're the best", "so cute",
    "adorable", "amazing", "sweet", "kind", "great job", "well done",
    "precious", "good girl", "cutie", "angel", "my favorite", "so helpful"
]
HEART_REACTIONS = ["💙", "💝", "💖", "💕", "💜", "❤️‍🔥", "☺️"]
kindness_reacted_messages = set()

# DM Mood System (simple, auto-rotating only)
DM_MOOD = "neutral"
DM_MOOD_DESCRIPTION = "I'm feeling neutral and balanced today."
AVAILABLE_MOODS = [
    "bubbly", "sleepy", "curious", "shy", "serious", "excited", "silly",
    "melancholy", "flirty", "romantic", "irritated", "angry", "neutral", "asleep"
]

# Evil Mode System
EVIL_MODE = False
EVIL_DM_MOOD = "evil_neutral"
EVIL_DM_MOOD_DESCRIPTION = "Evil Miku is calculating and cold."
EVIL_AVAILABLE_MOODS = ["aggressive", "cunning", "sarcastic", "evil_neutral", "bored", "manic", "jealous", "melancholic", "playful_cruel", "contemptuous"]
# EVIL_MOOD_EMOJIS removed — canonical source is utils/moods.py

# Bipolar Mode System (both Mikus can argue via webhooks)
BIPOLAR_MODE = False
BIPOLAR_WEBHOOKS = {}  # guild_id -> {"miku_webhook_url": str, "evil_webhook_url": str}
BIPOLAR_ARGUMENT_IN_PROGRESS = {}  # channel_id -> {"active": bool, "exchange_count": int, "current_speaker": str}
MIKU_NORMAL_AVATAR_URL = None  # Cached CDN URL of the regular Miku pfp (valid even after evil mode swaps the bot avatar)

# MOOD_EMOJIS removed — canonical source is utils/moods.py
# bipolar_mode.py now imports from there directly

BOT_USER = None

# Unified prompt history (replaces LAST_FULL_PROMPT and LAST_CAT_INTERACTION)
# Each entry: {id, source, full_prompt, response, user, mood, guild, channel,
#              timestamp, model, response_type}
PROMPT_HISTORY = deque(maxlen=10)
_prompt_id_counter = 0

# Legacy accessors for backward compatibility (routes, CLI, etc.)
# These are computed properties that read from PROMPT_HISTORY
def _get_last_fallback_prompt():
    for entry in reversed(PROMPT_HISTORY):
        if entry.get("source") == "fallback":
            return entry.get("full_prompt", "")
    return ""

def _get_last_cat_interaction():
    for entry in reversed(PROMPT_HISTORY):
        if entry.get("source") == "cat":
            return entry
    return {"full_prompt": "", "response": "", "user": "", "mood": "", "timestamp": ""}

# Persona Dialogue System (conversations between Miku and Evil Miku)
LAST_PERSONA_DIALOGUE_TIME = 0  # Timestamp of last dialogue for cooldown

# Voice Chat Session State
VOICE_SESSION_ACTIVE = False
TEXT_MESSAGE_QUEUE = []  # Queue for messages received during voice session

# Feature Blocking Flags (set during voice session)
VISION_MODEL_BLOCKED = False
IMAGE_GENERATION_BLOCKED = False
IMAGE_GENERATION_BLOCK_MESSAGE = None

