package main

import (
	"context"
	"git.dev.alexdunmow.com/block/core/templates/bn"
)

type PageData struct {
	Title            string
	Slots            map[string]string
	ThemeMode        string
	ThemeCSS         string
	SiteSettings     bn.SiteSettingsData
	PageMeta         bn.PageMeta
	StructuredData   string
	CSSHash          string
	PageviewNonce    string
	EngagementConfig bn.EngagementConfig
}

func parseGothamPageData(doc map[string]any) PageData {
	title := "Untitled"
	if t, ok := doc["title"].(string); ok {
		title = t
	}

	slots := make(map[string]string)
	if s, ok := doc["slots"].(map[string]string); ok {
		slots = s
	}

	themeCSS := ""
	if tc, ok := doc["theme_css"].(string); ok {
		themeCSS = tc
	}

	structuredData := ""
	if sd, ok := doc["structured_data"].(string); ok {
		structuredData = sd
	}

	cssHash := ""
	if ch, ok := doc["css_hash"].(string); ok {
		cssHash = ch
	}

	pageviewNonce := ""
	if pn, ok := doc["pageview_nonce"].(string); ok {
		pageviewNonce = pn
	}

	themeMode := "light"
	if tm, ok := doc["theme_mode"].(string); ok && tm != "" {
		themeMode = tm
	}

	siteSettings := bn.ParseSiteSettings(doc)
	pageMeta := bn.ParsePageMeta(doc)
	engagementConfig := bn.ParseEngagementConfig(doc)

	return PageData{Title: title, Slots: slots, ThemeMode: themeMode, ThemeCSS: themeCSS, SiteSettings: siteSettings, PageMeta: pageMeta, StructuredData: structuredData, CSSHash: cssHash, PageviewNonce: pageviewNonce, EngagementConfig: engagementConfig}
}

// Default Gotham page template
templ Gotham(data PageData) {
	<!DOCTYPE html>
	<html lang="en" class="dark">
		@bn.Head(bn.HeadData{
			Title:            data.Title,
			Settings:         data.SiteSettings,
			PageMeta:         data.PageMeta,
			ThemeMode:        data.ThemeMode,
			ThemeCSS:         data.ThemeCSS,
			PluginStyles:     []string{"/templates/gotham/style.css"},
			StructuredData:   data.StructuredData,
			CSSHash:          data.CSSHash,
			PageviewNonce:    data.PageviewNonce,
			EngagementConfig: data.EngagementConfig,
		})
		<body class="bg-background text-foreground antialiased min-h-screen flex flex-col">
			@bn.AdminBypassBanner(data.SiteSettings)
			<header class="w-full">
				@templ.Raw(data.Slots["header"])
			</header>
			<main class="flex-grow max-w-4xl mx-auto w-full px-4 py-8">
				if main, ok := data.Slots["main"]; ok && main != "" {
					@templ.Raw(main)
				} else {
					<div class="py-20 text-center">
						<p class="text-muted-foreground">No content blocks assigned to this page.</p>
					</div>
				}
			</main>
			<footer class="w-full mt-auto">
				@templ.Raw(data.Slots["footer"])
			</footer>
			@bn.BodyEnd(data.SiteSettings)
		</body>
	</html>
}

// Landing page template - full-width hero sections for marketing pages
templ GothamLanding(data PageData) {
	<!DOCTYPE html>
	<html lang="en" class="dark">
		@bn.Head(bn.HeadData{
			Title:            data.Title,
			Settings:         data.SiteSettings,
			PageMeta:         data.PageMeta,
			ThemeMode:        data.ThemeMode,
			ThemeCSS:         data.ThemeCSS,
			PluginStyles:     []string{"/templates/gotham/style.css"},
			StructuredData:   data.StructuredData,
			CSSHash:          data.CSSHash,
			PageviewNonce:    data.PageviewNonce,
			EngagementConfig: data.EngagementConfig,
		})
		<body class="bg-background text-foreground antialiased min-h-screen flex flex-col">
			@bn.AdminBypassBanner(data.SiteSettings)
			// Hero section - full width
			<section class="w-full">
				@templ.Raw(data.Slots["hero"])
			</section>
			// Main content
			<main class="flex-grow">
				if main, ok := data.Slots["main"]; ok && main != "" {
					<div class="max-w-6xl mx-auto px-4 py-16">
						@templ.Raw(main)
					</div>
				}
			</main>
			// Call to action section - full width
			<section class="w-full">
				@templ.Raw(data.Slots["cta"])
			</section>
			// Footer
			<footer class="w-full mt-auto">
				@templ.Raw(data.Slots["footer"])
			</footer>
			@bn.BodyEnd(data.SiteSettings)
		</body>
	</html>
}

