import logging
from pathlib import Path
from contextvars import ContextVar

# --- Project Root ---
# Get the directory where config.py is located
ROOT_DIR = Path(__file__).parent.resolve()

# --- Server & Logs ---
HOST = "127.0.0.1"
PORT = 8000
request_host = ContextVar("request_host", default=f"{HOST}:{PORT}")
LOG_LEVEL = "DEBUG" # Options: "DEBUG", "INFO", "WARNING", "ERROR"
LOG_FILE = str(ROOT_DIR / "debug.log")
USER_AGENT = "MooseCP/1.0 Local MCP Server (https://long-cat.net/)"

# --- Stable Diffusion Config ---
SD_URL = "http://127.0.0.1:7860"
MODEL_PRESETS_PATH = str(ROOT_DIR / "model_presets.toml")
RES_PRESETS_PATH = str(ROOT_DIR / "resolution_presets.toml")
TAG_DATABASE_PATH = str(ROOT_DIR / "danbooru.csv")
TAG_SEARCH_LIMIT = 20
ENABLE_TAG_WIKI = False
DANBOORU_LOGIN = ""  # Your Danbooru username (Optional)
DANBOORU_API_KEY = "" # Your Danbooru API key (Optional)

# --- Model Specific Token Tuning (Tuned for Gemma 4) ---
# Patch size is typically (clip.vision.patch_size * n_merge)
# For Gemma 4, this is 48px.
PATCH_SIZE = 48 

# The target token count for a single image preview. 
# The actual budget will be this or greater, but as small as possible.
PREVIEW_TOKEN_BUDGET = 70

# Contact sheet grid optimized to fit within Gemma 4's 1120 max token budget
# (10 cols * 7 rows) = 70 images. 
# Each thumb (192px) is 4x4 patches. 
# 70 images * (4*4) = 1120 tokens.
CONTACT_SHEET_COLS = 10
CONTACT_SHEET_ROWS = 7
CONTACT_SHEET_THUMB_SIZE = 192

# --- Image Quality ---
# JPEG image quality (usually max 95). 
# See: https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#jpeg-saving
IMAGE_QUALITY = 95

# --- Font Configuration ---
# Generic font names for cross-platform compatibility
# Pillow's truetype() can often find these by name in the system path
SYSTEM_FONT_NAMES = [
    "arialbd.ttf", 
    "DejaVuSans-Bold",
    "LiberationSans-Bold",
    "Verdana",
    "Tahoma",
]

# Fallback absolute paths for Linux systems
FALLBACK_FONT_PATHS = [
    "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
    "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf",
    "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf",
]
