"""Configuration module for APNG Lip Sync Tool."""
import os
from pathlib import Path
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# API Configuration
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
GEMINI_MODEL = os.getenv("GEMINI_MODEL", "gemini-2.0-flash-exp")
GEMINI_IMAGE_MODEL = os.getenv("GEMINI_IMAGE_MODEL", "gemini-2.0-flash-exp")

# Directory paths
BASE_DIR = Path(__file__).parent.parent
UPLOADS_DIR = BASE_DIR / "uploads"
OUTPUT_DIR = BASE_DIR / "output"
CHARACTERS_DIR = BASE_DIR / "characters"

# Ensure directories exist
UPLOADS_DIR.mkdir(exist_ok=True)
OUTPUT_DIR.mkdir(exist_ok=True)
CHARACTERS_DIR.mkdir(exist_ok=True)

# Standard lip sync viseme set (Preston Blair phoneme set)
# https://en.wikipedia.org/wiki/Viseme
VISEME_SET = {
    "rest": "Neutral/closed mouth",
    "A": "Open mouth (ah, a)",           # ㅏ, ㅑ, A
    "E": "Wide mouth (eh, e)",            # ㅔ, ㅐ, E
    "I": "Smile shape (ee, i)",           # ㅣ, I
    "O": "Round mouth (oh, o)",           # ㅗ, ㅛ, O
    "U": "Pursed lips (oo, u)",           # ㅜ, ㅠ, U
    "C": "Closed teeth (s, z, ts)",       # ㅅ, ㅈ, ㅊ
    "F": "Lower lip under teeth (f, v)",  # F, V
    "L": "Tongue visible (l, th)",        # ㄹ, L
    "M": "Closed lips (m, b, p)",         # ㅁ, ㅂ, ㅍ, M, B, P
    "W": "Tight round (w)",               # ㅜ, W
}

# Phoneme to viseme mapping for Korean
KOREAN_PHONEME_MAP = {
    "ㅏ": "A", "ㅑ": "A", "ㅘ": "A",
    "ㅓ": "A", "ㅕ": "A", "ㅝ": "A",
    "ㅔ": "E", "ㅐ": "E", "ㅖ": "E", "ㅒ": "E", "ㅙ": "E", "ㅞ": "E",
    "ㅣ": "I", "ㅢ": "I",
    "ㅗ": "O", "ㅛ": "O",
    "ㅜ": "U", "ㅠ": "U",
    "ㅡ": "I",  # Similar to I but less smile
    "ㅁ": "M", "ㅂ": "M", "ㅍ": "M",
    "ㅅ": "C", "ㅆ": "C", "ㅈ": "C", "ㅊ": "C",
    "ㄹ": "L",
    "ㄴ": "L", "ㄷ": "L", "ㅌ": "L",
    "ㄱ": "C", "ㅋ": "C", "ㄲ": "C",
    "ㅎ": "A",
    "ㅇ": "rest",
}

# Animation settings
DEFAULT_FRAME_DELAY = 100  # milliseconds
TRANSITION_FRAMES = 2     # Number of interpolation frames between visemes

# Enhanced viseme descriptions for better image generation
VISEME_PROMPTS = {
    "rest": {
        "description": "Neutral closed mouth, lips together naturally",
        "keywords": ["closed", "relaxed", "neutral"],
    },
    "A": {
        "description": "Wide open mouth, jaw dropped, tongue flat",
        "keywords": ["open wide", "ah sound", "jaw down"],
    },
    "E": {
        "description": "Mouth wide horizontally, teeth visible, slight smile",
        "keywords": ["wide smile", "teeth showing", "eh sound"],
    },
    "I": {
        "description": "Narrow opening, lips pulled back in smile shape",
        "keywords": ["smile", "narrow", "ee sound"],
    },
    "O": {
        "description": "Rounded lips forming circle shape, medium opening",
        "keywords": ["round", "circle", "oh sound"],
    },
    "U": {
        "description": "Lips pursed forward in small circle, tight round",
        "keywords": ["pursed", "forward", "oo sound"],
    },
    "C": {
        "description": "Teeth together/close, lips slightly parted showing teeth",
        "keywords": ["teeth visible", "s sound", "closed bite"],
    },
    "F": {
        "description": "Lower lip tucked under upper teeth",
        "keywords": ["lip under teeth", "f sound", "bite lip"],
    },
    "L": {
        "description": "Mouth slightly open, tongue tip visible touching upper teeth",
        "keywords": ["tongue visible", "l sound", "tongue up"],
    },
    "M": {
        "description": "Lips pressed firmly together, closed",
        "keywords": ["closed tight", "m sound", "lips pressed"],
    },
    "W": {
        "description": "Lips rounded and pushed forward, small tight circle",
        "keywords": ["pucker", "w sound", "forward lips"],
    },
}

# TTS Configuration
TTS_LANGUAGE = "ko"  # Default to Korean
TTS_SPEED_SLOW = False

# Supported image formats
SUPPORTED_IMAGE_FORMATS = {"png", "jpg", "jpeg", "gif", "webp"}
