package main

import (
	"context"

	"github.com/a-h/templ"

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

// wrap adapts a templ-returning render function to templates.TemplateFunc.
// templ.Component already implements templates.HTMLComponent via Render.
func wrap(f func(ctx context.Context, doc map[string]any) templ.Component) templates.TemplateFunc {
	return func(ctx context.Context, doc map[string]any) templates.HTMLComponent {
		return f(ctx, doc)
	}
}

// Register is the plugin entry point that registers the Gotham template and blocks.
// Returns an error if template registration fails (e.g., validation failure).
func Register(tr templates.TemplateRegistry, br blocks.BlockRegistry) error {
	// Register the Gotham system template with metadata
	tr.RegisterSystemTemplate(templates.SystemTemplateMeta{
		Key:         "gotham",
		Title:       "Gotham",
		Description: "Dark, modern theme with bold typography",
	})

	// Register page templates for the Gotham system template
	if err := tr.RegisterPageTemplate("gotham", templates.PageTemplateMeta{
		Key:         "default",
		Title:       "Default",
		Description: "Standard dark page layout with header, main content, and footer",
		Slots:       []string{"header", "main", "footer"},
	}, wrap(RenderGotham)); err != nil {
		return err
	}

	if err := tr.RegisterPageTemplate("gotham", templates.PageTemplateMeta{
		Key:         "landing",
		Title:       "Landing Page",
		Description: "Full-width hero sections, ideal for marketing pages",
		Slots:       []string{"hero", "main", "cta", "footer"},
	}, wrap(RenderGothamLanding)); err != nil {
		return err
	}

	if err := tr.RegisterPageTemplate("gotham", templates.PageTemplateMeta{
		Key:         "full-width",
		Title:       "Full Width",
		Description: "Edge-to-edge content without max-width constraints",
		Slots:       []string{"header", "main", "footer"},
	}, wrap(RenderGothamFullWidth)); err != nil {
		return err
	}

	if err := tr.RegisterPageTemplate("gotham", templates.PageTemplateMeta{
		Key:         "centered",
		Title:       "Centered Content",
		Description: "Narrow, centered content for articles and documentation",
		Slots:       []string{"header", "main", "footer"},
	}, wrap(RenderGothamCentered)); err != nil {
		return err
	}

	// Register Gotham-specific blocks
	br.Register(StatsBlockMeta, StatsBlock)
	br.Register(FeaturesBlockMeta, FeaturesBlock)
	br.Register(FooterBlockMeta, FooterBlock)

	// Register overrides for built-in blocks when using Gotham template
	br.RegisterTemplateOverride("gotham", "heading", GothamHeadingBlock)
	br.RegisterTemplateOverride("gotham", "text", GothamTextBlock)

	// Register email wrapper for branded communications
	tr.RegisterEmailWrapper("gotham", GothamEmailWrapper)

	return nil
}

// DefaultMasterPages returns the default master pages that Gotham provides.
// These are created automatically when the plugin is first loaded.
func DefaultMasterPages() []plugin.MasterPageDefinition {
	return []plugin.MasterPageDefinition{
		{
			Key:           "gotham:default-master",
			Title:         "Gotham Default Master",
			PageTemplates: []string{"default", "centered"},
			Blocks: []plugin.MasterPageBlock{
				{
					BlockKey:  "navbar",
					Title:     "Main Navigation",
					Content:   map[string]any{"menuName": "main"},
					Slot:      "header",
					SortOrder: 0,
				},
				{
					BlockKey:  "slot",
					Title:     "Main Content Slot",
					Content:   map[string]any{"slotName": "main", "placeholder": "Page content will appear here"},
					Slot:      "main",
					SortOrder: 0,
				},
				{
					BlockKey:  "gotham:footer",
					Title:     "Site Footer",
					Content:   map[string]any{"showSignup": true},
					Slot:      "footer",
					SortOrder: 0,
				},
			},
		},
	}
}
