-- ============================================================================
-- Vamoose's Companion - Constants
-- Imports color schemes from SchemeConstants.lua and adds VC-specific constants
-- ============================================================================

VC = VC or {}
VC.Constants = {}
VC.Colors = {}

-- ============================================================================
-- 1. IMPORT COLOR SCHEMES FROM SHARED SCHEMECONSTANTS
-- ============================================================================

VC.Colors.Schemes = VAMOOSE_SchemeConstants or {}

-- ============================================================================
-- SLIPSTREAM DARK (VC-local copy -- independent of VSS)
-- Deep midnight navy + ley-line cyan. VC default theme.
-- ============================================================================
VC.Colors.Schemes.SlipstreamDark = {
    bg           = {r=0.031, g=0.059, b=0.110, a=0.97},  -- #080f1c deep midnight
    panel        = {r=0.051, g=0.102, b=0.180, a=1.00},  -- #0d1a2e card bg
    panel_alt    = {r=0.067, g=0.122, b=0.220, a=1.00},  -- #111f38 hover state

    border       = {r=0.118, g=0.227, b=0.373, a=1.00},  -- #1e3a5f quiet border
    border_accent= {r=0.302, g=0.788, b=0.965, a=1.00},  -- #4dc9f6 active border

    text         = {r=0.784, g=0.847, b=0.941, a=1.00},  -- #c8d8f0 body
    text_header  = {r=0.925, g=0.941, b=0.945, a=1.00},  -- #ecf0f1 titles
    text_dim     = {r=0.533, g=0.600, b=0.667, a=1.00},  -- #8899aa captions

    accent       = {r=0.302, g=0.788, b=0.965, a=1.00},  -- #4dc9f6 ley-line cyan
    accent2      = {r=0.482, g=0.408, b=0.933, a=1.00},  -- #7b68ee portal purple

    button_normal   = {r=0.051, g=0.102, b=0.180, a=1.00},
    button_hover    = {r=0.118, g=0.227, b=0.373, a=1.00},
    button_active   = {r=0.302, g=0.788, b=0.965, a=1.00},
    button_inactive = {r=0.031, g=0.059, b=0.110, a=0.60},
    button_text_norm  = {r=0.784, g=0.847, b=0.941, a=1.00},
    button_text_hover = {r=0.925, g=0.941, b=0.945, a=1.00},
    button_text_dis   = {r=0.353, g=0.478, b=0.604, a=0.50},

    success      = {r=0.102, g=0.737, b=0.612, a=1.00},  -- #1abc9c frost teal
    warning      = {r=0.953, g=0.612, b=0.071, a=1.00},  -- #f39c12 arrival gold
    error        = {r=0.906, g=0.298, b=0.235, a=1.00},  -- #e74c3c

    fonts = {
        header = { file = "Fonts\\MORPHEUS.ttf", size = 14, flags = "" },
        body   = { file = "Fonts\\ARIALN.TTF",   size = 12, flags = "" },
        small  = { file = "Fonts\\ARIALN.TTF",   size = 10, flags = "" },
        xsmall = { file = "Fonts\\ARIALN.TTF",   size = 9,  flags = "" },
    },
}

-- VC-specific semantic colors injected into every scheme
local VC_COMPANION_COLORS = {
    mood_neutral  = {r=0.533, g=0.600, b=0.667},
    mood_positive = {r=0.102, g=0.737, b=0.612},
    mood_negative = {r=0.906, g=0.298, b=0.235},
    bonding_glow  = {r=0.302, g=0.788, b=0.965},
}

for _, scheme in pairs(VC.Colors.Schemes) do
    for k, v in pairs(VC_COMPANION_COLORS) do
        if not scheme[k] then scheme[k] = v end
    end
end

-- ============================================================================
-- 2. THEME MANAGEMENT
-- ============================================================================

VC.Constants.Colors = VC.Colors.Schemes.SlipstreamDark
VC.Constants.ColorsDark = VC.Colors.Schemes.SlipstreamDark
VC.Constants.ColorsLight = VC.Colors.Schemes.SolarizedLight

-- Theme cycle order (12 themes -- slipstreamdark first as new default)
VC.Constants.ThemeOrder = {
    "slipstreamdark",
    "solarizeddark", "solarizedlight",
    "gruvboxdark", "gruvboxlight",
    "everforestdark", "everforestlight", "everforestaccess",
    "kanagawadark", "kanagawalight",
    "accessibilityhc", "colorblindsafe", "housingtheme",
}

