package main

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

// GothamEmailWrapper wraps body content in a dark, modern Gotham-branded email template.
func GothamEmailWrapper(body string, emailCtx templates.EmailContext) string {
	var buf bytes.Buffer
	gothamEmailTemplate(emailCtx, body).Render(context.Background(), &buf)
	return buf.String()
}

// gothamEmailTemplate is the Gotham-branded email template component.
templ gothamEmailTemplate(emailCtx templates.EmailContext, body string) {
	<!DOCTYPE html>
	<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
	<head>
		<meta charset="utf-8"/>
		<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
		<meta name="x-apple-disable-message-reformatting"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
		<!--[if mso]>
		<noscript>
			<xml>
				<o:OfficeDocumentSettings>
					<o:PixelsPerInch>96</o:PixelsPerInch>
				</o:OfficeDocumentSettings>
			</xml>
		</noscript>
		<![endif]-->
		<title>{ emailCtx.SiteSettings.SiteName }</title>
		<style type="text/css">
			/* CSS Reset for Email */
			body, table, td, p, a, li, blockquote {
				-webkit-text-size-adjust: 100%;
				-ms-text-size-adjust: 100%;
			}
			table, td {
				mso-table-lspace: 0pt;
				mso-table-rspace: 0pt;
			}
			img {
				-ms-interpolation-mode: bicubic;
				border: 0;
				height: auto;
				line-height: 100%;
				outline: none;
				text-decoration: none;
			}
			body {
				margin: 0 !important;
				padding: 0 !important;
				width: 100% !important;
			}
			a[x-apple-data-detectors] {
				color: inherit !important;
				text-decoration: none !important;
			}
			/* Gotham-specific styles */
			h1, h2, h3, h4, h5, h6 {
				font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
				font-weight: 700;
			}
			/* Responsive styles */
			@media only screen and (max-width: 620px) {
				.email-container {
					width: 100% !important;
					max-width: 100% !important;
				}
				.content-padding {
					padding-left: 24px !important;
					padding-right: 24px !important;
				}
			}
		</style>
	</head>
	<body style={ fmt.Sprintf("background-color: %s; margin: 0; padding: 0; font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;", gothamBgColor(emailCtx)) }>
		<!-- Preview text (hidden) -->
		if emailCtx.PreviewText != "" {
			<div style="display: none; max-height: 0; overflow: hidden; mso-hide: all;">
				{ emailCtx.PreviewText }
			</div>
			<div style="display: none; max-height: 0; overflow: hidden; mso-hide: all;">
				&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;
			</div>
		}

		<!-- Main Container -->
		<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td align="center" style={ fmt.Sprintf("padding: 48px 10px; background-color: %s;", gothamBgColor(emailCtx)) }>
					<!-- Email Container -->
					<table role="presentation" class="email-container" width="600" cellspacing="0" cellpadding="0" border="0" style={ fmt.Sprintf("max-width: 600px; background-color: %s; border: 1px solid %s;", gothamCardColor(emailCtx), gothamBorderColor(emailCtx)) }>

						<!-- Header -->
						<tr>
							<td align="center" style={ fmt.Sprintf("padding: 32px 40px; background-color: %s; border-bottom: 1px solid %s;", gothamCardColor(emailCtx), gothamBorderColor(emailCtx)) }>
								if emailCtx.SiteSettings.LogoURL != "" {
									<img src={ emailCtx.SiteSettings.LogoURL } alt={ emailCtx.SiteSettings.SiteName } style="max-height: 48px; width: auto; display: block;"/>
								} else if emailCtx.SiteSettings.SiteName != "" {
									<h1 style={ fmt.Sprintf("margin: 0; font-size: 24px; font-weight: 800; color: %s; letter-spacing: -0.5px; text-transform: uppercase;", gothamFgColor(emailCtx)) }>
										{ emailCtx.SiteSettings.SiteName }
									</h1>
								}
							</td>
						</tr>

						<!-- Body Content -->
						<tr>
							<td class="content-padding" style={ fmt.Sprintf("padding: 40px 48px; color: %s; font-size: 16px; line-height: 1.75; letter-spacing: 0.01em;", gothamFgColor(emailCtx)) }>
								@templ.Raw(body)
							</td>
						</tr>

						<!-- Footer -->
						<tr>
							<td style={ fmt.Sprintf("padding: 32px 48px; background-color: %s; border-top: 1px solid %s;", gothamMutedColor(emailCtx), gothamBorderColor(emailCtx)) }>
								<table role="presentation" width="100%" cellspacing="0" cellpadding="0" border="0">
									<tr>
										<td align="center">
											<p style={ fmt.Sprintf("margin: 0 0 8px; font-size: 13px; font-weight: 700; color: %s; text-transform: uppercase; letter-spacing: 0.1em;", gothamFgColor(emailCtx)) }>
												{ emailCtx.SiteSettings.SiteName }
											</p>
											if emailCtx.SiteSettings.SiteURL != "" {
												<p style={ fmt.Sprintf("margin: 0 0 16px; font-size: 13px; color: %s;", gothamMutedFgColor(emailCtx)) }>
													<a href={ templ.SafeURL(emailCtx.SiteSettings.SiteURL) } style={ fmt.Sprintf("color: %s; text-decoration: none; border-bottom: 1px solid %s;", gothamPrimaryColor(emailCtx), gothamPrimaryColor(emailCtx)) }>
														{ emailCtx.SiteSettings.SiteURL }
													</a>
												</p>
											}
											if emailCtx.UnsubscribeURL != "" {
												<p style={ fmt.Sprintf("margin: 0; font-size: 11px; color: %s;", gothamMutedFgColor(emailCtx)) }>
													<a href={ templ.SafeURL(emailCtx.UnsubscribeURL) } style={ fmt.Sprintf("color: %s; text-decoration: none;", gothamMutedFgColor(emailCtx)) }>
														Unsubscribe
													</a>
												</p>
											}
										</td>
									</tr>
								</table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</body>
	</html>
}

// Gotham color helper functions - return hex colors with dark theme defaults
func gothamBgColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Background != "" {
		return emailCtx.Colors.Background
	}
	return "#0a0a0a" // Near black
}

func gothamCardColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Card != "" {
		return emailCtx.Colors.Card
	}
	return "#141414" // Dark card
}

func gothamFgColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Foreground != "" {
		return emailCtx.Colors.Foreground
	}
	return "#fafafa" // Near white
}

func gothamPrimaryColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Primary != "" {
		return emailCtx.Colors.Primary
	}
	return "#fafafa" // Gotham uses white as primary
}

func gothamMutedFgColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.MutedForeground != "" {
		return emailCtx.Colors.MutedForeground
	}
	return "#737373" // Gray
}

func gothamMutedColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Muted != "" {
		return emailCtx.Colors.Muted
	}
	return "#1a1a1a" // Darker muted
}

func gothamBorderColor(emailCtx templates.EmailContext) string {
	if emailCtx.Colors.Border != "" {
		return emailCtx.Colors.Border
	}
	return "#262626" // Dark border
}
