local addonName, NS = ...
local GR = NS.GR

-- =========================================================================
-- UI: HOME VIEW CONTROLLER
-- =========================================================================

function GR:BuildHomeView()
    local parent = CreateFrame("Frame", "GRS_HomeView", self.ViewContainer)
    parent:SetPoint("TOPLEFT", 0, -30) 
    parent:SetPoint("BOTTOMRIGHT", 0, 0)
    if not self.views then self.views = {} end
    self.views[1] = parent

    local outerScroll = CreateFrame("ScrollFrame", "GRS_OuterScroll", parent, "UIPanelScrollFrameTemplate")
    outerScroll:SetPoint("TOPLEFT", 0, 0)
    outerScroll:SetPoint("BOTTOMRIGHT", -28, 25) 
    
    if outerScroll.ScrollBar then outerScroll.ScrollBar:Hide(); outerScroll.ScrollBar:SetWidth(0) end
    
    local content = CreateFrame("Frame", "GRS_ScrollChild", outerScroll)
    outerScroll:SetScrollChild(content)
    
    local left = CreateFrame("Frame", nil, content)
    left:SetWidth(320); left:SetPoint("TOPLEFT"); left:SetPoint("BOTTOMLEFT")
    NS.CreateBg(left, 0.1, 0.1, 0.1, 0.5); NS.CreateBorder(left, 0.2, 0.2, 0.2, 1)
    
    -- Call Left Pane Builder (UI_Home_Controls.lua)
    self:BuildLeftPane(left)

    local rightPane = CreateFrame("Frame", "GRS_RightPane", content)
    rightPane:SetWidth(500)
    rightPane:SetPoint("TOPRIGHT"); rightPane:SetPoint("BOTTOMRIGHT")
    NS.CreateBg(rightPane, 0.05, 0.05, 0.05, 0.8)
    NS.CreateBorder(rightPane, 0.2, 0.2, 0.2, 1)
    
    -- Call Whisper Pane Builder (UI_Home_Chat.lua)
    self:BuildWhisperPane(rightPane)

    local center = CreateFrame("Frame", "GRS_CenterPane", content)
    center:SetPoint("TOPLEFT", left, "TOPRIGHT")
    center:SetPoint("BOTTOMRIGHT", rightPane, "BOTTOMLEFT")
    
    -- Call Center Pane Builder (UI_Home_Results.lua)
    self:BuildCenterPane(center)
    
    -- Legacy support if Filter UI exists separately
    if self.BuildFilterUI then self:BuildFilterUI() end
    
    parent:SetScript("OnSizeChanged", function(self, w, h)
        local scrollW = w - 52
        local scrollH = h - 45
        
        -- [[ CHANGED: STATIC LAYOUT LOGIC ]]
        -- To achieve the "sliding door" effect where elements do not move/compress but simply get covered,
        -- we must enforce a hard minimum width for the content frame that represents the "Full Open" state.
        -- When the window narrows, the content remains wide, causing the right side to clip off-screen 
        -- rather than shifting leftwards.
        
        local leftPaneW = 320
        local rightPaneW = 500 
        local centerPaneW = 760 -- Standard width for comfortable grid viewing
        
        local fullLayoutW = leftPaneW + centerPaneW + rightPaneW
        
        -- Use max(windowWidth, fullLayoutWidth). 
        -- If window is smaller, content stays at fullLayoutW (clipping right side).
        -- If window is larger, content expands to fill it.
        local finalContentW = math.max(scrollW, fullLayoutW)
        local finalH = math.max(scrollH, 1)
        
        content:SetWidth(finalContentW)
        content:SetHeight(finalH)
        
        local listAvailable = finalH - 115
        if listAvailable < 20 then listAvailable = 20 end

        local whisperAvailable = finalH - 50
        if whisperAvailable < 20 then whisperAvailable = 20 end

        if GR.UpdateResultsLayout then GR:UpdateResultsLayout(listAvailable) end
        if GR.UpdateWhisperLayout then GR:UpdateWhisperLayout(whisperAvailable) end
    end)
    
    GR.UpdateHomeLayout = function()
        local w, h = parent:GetSize()
        parent:GetScript("OnSizeChanged")(parent, w, h)
    end
end