-- UI/Controls/Section.lua
--
-- A reusable control for creating a standardized section with a header,
-- divider line, and optional description text.

local addonName, SinsToolkit = ...

-- Ensure the namespace exists.
SinsToolkit.UI = SinsToolkit.UI or {}
SinsToolkit.UI.Controls = SinsToolkit.UI.Controls or {}

local Section = {}
SinsToolkit.UI.Controls.Section = Section

function Section:Create(parent, headerText, descriptionText)
    local control = {}
    local Theme = SinsToolkit.UI.Theme

    -- Main container frame for the section.
    local frame = CreateFrame("Frame", nil, parent)
    frame:SetPoint("TOPLEFT")
    frame:SetPoint("TOPRIGHT")
    -- Height will be set dynamically.

    -- Create the header and divider using the Theme helper.
    local header, divider = Theme.ApplySection(frame, headerText)
    
    local contentAnchor = divider
    
    -- Optional description text
    if descriptionText and descriptionText ~= "" then
        local description = frame:CreateFontString(nil, "OVERLAY", Theme.GetFont("text"))
        description:SetPoint("TOPLEFT", divider, "BOTTOMLEFT", 0, Theme.GetSpacing("section_content_top"))
        description:SetPoint("RIGHT", frame, "RIGHT", -Theme.GetSpacing("card_padding"), 0)
        description:SetJustifyH("LEFT")
        description:SetText(descriptionText)
        contentAnchor = description
    end
    
    --[[
        Public API for the new control instance.
    --]]
    
    --[[
        Returns the main container frame for positioning this section.
        @return The Section's main Frame.
    --]]
    function control:GetFrame()
        return frame
    end
    
    --[[
        Returns a frame intended to be the parent for content within this section.
        This provides a consistent anchor point below the header/description.
        @return A Frame to be used as an anchor for content.
    --]]
    function control:GetContentAnchor()
        return contentAnchor
    end

    return control
end
