package templates

import "fmt"

// ─── Template Data Structs ───────────────────────────────────────────────────

type InvoiceEmailData struct {
	CompanyName   string
	InvoiceNumber string
	PackageName   string
	Duration      string
	Subtotal      string
	Tax           string
	Total         string
	DueDate       string
	PaymentURL    string
}

type PaymentEmailData struct {
	CompanyName   string
	InvoiceNumber string
	PackageName   string
	Amount        string
	PaymentMethod string
	PaidAt        string
	Status        string
}

type SubscriptionEmailData struct {
	CompanyName  string
	PackageName  string
	StartedAt    string
	EndAt        string
	Quota        int
	DashboardURL string
}

// ─── Base Layout ─────────────────────────────────────────────────────────────

func baseLayout(title, content string) string {
	return fmt.Sprintf(`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>%s</title>
<style>
  body { margin: 0; padding: 0; background-color: #f4f6f9; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif; }
  .wrapper { max-width: 600px; margin: 0 auto; padding: 20px; }
  .card { background: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 24px rgba(0,0,0,0.06); }
  .header { background: linear-gradient(135deg, #6366f1 0%%, #4f46e5 50%%, #4338ca 100%%); padding: 32px 40px; text-align: center; }
  .header img { height: 32px; }
  .header h1 { color: #ffffff; font-size: 22px; font-weight: 700; margin: 16px 0 0; letter-spacing: -0.3px; }
  .header p { color: rgba(255,255,255,0.8); font-size: 14px; margin: 6px 0 0; }
  .body { padding: 40px; }
  .body h2 { color: #1e293b; font-size: 18px; font-weight: 600; margin: 0 0 16px; }
  .body p { color: #475569; font-size: 14px; line-height: 1.7; margin: 0 0 16px; }
  .info-table { width: 100%%; border-collapse: collapse; margin: 20px 0; }
  .info-table td { padding: 10px 0; font-size: 14px; color: #475569; border-bottom: 1px solid #f1f5f9; }
  .info-table td:first-child { font-weight: 600; color: #1e293b; width: 40%%; }
  .info-table tr:last-child td { border-bottom: none; }
  .total-row td { font-size: 16px !important; font-weight: 700 !important; color: #4f46e5 !important; padding-top: 16px !important; border-top: 2px solid #e2e8f0 !important; }
  .btn { display: inline-block; background: linear-gradient(135deg, #6366f1, #4f46e5); color: #ffffff !important; text-decoration: none; padding: 14px 36px; border-radius: 8px; font-size: 14px; font-weight: 600; letter-spacing: 0.3px; margin: 8px 0; }
  .btn:hover { opacity: 0.9; }
  .status-badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; text-transform: uppercase; }
  .status-success { background: #dcfce7; color: #16a34a; }
  .status-failed { background: #fee2e2; color: #dc2626; }
  .status-pending { background: #fef3c7; color: #d97706; }
  .footer { background: #f8fafc; padding: 24px 40px; text-align: center; border-top: 1px solid #f1f5f9; }
  .footer p { color: #94a3b8; font-size: 12px; margin: 4px 0; }
  .footer a { color: #6366f1; text-decoration: none; }
  .divider { height: 1px; background: #e2e8f0; margin: 24px 0; }
  @media (max-width: 600px) {
    .wrapper { padding: 12px; }
    .header, .body, .footer { padding: 24px 20px; }
    .body h2 { font-size: 16px; }
  }
</style>
</head>
<body>
<div class="wrapper">
  <div class="card">
    <div class="header">
      <h1>TalentScale</h1>
      <p>Enterprise HR Assessment Platform</p>
    </div>
    %s
    <div class="footer">
      <p>© 2026 TalentScale. All rights reserved.</p>
      <p>Need help? <a href="mailto:support@talentscale.id">support@talentscale.id</a></p>
    </div>
  </div>
</div>
</body>
</html>`, title, content)
}

// ─── Invoice Created ─────────────────────────────────────────────────────────

