-- =========================================================
-- Cooperativa Agricola - Runtime Settings
-- =========================================================
-- Configurações editáveis pelo dono via Shift+M.
-- Sincronizadas via DepotSettingsEvent.

---@class DepotSettings
DepotSettings = {}
local DepotSettings_mt = Class(DepotSettings)

-- Preset options
DepotSettings.CAPACITY_OPTIONS    = {25000, 50000, 100000, 250000, 500000, 1000000000}
-- Spreads de 10% a 50% (passos de 5%) — admin seleciona no painel (Shift+M)
DepotSettings.SELL_SPREAD_OPTIONS = {0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50}
DepotSettings.BUY_SPREAD_OPTIONS  = {0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50}

-- Aliases retro-compatíveis (DepotSettingsDialog ainda usa esses nomes)
DepotSettings.SELL_RATIO_OPTIONS  = DepotSettings.SELL_SPREAD_OPTIONS
DepotSettings.BUY_MULT_OPTIONS    = DepotSettings.BUY_SPREAD_OPTIONS

-- Aliases retro-compatíveis (DepotSettingsDialog ainda usa esses nomes)
DepotSettings.SELL_RATIO_OPTIONS  = DepotSettings.SELL_SPREAD_OPTIONS
DepotSettings.BUY_MULT_OPTIONS    = DepotSettings.BUY_SPREAD_OPTIONS

DepotSettings.DEFAULTS = {
    seasonalPricing  = false,   -- desabilitado na cooperativa (preço de mercado puro)
    storageCapacity  = 100000,
    coopSellSpread   = 0.10,    -- 10% — desconto pra quem vende à coop
    coopBuySpread    = 0.20,    -- 20% — markup pra quem compra da coop
    debugLogging     = false,

    -- Mantidos por compatibilidade com código antigo (não usados na coop)
    sellRatio        = 0.90,
    buyMultiplier    = 1.00,
}

function DepotSettings.new()
    local self = setmetatable({}, DepotSettings_mt)
    self:reset()
    return self
end

function DepotSettings:reset()
    for k, v in pairs(DepotSettings.DEFAULTS) do
        self[k] = v
    end
end

-- ─── Helpers ─────────────────────────────────────────────

local function findIndex(tbl, value)
    for i, v in ipairs(tbl) do
        if math.abs(v - value) < 0.001 then return i end
    end
    return 1
end

function DepotSettings.indexInOptions(tbl, value)
    return findIndex(tbl, value)
end

function DepotSettings:getCapacityIndex()
    return findIndex(DepotSettings.CAPACITY_OPTIONS, self.storageCapacity)
end

function DepotSettings:getSellSpreadIndex()
    return findIndex(DepotSettings.SELL_SPREAD_OPTIONS, self.coopSellSpread)
end

function DepotSettings:getBuySpreadIndex()
    return findIndex(DepotSettings.BUY_SPREAD_OPTIONS, self.coopBuySpread)
end

-- Por compatibilidade com DepotSettingsDialog que ainda chama:
function DepotSettings:getSellRatioIndex()
    return self:getSellSpreadIndex()
end

function DepotSettings:getBuyMultiplierIndex()
    return self:getBuySpreadIndex()
end

-- ─── Save / Load ─────────────────────────────────────────

function DepotSettings:saveToXML(xmlFile, key)
    xmlFile:setBool(key  .. "#seasonalPricing", self.seasonalPricing)
    xmlFile:setFloat(key .. "#storageCapacity", self.storageCapacity)
    xmlFile:setFloat(key .. "#coopSellSpread",  self.coopSellSpread)
    xmlFile:setFloat(key .. "#coopBuySpread",   self.coopBuySpread)
    xmlFile:setBool(key  .. "#debugLogging",    self.debugLogging)
end

function DepotSettings:loadFromXML(xmlFile, key)
    self.seasonalPricing = xmlFile:getBool(key  .. "#seasonalPricing",
        DepotSettings.DEFAULTS.seasonalPricing)
    self.storageCapacity = xmlFile:getFloat(key .. "#storageCapacity",
        DepotSettings.DEFAULTS.storageCapacity)
    self.coopSellSpread  = xmlFile:getFloat(key .. "#coopSellSpread",
        DepotSettings.DEFAULTS.coopSellSpread)
    self.coopBuySpread   = xmlFile:getFloat(key .. "#coopBuySpread",
        DepotSettings.DEFAULTS.coopBuySpread)
    self.debugLogging    = xmlFile:getBool(key  .. "#debugLogging",
        DepotSettings.DEFAULTS.debugLogging)
end

-- ─── Stream sync ─────────────────────────────────────────

function DepotSettings:writeStream(streamId)
    streamWriteBool(streamId, self.seasonalPricing)
    streamWriteFloat32(streamId, self.storageCapacity)
    streamWriteFloat32(streamId, self.coopSellSpread)
    streamWriteFloat32(streamId, self.coopBuySpread)
    streamWriteBool(streamId, self.debugLogging)
end

function DepotSettings:readStream(streamId)
    self.seasonalPricing = streamReadBool(streamId)
    self.storageCapacity = streamReadFloat32(streamId)
    self.coopSellSpread  = streamReadFloat32(streamId)
    self.coopBuySpread   = streamReadFloat32(streamId)
    self.debugLogging    = streamReadBool(streamId)
end
