import os
import sys
from pathlib import Path

# BASE_DIR points to the source folder while developing, and to PyInstaller's
# runtime extraction folder when packaged. APP_ROOT points to the visible EXE
# folder in dist\NexaOffice_WorkSuite.
if getattr(sys, "frozen", False):
    BASE_DIR = Path(getattr(sys, "_MEIPASS", Path(sys.executable).resolve().parent))
    APP_ROOT = Path(sys.executable).resolve().parent
else:
    BASE_DIR = Path(__file__).resolve().parent.parent
    APP_ROOT = BASE_DIR

DEFAULT_DATA_DIR = BASE_DIR / "data"
LANG_DIR = BASE_DIR / "lang"


def _runtime_data_dir() -> Path:
    """Return a writable data folder.

    Important for EXE builds:
    - Do NOT write user documents into the bundled dist/data folder.
    - Store runtime data in AppData so updating/rebuilding the EXE will not
      overwrite or get stuck on old bundled JSON files.
    - If a file named portable.flag exists beside the EXE/source, use local
      portable data for testing.
    """
    portable_flag = APP_ROOT / "portable.flag"
    if portable_flag.exists():
        return APP_ROOT / "data"

    appdata = os.environ.get("APPDATA")
    if appdata:
        return Path(appdata) / "NexaOffice" / "data"

    local = os.environ.get("LOCALAPPDATA")
    if local:
        return Path(local) / "NexaOffice" / "data"

    return Path.home() / ".nexaoffice" / "data"


DATA_DIR = _runtime_data_dir()

LANG_SHORT = {"zh_CN": "CN", "en_US": "EN", "id_ID": "ID"}
LANG_FULL = {"zh_CN": "中文", "en_US": "English", "id_ID": "Indonesia"}
# User-facing language selectors use the confirmed hybrid style.
# Keep internal short codes available for ultra-compact layouts only.
LANG_DISP = {"zh_CN": "CN-中文", "en_US": "EN-English", "id_ID": "ID-Indonesia"}
LANG_REV = {v: k for k, v in {**LANG_SHORT, **LANG_DISP, **LANG_FULL}.items()}
HOLIDAY_NAME_KEY = {"zh_CN": "zh", "en_US": "en", "id_ID": "id"}
APP_VERSION = "v2026.06.21.20"

COLORS = {
    "primary": "#3B82F6",
    "primary_dark": "#2563EB",
    "primary_light": "#EAF2FF",
    "bg": "#F6F8FB",
    "card": "#FFFFFF",
    "card_soft": "#FBFCFF",
    "border": "#E6ECF5",
    "text": "#1F2937",
    "text_secondary": "#6B7280",
    "text_light": "#94A3B8",
    "success": "#16A34A",
    "success_light": "#ECFDF3",
    "danger": "#EF4444",
    "danger_light": "#FEECEC",
    "warning": "#F59E0B",
    "warning_light": "#FFF7E8",
    "solar": "#0EA5A4",
    "solar_light": "#ECFDFC",
    "sidebar": "#F8FBFF",
    "sidebar_active": "#E6F0FF",
    "sidebar_text": "#516070",
    "hero": "#F8FBFF",
    "todo_done": "#CBD5E1",
    "other_month": "#F8FAFC",
    "other_month_text": "#CBD5E1",
}