-- Maps config key to scheme name
VC.Constants.ThemeNames = {
    slipstreamdark = "SlipstreamDark",
    solarizeddark = "SolarizedDark",
    solarizedlight = "SolarizedLight",
    gruvboxdark = "GruvboxDark",
    gruvboxlight = "GruvboxLight",
    everforestdark = "EverforestDark",
    everforestlight = "EverforestLight",
    everforestaccess = "EverforestAccess",
    kanagawadark = "KanagawaDark",
    kanagawalight = "KanagawaLight",
    accessibilityhc = "AccessibilityHC",
    colorblindsafe = "ColorblindSafe",
    housingtheme = "HousingTheme",
    -- Legacy mappings
    dark = "SolarizedDark",
    light = "SolarizedLight",
    everforest = "EverforestDark",
    kanagawa = "KanagawaDark",
}

VC.Constants.ThemeDisplayNames = {
    slipstreamdark = "Slipstream Dark",
    solarizeddark = "Solarized Dark",
    solarizedlight = "Solarized Light",
    gruvboxdark = "Gruvbox Dark",
    gruvboxlight = "Gruvbox Light",
    everforestdark = "Everforest Dark",
    everforestlight = "Everforest Light",
    everforestaccess = "Everforest Access",
    kanagawadark = "Kanagawa Dark",
    kanagawalight = "Kanagawa Light",
    accessibilityhc = "Accessibility HC",
    colorblindsafe = "Colorblind Safe",
    housingtheme = "Housing Theme",
    dark = "Solarized Dark",
    light = "Solarized Light",
    everforest = "Everforest Dark",
    kanagawa = "Kanagawa Dark",
}

-- Font families
VC.Constants.FontOrder = { "ARIALN", "FRIZQT__", "skurri", "MORPHEUS" }
VC.Constants.FontFiles = {
    ARIALN = "Fonts\\ARIALN.TTF",
    FRIZQT__ = "Fonts\\FRIZQT__.TTF",
    skurri = "Fonts\\skurri.TTF",
    MORPHEUS = "Fonts\\MORPHEUS.TTF",
}

function VC.Constants:GetFontFile()
    local family = "ARIALN"
    if VC.Store and VC.Store.state and VC.Store.state.config then
        family = VC.Store.state.config.fontFamily or "ARIALN"
    elseif VC_DB and VC_DB.config and VC_DB.config.fontFamily then
        family = VC_DB.config.fontFamily
    end
    return self.FontFiles[family] or self.FontFiles.ARIALN
end

function VC.Constants:GetThemeColors()
    local theme = "slipstreamdark"
    if VC.Store and VC.Store.state and VC.Store.state.config then
        theme = VC.Store.state.config.theme or "slipstreamdark"
    elseif VC_DB and VC_DB.config and VC_DB.config.theme then
        theme = VC_DB.config.theme
    end
    local themeName = self.ThemeNames[theme] or "SlipstreamDark"
    return VC.Colors.Schemes[themeName] or VC.Colors.Schemes.SlipstreamDark
end

function VC.Constants:ApplyTheme()
    self.Colors = self:GetThemeColors()
end

function VC.Constants:GetCurrentTheme()
    if VC.Store and VC.Store.state and VC.Store.state.config then
        return VC.Store.state.config.theme or "slipstreamdark"
    elseif VC_DB and VC_DB.config and VC_DB.config.theme then
        return VC_DB.config.theme
    end
    return "slipstreamdark"
end

