-- HousingDecorGuide/UI/Projects/HDG_RoomSidePanel.lua
-- Right-pane room detail. Shows shape, occupied/open doors, placeholder for
-- crate list (real crates land in P3).

local _, privateDB = ...
local IDs   = privateDB.HDG_ProjectsIDs
local Store = privateDB.HDG_ProjectsStore

local M = {}

local PANE_WIDTH = 280

function M:Create(parent)
    if self.frame then return self.frame end
    local f = CreateFrame("Frame", nil, parent)
    f:SetWidth(PANE_WIDTH)
    f:SetPoint("TOPRIGHT", parent, "TOPRIGHT", 0, 0)
    f:SetPoint("BOTTOMRIGHT", parent, "BOTTOMRIGHT", 0, 0)
    f:SetFrameStrata("MEDIUM")
    f:Hide()

    local bg = f:CreateTexture(nil, "BACKGROUND")
    bg:SetAllPoints()
    bg:SetColorTexture(0.06, 0.13, 0.21, 0.95)

    local header = f:CreateFontString(nil, "OVERLAY", "GameFontNormal")
    header:SetPoint("TOPLEFT", f, "TOPLEFT", 12, -12)
    header:SetTextColor(0.96, 0.80, 0.36, 1)
    f.header = header

    local meta = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    meta:SetPoint("TOPLEFT", header, "BOTTOMLEFT", 0, -4)
    meta:SetTextColor(0.6, 0.7, 0.8, 1)
    f.meta = meta

    local doorsLabel = f:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
    doorsLabel:SetPoint("TOPLEFT", meta, "BOTTOMLEFT", 0, -16)
    doorsLabel:SetText("Doors")
    f.doorsLabel = doorsLabel

    local doorsList = f:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
    doorsList:SetPoint("TOPLEFT", doorsLabel, "BOTTOMLEFT", 0, -4)
    doorsList:SetWidth(PANE_WIDTH - 24)
    doorsList:SetJustifyH("LEFT")
    doorsList:SetJustifyV("TOP")
    f.doorsList = doorsList

    local crateLabel = f:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
    crateLabel:SetPoint("TOPLEFT", doorsList, "BOTTOMLEFT", 0, -20)
    crateLabel:SetText("Crates")
    f.crateLabel = crateLabel

    local crateStub = f:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
    crateStub:SetPoint("TOPLEFT", crateLabel, "BOTTOMLEFT", 0, -4)
    crateStub:SetText("(arrives in P3)")
    f.crateStub = crateStub

    self.frame = f
    return f
end

function M:Show(roomID)
    if not self.frame then return end
    local room = Store.GetRoom(roomID)
    if not room then self.frame:Hide(); return end

    local parsed = IDs.parsePath(roomID) or {}
    self.frame.header:SetText(string.format("Room %s", parsed.roomShortID or "?"))
    self.frame.meta:SetText(string.format("F%d · shape=%s",
        parsed.floor or 0, tostring(room.shape)))

    local fp = room.fingerprint or {}
    local lines = {}

    -- Occupied (connected) doors
    for _, d in ipairs(fp.occupiedDoors or {}) do
        lines[#lines + 1] = string.format("|cff8ee0a3%s|r connected", d)
    end

    -- Open doors = doors not in occupiedDoors
    -- Walk the full doors list and subtract one per occupiedDoor occurrence
    local seen = {}
    for _, d in ipairs(fp.occupiedDoors or {}) do seen[d] = (seen[d] or 0) + 1 end
    for _, d in ipairs(fp.doors or {}) do
        local count = seen[d] or 0
        if count == 0 then
            lines[#lines + 1] = string.format("|cfff5cb5c%s|r open", d)
        else
            seen[d] = count - 1
        end
    end

    self.frame.doorsList:SetText(table.concat(lines, "\n"))
    self.frame:Show()
end

function M:Hide()
    if self.frame then self.frame:Hide() end
end

privateDB.HDG_RoomSidePanel = M
