-- ============================================================
-- RealNibba.Config.lua
-- AceConfigDialog handles price/quality/profile settings.
-- This file: Price Edit popup + Sessions popup only.
-- ============================================================
local P = RNPack

-- ============================================================
-- CUSTOM PRICE EDIT  (AceGUI Frame - clean dropdowns + inputs)
-- ============================================================
function P.ShowPriceEdit(link)
    local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
    if not AceGUI then P.prt("AceGUI not available"); return end
    
    local name = GetItemInfo(link) or "Unknown Item"
    
    local frame = AceGUI:Create("Frame")
    frame:SetTitle("Custom Price: " .. name)
    frame:SetStatusText("Set a manual override price for this item")
    frame:SetWidth(340); frame:SetHeight(180)
    frame:SetLayout("Flow")
    frame.frame:SetBackdropColor(0, 0, 0, 0)
    local _cpBg = frame.frame:CreateTexture(nil, "BACKGROUND")
    _cpBg:SetTexture("Interface\\AddOns\\RealNibba.Pack\\Images\\Backdrop")
    _cpBg:SetBlendMode("DISABLE"); _cpBg:SetAllPoints(frame.frame)
    
    -- Gold / Silver / Copper inputs
    local existing = P.DB and P.DB.customPrices and P.DB.customPrices[link]
    local initG = existing and math.floor(existing/10000) or 0
    local initS = existing and math.floor((existing%10000)/100) or 0
    local initC = existing and existing%100 or 0
    
    local goldBox = AceGUI:Create("EditBox")
    goldBox:SetLabel("|cffFFD700Gold|r"); goldBox:SetWidth(90)
    goldBox:SetText(tostring(initG))
    frame:AddChild(goldBox)
    
    local silverBox = AceGUI:Create("EditBox")
    silverBox:SetLabel("|cffC0C0C0Silver|r"); silverBox:SetWidth(90)
    silverBox:SetText(tostring(initS))
    frame:AddChild(silverBox)
    
    local copperBox = AceGUI:Create("EditBox")
    copperBox:SetLabel("|cffCC7A3DCopper|r"); copperBox:SetWidth(90)
    copperBox:SetText(tostring(initC))
    frame:AddChild(copperBox)
    
    local spacer = AceGUI:Create("Label")
    spacer:SetText(" "); spacer:SetFullWidth(true)
    frame:AddChild(spacer)
    
    local setBtn = AceGUI:Create("Button")
    setBtn:SetText("Set Custom Price"); setBtn:SetWidth(160)
    setBtn:SetCallback("OnClick", function()
        local g = tonumber(goldBox:GetText())   or 0
        local s = tonumber(silverBox:GetText()) or 0
        local c = tonumber(copperBox:GetText()) or 0
        local copper = g*10000 + s*100 + c
        if copper > 0 and P.DB then
            if not P.DB.customPrices then P.DB.customPrices = {} end
            P.DB.customPrices[link] = copper
            P.prt("Custom price set: " .. P.FormatMoney(copper))
            if P.RefreshItemList then P.RefreshItemList() end
            AceGUI:Release(frame)
        else
            P.prt("Enter at least 1 copper.")
        end
    end)
    frame:AddChild(setBtn)
    
    local clearBtn = AceGUI:Create("Button")
    clearBtn:SetText("Clear Override"); clearBtn:SetWidth(140)
    clearBtn:SetCallback("OnClick", function()
        if P.DB and P.DB.customPrices then
            P.DB.customPrices[link] = nil
        end
        P.prt("Custom price cleared.")
        if P.RefreshItemList then P.RefreshItemList() end
        AceGUI:Release(frame)
    end)
    frame:AddChild(clearBtn)
    
    frame:SetCallback("OnClose", function(widget)
        AceGUI:Release(widget)
    end)
end


local sessPopup=CreateFrame("Frame","RNPackSessPopup",UIParent)
sessPopup:SetSize(360,400)
sessPopup:SetPoint("CENTER")
sessPopup:SetMovable(true); sessPopup:EnableMouse(true)
sessPopup:RegisterForDrag("LeftButton")
sessPopup:SetScript("OnDragStart",function() sessPopup:StartMoving() end)
sessPopup:SetScript("OnDragStop", function() sessPopup:StopMovingOrSizing() end)
sessPopup:SetClampedToScreen(true); sessPopup:SetBackdrop(P.BACKDROP_DARK)
sessPopup:SetBackdropBorderColor(0.4,0.35,0.25,0.9)
P.ApplyBG(sessPopup, true)
sessPopup:SetToplevel(true)
sessPopup:SetScript("OnMouseDown",function() sessPopup:Raise() end)
sessPopup:SetFrameStrata("DIALOG"); sessPopup:Hide()
if P.RegisterScalableFrame then P.RegisterScalableFrame(sessPopup) end
-- Title
local spTitleLbl=sessPopup:CreateFontString(nil,"OVERLAY","GameFontNormal")
spTitleLbl:SetPoint("TOP",sessPopup,"TOP",0,-10)
spTitleLbl:SetText("|cffCC2222Session|r History")
-- Close button
local spCloseBtn=CreateFrame("Button",nil,sessPopup,"UIPanelCloseButton")
spCloseBtn.isCloseBtn=true
spCloseBtn:SetPoint("TOPRIGHT",sessPopup,"TOPRIGHT",-4,-4)
spCloseBtn:SetScript("OnClick",function() sessPopup:Hide() end)