// Full-width page template - edge-to-edge content
templ GothamFullWidth(data PageData) {
	<!DOCTYPE html>
	<html lang="en" class="dark">
		@bn.Head(bn.HeadData{
			Title:            data.Title,
			Settings:         data.SiteSettings,
			PageMeta:         data.PageMeta,
			ThemeMode:        data.ThemeMode,
			ThemeCSS:         data.ThemeCSS,
			PluginStyles:     []string{"/templates/gotham/style.css"},
			StructuredData:   data.StructuredData,
			CSSHash:          data.CSSHash,
			PageviewNonce:    data.PageviewNonce,
			EngagementConfig: data.EngagementConfig,
		})
		<body class="bg-background text-foreground antialiased min-h-screen flex flex-col">
			@bn.AdminBypassBanner(data.SiteSettings)
			<header class="w-full">
				@templ.Raw(data.Slots["header"])
			</header>
			<main class="flex-grow w-full">
				if main, ok := data.Slots["main"]; ok && main != "" {
					@templ.Raw(main)
				} else {
					<div class="max-w-4xl mx-auto py-20 px-4 text-center">
						<p class="text-muted-foreground">No content blocks assigned to this page.</p>
					</div>
				}
			</main>
			<footer class="w-full mt-auto">
				@templ.Raw(data.Slots["footer"])
			</footer>
			@bn.BodyEnd(data.SiteSettings)
		</body>
	</html>
}

// Centered content page template - narrow, centered layout for articles
templ GothamCentered(data PageData) {
	<!DOCTYPE html>
	<html lang="en" class="dark">
		@bn.Head(bn.HeadData{
			Title:            data.Title,
			Settings:         data.SiteSettings,
			PageMeta:         data.PageMeta,
			ThemeMode:        data.ThemeMode,
			ThemeCSS:         data.ThemeCSS,
			PluginStyles:     []string{"/templates/gotham/style.css"},
			StructuredData:   data.StructuredData,
			CSSHash:          data.CSSHash,
			PageviewNonce:    data.PageviewNonce,
			EngagementConfig: data.EngagementConfig,
		})
		<body class="bg-background text-foreground antialiased min-h-screen flex flex-col">
			@bn.AdminBypassBanner(data.SiteSettings)
			<header class="w-full border-b border-border">
				<div class="max-w-2xl mx-auto px-4">
					@templ.Raw(data.Slots["header"])
				</div>
			</header>
			<main class="flex-grow max-w-2xl mx-auto w-full px-4 py-12">
				if main, ok := data.Slots["main"]; ok && main != "" {
					<article class="prose prose-invert prose-lg max-w-none">
						@templ.Raw(main)
					</article>
				} else {
					<div class="py-20 text-center">
						<p class="text-muted-foreground">No content blocks assigned to this page.</p>
					</div>
				}
			</main>
			<footer class="w-full mt-auto">
				@templ.Raw(data.Slots["footer"])
			</footer>
			@bn.BodyEnd(data.SiteSettings)
		</body>
	</html>
}

func RenderGotham(ctx context.Context, doc map[string]any) templ.Component {
	return Gotham(parseGothamPageData(doc))
}

func RenderGothamLanding(ctx context.Context, doc map[string]any) templ.Component {
	return GothamLanding(parseGothamPageData(doc))
}

func RenderGothamFullWidth(ctx context.Context, doc map[string]any) templ.Component {
	return GothamFullWidth(parseGothamPageData(doc))
}

func RenderGothamCentered(ctx context.Context, doc map[string]any) templ.Component {
	return GothamCentered(parseGothamPageData(doc))
}