THEME_PRESETS = {
    "blue": {
        "name_key": "theme_blue",
        "primary": "#2563EB",
        "primary_dark": "#1D4ED8",
        "primary_light": "#EAF2FF",
        "bg": "#F6F8FB",
        "card": "#FFFFFF",
        "card_soft": "#F9FBFF",
        "border": "#E4EAF3",
        "text": "#1F2937",
        "text_secondary": "#64748B",
        "text_light": "#94A3B8",
        "sidebar": "#F8FBFF",
        "sidebar_active": "#EAF2FF",
        "sidebar_text": "#526174",
        "hero": "#F8FBFF",
        "success": "#16A34A",
        "success_light": "#ECFDF3",
        "danger": "#DC2626",
        "danger_light": "#FEECEC",
        "warning": "#D97706",
        "warning_light": "#FFF7E8",
        "solar": "#0EA5A4",
        "solar_light": "#ECFDFC",
        "other_month": "#F8FAFC",
        "other_month_text": "#CBD5E1",
    },
    "cyan": {
        "name_key": "theme_cyan",
        "primary": "#0284C7",
        "primary_dark": "#0369A1",
        "primary_light": "#E0F2FE",
        "bg": "#F6FBFF",
        "card": "#FFFFFF",
        "card_soft": "#F8FCFF",
        "border": "#DDEFFC",
        "text": "#102A43",
        "text_secondary": "#526D82",
        "text_light": "#8AA6B8",
        "sidebar": "#F8FCFF",
        "sidebar_active": "#E0F2FE",
        "sidebar_text": "#526D82",
        "hero": "#F8FCFF",
        "success": "#059669",
        "success_light": "#E9F8F1",
        "danger": "#DC2626",
        "danger_light": "#FEECEC",
        "warning": "#D97706",
        "warning_light": "#FFF7E8",
        "solar": "#0891B2",
        "solar_light": "#E0F7FA",
        "other_month": "#F1F7FB",
        "other_month_text": "#B7C9D6",
    },
    "green": {
        "name_key": "theme_green",
        "primary": "#059669",
        "primary_dark": "#047857",
        "primary_light": "#E7F8F0",
        "bg": "#F7FBF8",
        "card": "#FFFFFF",
        "card_soft": "#FAFFFC",
        "border": "#DDEFE6",
        "text": "#17352A",
        "text_secondary": "#5D7168",
        "text_light": "#94A59D",
        "sidebar": "#FAFFFC",
        "sidebar_active": "#E7F8F0",
        "sidebar_text": "#5D7168",
        "hero": "#FAFFFC",
        "success": "#059669",
        "success_light": "#E7F8F0",
        "danger": "#DC2626",
        "danger_light": "#FEECEC",
        "warning": "#B45309",
        "warning_light": "#FFF4DC",
        "solar": "#0D9488",
        "solar_light": "#E6F8F5",
        "other_month": "#F3F7F3",
        "other_month_text": "#B8C6BC",
    },
    "purple": {
        "name_key": "theme_purple",
        "primary": "#6D28D9",
        "primary_dark": "#5B21B6",
        "primary_light": "#F0E9FF",
        "bg": "#F8F6FF",
        "card": "#FFFFFF",
        "card_soft": "#FCFAFF",
        "border": "#E6DDF8",
        "text": "#2E2240",
        "text_secondary": "#6E617D",
        "text_light": "#A59BB5",
        "sidebar": "#FCFAFF",
        "sidebar_active": "#F0E9FF",
        "sidebar_text": "#6E617D",
        "hero": "#FCFAFF",
        "success": "#059669",
        "success_light": "#E7F8F0",
        "danger": "#DC2626",
        "danger_light": "#FDECEC",
        "warning": "#C2410C",
        "warning_light": "#FFF1E8",
        "solar": "#7C3AED",
        "solar_light": "#F0E9FF",
        "other_month": "#F8F5FC",
        "other_month_text": "#B9AEC9",
    },
    "eye": {
        "name_key": "theme_eye",
        "primary": "#4F7F52",
        "primary_dark": "#3F6541",
        "primary_light": "#EEF7E9",
        "bg": "#F5F2E8",
        "card": "#FFFDF5",
        "card_soft": "#FCF8EA",
        "border": "#E3DDC6",
        "text": "#2F3526",
        "text_secondary": "#6D745C",
        "text_light": "#9CA58C",
        "sidebar": "#FBF8EE",
        "sidebar_active": "#EEF7E9",
        "sidebar_text": "#68705B",
        "hero": "#FBF8EE",
        "success": "#4F7F52",
        "success_light": "#EEF7E9",
        "danger": "#B91C1C",
        "danger_light": "#F7E8E3",
        "warning": "#9A6500",
        "warning_light": "#F8EECF",
        "solar": "#4E7F74",
        "solar_light": "#E8F2EE",
        "other_month": "#F2EEE0",
        "other_month_text": "#B8B199",
    },
    "dark": {
        "name_key": "theme_dark",
        "primary": "#60A5FA",
        "primary_dark": "#3B82F6",
        "primary_light": "#1E3A5F",
        "bg": "#111827",
        "card": "#1F2937",
        "card_soft": "#273244",
        "border": "#374151",
        "text": "#F9FAFB",
        "text_secondary": "#CBD5E1",
        "text_light": "#94A3B8",
        "sidebar": "#0F172A",
        "sidebar_active": "#1E3A5F",
        "sidebar_text": "#CBD5E1",
        "hero": "#1F2937",
        "success_light": "#123825",
        "danger_light": "#4B1D1D",
        "warning_light": "#4A3615",
        "solar_light": "#123A3A",
        "other_month": "#182131",
        "other_month_text": "#64748B",
        "todo_done": "#64748B",
    },
}

def get_theme_colors(theme_id):
    colors = dict(COLORS)
    theme = THEME_PRESETS.get(theme_id or "blue", THEME_PRESETS["blue"])
    colors.update({k: v for k, v in theme.items() if k != "name_key"})
    return colors