func RenderInvoiceCreated(d InvoiceEmailData) string {
	content := fmt.Sprintf(`
    <div class="body">
      <h2>Invoice Created</h2>
      <p>Dear <strong>%s</strong>,</p>
      <p>A new invoice has been generated for your subscription purchase. Please complete your payment before the due date.</p>

      <table class="info-table">
        <tr><td>Invoice Number</td><td>%s</td></tr>
        <tr><td>Package</td><td>%s</td></tr>
        <tr><td>Duration</td><td>%s</td></tr>
        <tr><td>Subtotal</td><td>Rp %s</td></tr>
        <tr><td>Tax (11%% PPN)</td><td>Rp %s</td></tr>
        <tr class="total-row"><td>Total</td><td>Rp %s</td></tr>
      </table>

      <div class="divider"></div>
      <p style="text-align:center">
        <a href="%s" class="btn">Complete Payment</a>
      </p>
      <p style="text-align:center;color:#94a3b8;font-size:12px">Payment due: %s</p>
    </div>`,
		d.CompanyName, d.InvoiceNumber, d.PackageName, d.Duration,
		d.Subtotal, d.Tax, d.Total, d.PaymentURL, d.DueDate,
	)
	return baseLayout("Invoice Created — TalentScale", content)
}

// ─── Payment Success ─────────────────────────────────────────────────────────

func RenderPaymentSuccess(d PaymentEmailData) string {
	content := fmt.Sprintf(`
    <div class="body">
      <h2>Payment Successful ✓</h2>
      <p>Dear <strong>%s</strong>,</p>
      <p>Your payment has been confirmed and your subscription is now active!</p>

      <table class="info-table">
        <tr><td>Invoice</td><td>%s</td></tr>
        <tr><td>Package</td><td>%s</td></tr>
        <tr><td>Amount Paid</td><td>Rp %s</td></tr>
        <tr><td>Payment Method</td><td>%s</td></tr>
        <tr><td>Paid At</td><td>%s</td></tr>
        <tr><td>Status</td><td><span class="status-badge status-success">SUCCESS</span></td></tr>
      </table>

      <div class="divider"></div>
      <p style="text-align:center">
        <a href="https://app.talentscale.id/dashboard" class="btn">Go to Dashboard</a>
      </p>
    </div>`,
		d.CompanyName, d.InvoiceNumber, d.PackageName,
		d.Amount, d.PaymentMethod, d.PaidAt,
	)
	return baseLayout("Payment Successful — TalentScale", content)
}

// ─── Payment Failed ──────────────────────────────────────────────────────────

func RenderPaymentFailed(d PaymentEmailData) string {
	content := fmt.Sprintf(`
    <div class="body">
      <h2>Payment Failed</h2>
      <p>Dear <strong>%s</strong>,</p>
      <p>Unfortunately, your payment could not be processed. Please try again or use a different payment method.</p>

      <table class="info-table">
        <tr><td>Invoice</td><td>%s</td></tr>
        <tr><td>Package</td><td>%s</td></tr>
        <tr><td>Amount</td><td>Rp %s</td></tr>
        <tr><td>Status</td><td><span class="status-badge status-failed">FAILED</span></td></tr>
      </table>

      <div class="divider"></div>
      <p style="text-align:center">
        <a href="https://app.talentscale.id/billing" class="btn">Retry Payment</a>
      </p>
      <p style="text-align:center;color:#94a3b8;font-size:12px">If you continue to experience issues, contact our support team.</p>
    </div>`,
		d.CompanyName, d.InvoiceNumber, d.PackageName, d.Amount,
	)
	return baseLayout("Payment Failed — TalentScale", content)
}

// ─── Subscription Activated ──────────────────────────────────────────────────

func RenderSubscriptionActivated(d SubscriptionEmailData) string {
	content := fmt.Sprintf(`
    <div class="body">
      <h2>Subscription Activated 🎉</h2>
      <p>Dear <strong>%s</strong>,</p>
      <p>Your subscription has been activated. You now have full access to all features included in your plan.</p>

      <table class="info-table">
        <tr><td>Package</td><td>%s</td></tr>
        <tr><td>Start Date</td><td>%s</td></tr>
        <tr><td>End Date</td><td>%s</td></tr>
        <tr><td>Candidate Quota</td><td>%d candidates</td></tr>
        <tr><td>Status</td><td><span class="status-badge status-success">ACTIVE</span></td></tr>
      </table>

      <div class="divider"></div>
      <p style="text-align:center">
        <a href="%s" class="btn">Start Using TalentScale</a>
      </p>
    </div>`,
		d.CompanyName, d.PackageName,
		d.StartedAt, d.EndAt, d.Quota, d.DashboardURL,
	)
	return baseLayout("Subscription Activated — TalentScale", content)
}