function VC.Constants:ToggleTheme()
    local currentTheme = self:GetCurrentTheme()
    local currentIndex = 1
    for i, theme in ipairs(self.ThemeOrder) do
        if theme == currentTheme then
            currentIndex = i
            break
        end
    end
    local nextIndex = (currentIndex % #self.ThemeOrder) + 1
    local newTheme = self.ThemeOrder[nextIndex]
    VC.Store:Dispatch("SET_CONFIG", { key = "theme", value = newTheme })
    self:ApplyTheme()
    return newTheme
end

function VC.Constants:GetColorCode(colorName)
    local color = self.Colors[colorName]
    if color then
        if color.hex then
            return string.format("|cFF%s", color.hex)
        else
            local hex = string.format("%02x%02x%02x",
                math.floor(color.r * 255),
                math.floor(color.g * 255),
                math.floor(color.b * 255))
            return string.format("|cFF%s", hex)
        end
    end
    return "|cFFFFFFFF"
end

-- ============================================================================
-- 3. EXPANSION ZONE MAPPING (continent-level uiMapIDs)
-- ============================================================================

VC.Constants.EXPANSION_ZONES = {
    [1415] = "classic",  -- Eastern Kingdoms
    [1414] = "classic",  -- Kalimdor
    [1945] = "tbc",      -- Outland
    [113]  = "wrath",    -- Northrend
    [948]  = "cata",     -- The Maelstrom
    [424]  = "mop",      -- Pandaria
    [572]  = "wod",      -- Draenor
    [619]  = "legion",   -- Broken Isles
    [875]  = "bfa",      -- Zandalar
    [876]  = "bfa",      -- Kul Tiras
    [1550] = "sl",       -- Shadowlands
    [1978] = "df",       -- Dragon Isles
    [2274] = "tww",      -- Khaz Algar
}

-- ============================================================================
-- 4. MOOD CONSTANTS (session-only, not persisted)
-- ============================================================================

VC.Constants.MOOD = {
    SIGNALS = {
        DEATH = -0.3, DEATH_STREAK = -0.6, BOSS_WIPE = -0.4,
        LOOT_EPIC = 0.3, ACHIEVEMENT = 0.4, BOSS_KILL = 0.2,
        RARE_DROP = 0.5, LEVEL_UP = 0.6, QUEST_COMPLETE = 0.1,
        HOUSING_LEVELUP = 0.5, INITIATIVE_DONE = 0.3,
        MYTHICPLUS_TIMED = 0.4, MYTHICPLUS_DEPLETED = -0.2, PET_WIN = 0.2,
        CRAFT_CRIT = 0.3, UNGHOST = 0.1, BATTLE_REZ = 0.25, BIG_TIP = 0.2,
        EATING = 0.10, DRINKING = 0.10, RESTING = 0.15, HEARTHING = 0.05,
        RARE_SPOTTED = 0.20, IDLE_LONG = -0.10, SWIMMING = 0.05,
        FARMING_LONG = -0.05, ZONE_LONG = -0.05,
    },
    THRESHOLDS = { -- intensity boundaries for state labels
        FRUSTRATED = -0.6, STRESSED = -0.2,
        -- -0.2 to 0.2 = NEUTRAL
        FOCUSED = 0.2, EXCITED = 0.5, TRIUMPHANT = 0.75,
    },
    DECAY_RATE = 0.001,    -- per second toward 0
    GRACE_PERIOD = 30,     -- seconds after signal before decay resumes
    DEATH_STREAK_WINDOW = 600, -- 10 min window for death streak detection
    DEATH_STREAK_COUNT = 3,    -- deaths in window to trigger streak
}

-- Mood state colors for UI display
VC.Constants.MOOD_COLORS = {
    FRUSTRATED  = { r = 0.80, g = 0.20, b = 0.20 },
    STRESSED    = { r = 0.80, g = 0.50, b = 0.20 },
    NEUTRAL     = { r = 0.60, g = 0.60, b = 0.60 },
    FOCUSED     = { r = 0.20, g = 0.60, b = 0.80 },
    EXCITED     = { r = 0.20, g = 0.80, b = 0.40 },
    TRIUMPHANT  = { r = 1.00, g = 0.82, b = 0.00 },
}

-- ============================================================================
-- 5. BONDING CONSTANTS (persisted per-companion)
-- ============================================================================

VC.Constants.BONDING = {
    LEVELS = {
        { name = "Stranger",     threshold = 0 },
        { name = "Acquaintance", threshold = 100 },
        { name = "Friend",       threshold = 300 },
        { name = "Companion",    threshold = 600 },
        { name = "Bonded",       threshold = 1000 },
    },
    GAINS = {
        DAILY_LOGIN = 10, LOGIN_STREAK = 5, LONG_SESSION = 5,
        QUOTE_DELIVERED = 1, CONTENT_COMPLETE = 3,
    },
    DAILY_CAP = 50,
    ABSENCE_PENALTY = -20, -- per week of absence (7+ days)
    -- Rare/epic quality weight multiplier by bonding level
    QUALITY_BOOST = {
        Stranger = 1.0, Acquaintance = 1.1, Friend = 1.3,
        Companion = 1.5, Bonded = 2.0,
    },
}

-- ============================================================================
-- 6. COMPANION ENGINE CONSTANTS
-- ============================================================================

-- Chattiness tiers: index 1-4
VC.Constants.CHATTINESS = {
    { key = "minimal",  label = "Minimal",  globalCD = 90,  categoryMult = 4.0, priorityThreshold = 3 },
    { key = "relaxed",  label = "Relaxed",  globalCD = 45,  categoryMult = 2.0, priorityThreshold = 2 },
    { key = "chatty",   label = "Chatty",   globalCD = 30,  categoryMult = 1.0, priorityThreshold = 2 },
    { key = "maximum",  label = "Maximum",  globalCD = 15,  categoryMult = 0.5, priorityThreshold = 1 },
}

-- Base category cooldowns in seconds (before chattiness multiplier)
VC.Constants.CATEGORY_COOLDOWNS = {
    farming    = 45,
    gold       = 180,
    achieve    = 10,
    death      = 30,
    ambient    = 180,
    quest      = 30,
    boss       = 10,
    levelup    = 0,   -- always fire
    rep        = 45,
    zone       = 120,
    login      = 0,   -- always fire
    housing    = 10,  -- always celebrate housing milestones
    mythicplus = 10,
    petbattle  = 30,
    group      = 120,
    crafting        = 20,
    resurrection    = 60,
    battlerez       = 30,
    travelerslog    = 30,
    scenario        = 10,
    transmog        = 45,
    appearance      = 120,
    transmogcollection = 120,
    fishing         = 120,
    afkreturn       = 180,
    reprank         = 30,
    mission         = 120,
    logout          = 0,
    craftingorder   = 30,
    achieveprogress = 180,
    rarespawn       = 120,
    combatend       = 180,
    friendonline    = 120,
    almostlevel     = 600,
    milestone       = 0,   -- always fire (one-time events)
    bondlevelup     = 0,   -- always fire (one-time events)
    eating          = 60,
    drinking        = 60,
    hearthing       = 300,
    flightpath      = 120,
    restzone        = 600,
    idle            = 300,
}

-- Event intensity levels
VC.Constants.INTENSITY = {
    LOW    = 1,
    MEDIUM = 2,
    HIGH   = 3,
    EPIC   = 4,
}

-- Events scoring below this are dropped
VC.Constants.PRIORITY_THRESHOLD = 3

-- Quote quality tier selection weights (higher = more likely)
VC.Constants.QUALITY_WEIGHTS = {
    common    = 10,
    uncommon  = 7,
    rare      = 3.5,
    epic      = 1,
    legendary = 0.2,
}

-- Rolling quote history cap
VC.Constants.HISTORY_MAX = 200

-- Ambient timer range in seconds
VC.Constants.AMBIENT_TIMER = {
    MIN = 120,
    MAX = 360,
}

-- ============================================================================
-- 7. DISPLAY CONSTANTS
-- ============================================================================

VC.Constants.DISPLAY = {
    DURATION = 6,        -- seconds before auto-hide
    DURATION_EPIC = 10,  -- seconds for epic/legendary quotes
    FADE_IN = 0.3,       -- seconds
    FADE_OUT = 0.5,      -- seconds
    BUBBLE_WIDTH = 300,   -- quote bubble width
    PORTRAIT_SIZE = 48,   -- compact portrait (no labels)
    PORTRAIT_SIZE_STANDARD_W = 120,   -- standard portrait width
    PORTRAIT_SIZE_STANDARD_H = 150,  -- standard portrait height
    TALKING_HEAD_W = 500,
    TALKING_HEAD_H = 160,
}

-- ============================================================================
-- 8. QUALITY TIER COLORS (WoW standard item quality colors)
-- ============================================================================

VC.Constants.QUALITY_COLORS = {
    common    = { r = 0.65, g = 0.65, b = 0.65 },
    uncommon  = { r = 0.12, g = 1.00, b = 0.00 },
    rare      = { r = 0.00, g = 0.44, b = 0.87 },
    epic      = { r = 0.64, g = 0.21, b = 0.93 },
    legendary = { r = 1.00, g = 0.50, b = 0.00 },
}

-- ============================================================================
-- 9. EVENT TYPE DISPLAY LABELS
-- ============================================================================

VC.Constants.EVENT_TYPE_LABELS = {
    farming = "Farming", gold = "Gold", achieve = "Achievement",
    death = "Death", ambient = "Ambient", quest = "Quest",
    boss = "Boss Kill", levelup = "Level Up", rep = "Reputation",
    zone = "Zone Change", login = "Login",
    housing = "Housing", mythicplus = "Mythic+", petbattle = "Pet Battle",
    group = "Group Formed",
    crafting = "Crafting", resurrection = "Resurrection",
    battlerez = "Battle Rez", travelerslog = "Traveler's Log",
    scenario = "Scenario", transmog = "Transmog",
    appearance = "Appearance", transmogcollection = "Transmog Unlocked",
    fishing = "Fishing", afkreturn = "Back from AFK",
    reprank = "Rep Rank Up", mission = "Mission Complete",
    logout = "Logout", craftingorder = "Crafting Order",
    achieveprogress = "Achievement Progress", rarespawn = "Rare Spawn",
    combatend = "Combat End", friendonline = "Friend Online",
    almostlevel = "Almost Level",
    eating = "Eating", drinking = "Drinking", hearthing = "Hearthing",
    flightpath = "Flight Path", restzone = "Rest Zone", idle = "Idle",
}

-- ============================================================================
-- 10. SETTINGS PANEL
-- ============================================================================

VC.Constants.SETTINGS = {
    WIDTH = 760,
    HEIGHT = 580,
    TITLE_BAR_H = 32,
    TAB_BAR_H = 28,
    SIDEBAR_W = 180,
    FOOTER_H = 20,
}

-- Portrait size options stored in VC_DB.config.portraitSize
-- "compact" = 48x48 no labels, "standard" = 80x100 with name+bonding bar
VC.Constants.PORTRAIT_SIZE_DEFAULT = "standard"
