local _, ns = ...

ns.Session = {}
local Session = ns.Session

local function newHistoryEntry()
    local current = ns.DB.currentSession
    return {
        started = current.startTime,
        ended = time(),
        zone = current.zone,
        casts = current.casts,
        catches = current.catches,
        lastCatch = current.lastCatch,
        fishingSkill = current.fishingSkill,
        uniqueItems = ns.Utils:CountTableEntries(current.items),
        rareCatches = current.rareCatches or 0,
        fishPerHour = ns.Utils:FormatPerHour(current.catches or 0, ns.Session:GetElapsed(), 300),
    }
end

function Session:EnsureSession()
    if not ns.DB.currentSession.active then self:Start() end
end

function Session:Start()
    local current = ns.DB.currentSession
    current.active = true
    current.startTime = time()
    current.zone = ns.Utils:GetZoneName()
    current.fishingSkill = ns.Utils:GetFishingSkill()
    current.items = current.items or {}
    current.recentCatches = current.recentCatches or {}
    current.recentRares = current.recentRares or {}
    current.rareCatches = current.rareCatches or 0
    if ns.UI then ns.UI:RefreshAll() end
end

function Session:Stop(saveToHistory)
    local current = ns.DB.currentSession
    if current.active and saveToHistory then
        table.insert(ns.DB.history.sessions, 1, newHistoryEntry())
        ns.DB.stats.sessionsCompleted = (ns.DB.stats.sessionsCompleted or 0) + 1
        while #ns.DB.history.sessions > 20 do table.remove(ns.DB.history.sessions) end
    end
    current.active = false
    -- Auto-restore fishing gear set if configured
    if saveToHistory and ns.DB.profile.gear.autoRestoreOnStop
        and ns.DB.profile.gear.returnSet and ns.DB.profile.gear.returnSet ~= "" then
        ns:ScheduleTimer(function()
            if ns.Fishing and ns.Fishing.RestorePreviousSet then
                ns.Fishing:RestorePreviousSet()
            end
        end, 0.5)
    end
    if ns.UI then ns.UI:RefreshAll() end
end

function Session:Reset()
    local oldActive = ns.DB.currentSession.active
    ns.DB.currentSession = {
        active = oldActive,
        startTime = oldActive and time() or 0,
        zone = ns.Utils:GetZoneName(),
        casts = 0,
        catches = 0,
        lastCatch = "None",
        fishingSkill = ns.Utils:GetFishingSkill(),
        items = {},
        recentCatches = {},
        recentRares = {},
        rareCatches = 0,
        poolCatches = 0,
        openWaterCatches = 0,
    }
    if ns.UI then ns.UI:RefreshAll() end
end

function Session:ResetForLogin()
    ns.DB.currentSession = {
        active = false,
        startTime = 0,
        zone = ns.Utils:GetZoneName(),
        casts = 0,
        catches = 0,
        lastCatch = "None",
        fishingSkill = ns.Utils:GetFishingSkill(),
        items = {},
        recentCatches = {},
        recentRares = {},
        rareCatches = 0,
        poolCatches = 0,
        openWaterCatches = 0,
    }
    ns.state.loginAt = time()
    if ns.UI then ns.UI:RefreshAll() end
end

function Session:GetElapsed()
    local current = ns.DB.currentSession
    if not current.active or not current.startTime or current.startTime == 0 then return 0 end
    return math.max(0, time() - current.startTime)
end
