package main

import (
	"bytes"
	"context"

	"git.dev.alexdunmow.com/block/core/blocks"
)

// FooterBlockMeta defines metadata for the footer block.
var FooterBlockMeta = blocks.BlockMeta{
	Key:             "footer",
	Title:           "Footer",
	Description:     "Multi-column footer with links and copyright",
	Source:          "gotham",
}

// FooterBlock renders a multi-column footer.
// Content expects: {"copyright": "...", "columns": [{"heading": "...", "links": [{"text": "...", "page_id": "...", "url": "..."}]}]}
func FooterBlock(ctx context.Context, content map[string]any) string {
	columns := getSlice(content, "columns")

	var footerColumns []FooterColumn
	for _, col := range columns {
		links := getSlice(col, "links")
		var footerLinks []FooterLink
		for _, link := range links {
			footerLinks = append(footerLinks, FooterLink{
				Text:   getString(link, "text"),
				PageID: getString(link, "page_id"),
				URL:    getString(link, "url"),
			})
		}
		footerColumns = append(footerColumns, FooterColumn{
			Heading: getString(col, "heading"),
			Links:   footerLinks,
		})
	}

	data := FooterData{
		Copyright: getString(content, "copyright"),
		Columns:   footerColumns,
	}

	var buf bytes.Buffer
	_ = footerComponent(data).Render(ctx, &buf)
	return buf.String()
}

// FooterData contains data for the footer component.
type FooterData struct {
	Copyright string
	Columns   []FooterColumn
}

// FooterColumn represents a column in the footer.
type FooterColumn struct {
	Heading string
	Links   []FooterLink
}

// FooterLink represents a link in the footer.
// If PageID is set, it's an internal page link. Otherwise, URL is used.
type FooterLink struct {
	Text   string
	PageID string
	URL    string
}
