local addonName, addon = ...

addon.LootTracker = addon.LootTracker or {}
local LT = addon.LootTracker
local Crafting = LT.Crafting

if not Crafting then
    return
end

-- Live profession synchronization is deliberately limited to cache maintenance.
-- Reagent-slot simulation and planner helpers were removed in 0.15.3.

function Crafting:GetClientPatchKey()
    local version, _, _, toc = GetBuildInfo()

    -- Intentionally ignore the internal build/date. Blizzard hotfix builds can
    -- change frequently without changing the profession catalogue. The user
    -- asked for the expensive rebuild only after an actual WoW patch.
    return table.concat({
        tostring(version or ""),
        tostring(toc or ""),
    }, ":")
end

local CACHE_SCHEMA = 3

-- Cheap, session-local gate. It is evaluated only the first time the crafted
-- browser requests data after login/reload. After that the SavedVariables
-- cache is used without touching the live profession database again.
function Crafting:EnsureBlizzardDatabase()
    if self._databaseCheckedThisSession then
        return self:GetCachedRecipeCount(), nil
    end

    self._databaseCheckedThisSession = true
    self:InitializeRecipeDatabase()

    local state = self:GetScanState()
    local patchKey = self:GetClientPatchKey()
    local cacheCount = self:GetCachedRecipeCount()

    local needsRebuild =
        cacheCount == 0
        or state.clientPatch ~= patchKey
        or state.cacheSchema ~= CACHE_SCHEMA

    if not needsRebuild then
        return cacheCount, nil
    end

    return self:SynchronizeWithBlizzard(true)
end

-- Expensive synchronization. Do not call this from events, login handlers,
-- selector changes, or ordinary redraws. EnsureBlizzardDatabase() is the sole
-- automatic entry point and invokes it only for the first cache or a new patch.
function Crafting:SynchronizeWithBlizzard(force)
    self:InitializeRecipeDatabase()

    local state = self:GetScanState()
    local patchKey = self:GetClientPatchKey()
    local cacheCount = self:GetCachedRecipeCount()

    local shouldRebuild = force
        or cacheCount == 0
        or state.clientPatch ~= patchKey
        or state.cacheSchema ~= CACHE_SCHEMA

    if not shouldRebuild then
        return cacheCount, nil
    end

    WhatsNextCache.professionRecipes = {}
    state = self:GetScanState()
    state.clientPatch = patchKey
    state.clientBuild = nil
    state.cacheSchema = CACHE_SCHEMA
    state.lastScan = 0
    state.recipes = 0
    state.skillLines = 0

    local ordersAdded, ordersErr = self:MergeCraftingOrderOptions()
    local added, err = self:ScanProfessionDatabase()

    state = self:GetScanState()
    state.clientPatch = patchKey
    state.cacheSchema = CACHE_SCHEMA
    state.lastSync = time()
    state.recipes = self:GetCachedRecipeCount()
    state.blizzardRecipeAdds = tonumber(added) or 0
    state.orderRecipeAdds = tonumber(ordersAdded) or 0
    state.globalCatalogError = ordersErr

    return state.recipes, ordersErr or err
end