local spX=CreateFrame("Button",nil,sessPopup,"UIPanelCloseButton")
spX.isCloseBtn=true
spX:SetPoint("TOPRIGHT",sessPopup,"TOPRIGHT",-6,-6)
spX:SetScript("OnClick",function() sessPopup:Hide() end)

local spScroll=CreateFrame("ScrollFrame","RNPackSessScroll",sessPopup,"UIPanelScrollFrameTemplate")
spScroll:SetPoint("TOPLEFT",   sessPopup,"TOPLEFT",  14,-28)
spScroll:SetPoint("BOTTOMRIGHT",sessPopup,"BOTTOMRIGHT",-34,14)
local spChild=CreateFrame("Frame",nil,spScroll)
spChild:SetSize(spScroll:GetWidth() or 300,1)
spScroll:SetScrollChild(spChild)
local spRows={}

-- Session detail (items in a session)
local sdFrame=CreateFrame("Frame","RNPackSessDetail",UIParent)
sdFrame:SetSize(340,380); sdFrame:SetPoint("CENTER")
sdFrame:SetMovable(true); sdFrame:EnableMouse(true)
sdFrame:RegisterForDrag("LeftButton")
sdFrame:SetScript("OnDragStart",function() sdFrame:StartMoving() end)
sdFrame:SetScript("OnDragStop", function() sdFrame:StopMovingOrSizing() end)
sdFrame:SetClampedToScreen(true); sdFrame:SetBackdrop(P.BACKDROP_DARK)
sdFrame:SetBackdropBorderColor(0.4,0.35,0.25,0.9)
P.ApplyBG(sdFrame)
sdFrame:SetToplevel(true); sdFrame:SetFrameStrata("DIALOG"); sdFrame:Hide()
if P.RegisterScalableFrame then P.RegisterScalableFrame(sdFrame) end
-- Title (updated per session in ShowSessionDetail)
local sdTitleLbl=sdFrame:CreateFontString(nil,"OVERLAY","GameFontNormal")
sdTitleLbl:SetPoint("TOP",sdFrame,"TOP",0,-10)
sdTitleLbl:SetText("Session Items")
-- Close button
local sdCloseBtn=CreateFrame("Button",nil,sdFrame,"UIPanelCloseButton")
sdCloseBtn.isCloseBtn=true
sdCloseBtn:SetPoint("TOPRIGHT",sdFrame,"TOPRIGHT",-4,-4)
sdCloseBtn:SetScript("OnClick",function() sdFrame:Hide() end)

local sdX=CreateFrame("Button",nil,sdFrame,"UIPanelCloseButton")
sdX.isCloseBtn=true
sdX:SetPoint("TOPRIGHT",sdFrame,"TOPRIGHT",-6,-6)
sdX:SetScript("OnClick",function() sdFrame:Hide() end)

local sdScroll=CreateFrame("ScrollFrame","RNPackSessDetailScroll",sdFrame,"UIPanelScrollFrameTemplate")
sdScroll:SetPoint("TOPLEFT",  sdFrame,"TOPLEFT",  14,-28)
sdScroll:SetPoint("BOTTOMRIGHT",sdFrame,"BOTTOMRIGHT",-34,14)
local sdChild=CreateFrame("Frame",nil,sdScroll)
sdChild:SetSize(sdScroll:GetWidth() or 290,1); sdScroll:SetScrollChild(sdChild)
local sdRows={}

local function ShowSessionDetail(sess)
        for _,r in ipairs(sdRows) do r:Hide() end; sdRows={}
    if sdTitleLbl then sdTitleLbl:SetText("|cffFFD700"..(sess.zone or "?").."|r  "..P.FormatTime(sess.duration or 0)) end
    local items=sess.lootedItems or {}
    local rowH=20; sdChild:SetHeight(math.max(1,#items*rowH))
    for i,item in ipairs(items) do
        local row=CreateFrame("Frame",nil,sdChild)
        row:SetHeight(rowH)
        row:SetPoint("TOPLEFT",  sdChild,"TOPLEFT",  0,-(i-1)*rowH)
        row:SetPoint("TOPRIGHT", sdChild,"TOPRIGHT",  0,-(i-1)*rowH)
        local rbg=row:CreateTexture(nil,"BACKGROUND"); rbg:SetAllPoints()
        rbg:SetTexture(i%2==0 and 0.12 or 0.07,i%2==0 and 0.12 or 0.07,
                       i%2==0 and 0.14 or 0.09,0.5)
        local ico=row:CreateTexture(nil,"ARTWORK")
        ico:SetSize(16,16); ico:SetPoint("LEFT",row,"LEFT",2,0)
        local nm=row:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        nm:SetPoint("LEFT",ico,"RIGHT",2,0); nm:SetWidth(170); nm:SetJustifyH("LEFT")
        local cnt=row:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        cnt:SetPoint("RIGHT",row,"RIGHT",-58,0); cnt:SetWidth(36); cnt:SetJustifyH("RIGHT")
        cnt:SetTextColor(0.7,0.7,0.7,1)
        local val=row:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        val:SetPoint("RIGHT",row,"RIGHT",-2,0); val:SetWidth(54); val:SetJustifyH("RIGHT")
        val:SetTextColor(1,0.82,0,1)
        local name,_,rarity,_,_,_,_,_,_,tex=GetItemInfo(item.link or "")
        ico:SetTexture(tex or "Interface/Icons/INV_Misc_QuestionMark")
        local qc=P.QUALITY_COLORS[rarity or 1] or P.QUALITY_COLORS[1]
        local dn=name or "Unknown"; if #dn>24 then dn=string.sub(dn,1,24).."..." end
        nm:SetText(dn); nm:SetTextColor(qc[1],qc[2],qc[3],1)
        cnt:SetText("x"..(item.count or 0))
        val:SetText(P.FormatMoneyShort(item.value or 0))
        sdRows[#sdRows+1]=row; row:Show()
    end
    sdFrame:Show(); sdFrame:Raise()
end

local function RefreshSessions()
    for _,r in ipairs(spRows) do r:Hide() end; spRows={}
    local sessions=(P.DB and P.DB.sessions) or {}
    local rowH=42; local W=spChild:GetWidth() or 310
    spChild:SetHeight(math.max(1,#sessions*(rowH+3)+6))
    for i,sess in ipairs(sessions) do
        local row=CreateFrame("Frame",nil,spChild)
        row:SetHeight(rowH)
        row:SetPoint("TOPLEFT", spChild,"TOPLEFT",  0,-(i-1)*(rowH+3))
        row:SetPoint("TOPRIGHT",spChild,"TOPRIGHT",  0,-(i-1)*(rowH+3))
        row:SetBackdrop(P.BACKDROP_DARK)
        row:SetBackdropColor(i%2==0 and 0.10 or 0.07,i%2==0 and 0.10 or 0.07,
                             i%2==0 and 0.12 or 0.09, 0.9)
        row:SetBackdropBorderColor(0.35,0.30,0.20,0.8)
        local total=(sess.cash or 0)+(sess.items or 0)+(sess.quests or 0)
        local gph=sess.duration>0 and math.floor(total/sess.duration*3600) or 0
        local l1=string.format("|cffFFD700%s|r  %s  |cffFFD700%s|r  (%s/hr)",
            string.sub(sess.zone or "?",1,14), P.FormatTime(sess.duration or 0),
            P.FormatMoneyShort(total), P.FormatMoneyShort(gph))
        local l2=string.format("|cff00CC00Herbs:%d|r  |cff4488FFMines:%d|r  Items:%d",
            sess.herbs or 0, sess.mines or 0, sess.itemCount or 0)
        local t1=row:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        t1:SetPoint("TOPLEFT", row,"TOPLEFT", 5,-4)
        t1:SetPoint("TOPRIGHT",row,"TOPRIGHT",-52,-4)
        t1:SetText(l1); t1:SetJustifyH("LEFT")
        local t2=row:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        t2:SetPoint("TOPLEFT",row,"TOPLEFT",5,-18)
        t2:SetPoint("TOPRIGHT",row,"TOPRIGHT",-52,-18)
        t2:SetText(l2); t2:SetJustifyH("LEFT"); t2:SetTextColor(0.7,0.7,0.7,1)
        local itmBtn=P.MakeBtn("Items",row,44,rowH-6)
        itmBtn:SetPoint("RIGHT",row,"RIGHT",-3,0)
        local s=sess
        itmBtn:SetScript("OnClick",function() ShowSessionDetail(s) end)
        row:EnableMouse(true)
        spRows[#spRows+1]=row; row:Show()
        if _G.RNPackSkinFrame then _G.RNPackSkinFrame(row) end
    end
    if #sessions==0 then
        local nl=spChild:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
        nl:SetPoint("TOPLEFT",spChild,"TOPLEFT",6,-6)
        nl:SetTextColor(0.5,0.5,0.5,1); nl:SetText("No sessions yet. Start farming!")
        spRows[1]=nl
    end
end

function P.OpenSessions()
    if sessPopup:IsShown() then sessPopup:Hide(); return end
    RefreshSessions()
    sessPopup:Show(); sessPopup:Raise()
end
P.RefreshSessions = RefreshSessions
