'use client';

import { ShoppingCart, Star, Search, Heart, Info } from 'lucide-react';
import { buildThemeCss, type StorefrontTheme } from '@/lib/theme/storefrontTheme';

// A miniature storefront rendered with the draft theme. It uses exactly the
// same buildThemeCss() as the live store (scoped to [data-theme-preview]),
// and inline styles on the storefront tokens — the admin bundle doesn't load
// the storefront's utility classes, so classes like bg-primary aren't used here.
// Palette / radius vars have Tailwind-default fallbacks for the unchanged case.

const v = {
  primary: 'var(--primary)',
  primaryFg: 'var(--primary-foreground)',
  secondary: 'var(--secondary)',
  bg: 'var(--background)',
  fg: 'var(--foreground)',
  muted: 'var(--gray)',
  border: 'var(--border)',
  section: 'var(--light-gray)',
  footer: 'var(--dark-bg)',
  gradient: 'linear-gradient(135deg, var(--primary), var(--secondary))',
  heading: 'var(--sf-font-heading), ui-sans-serif, system-ui, sans-serif',
  body: 'var(--sf-font-body), ui-sans-serif, system-ui, sans-serif',
  rMd: 'var(--radius-md, 0.375rem)',
  rLg: 'var(--radius-lg, 0.5rem)',
  rXl: 'var(--radius-xl, 0.75rem)',
  r2xl: 'var(--radius-2xl, 1rem)',
  green50: 'var(--color-green-50, #f0fdf4)',
  green700: 'var(--color-green-700, #008138)',
  red500: 'var(--color-red-500, #fb2c36)',
  red50: 'var(--color-red-50, #fef2f2)',
  yellow400: 'var(--color-yellow-400, #fac800)',
  blue50: 'var(--color-blue-50, #eff6ff)',
  blue700: 'var(--color-blue-700, #1447e6)',
  gray100: 'var(--color-gray-100, #f3f4f6)',
  gray400: 'var(--color-gray-400, #99a1af)',
};

export default function ThemePreview({ theme, fontClassName }: { theme: StorefrontTheme; fontClassName: string }) {
  return (
    <div
      data-theme-preview
      className={`${fontClassName} overflow-hidden border`}
      style={{ background: v.bg, color: v.fg, fontFamily: v.body, borderColor: v.border, borderRadius: v.rXl }}
    >
      <style dangerouslySetInnerHTML={{ __html: buildThemeCss(theme, '[data-theme-preview]') }} />

      {/* Top bar + header */}
      <div className="px-4 py-1.5 text-[11px] text-white" style={{ background: v.secondary }}>
        Free delivery on orders above 1,000
      </div>
      <div className="flex items-center justify-between gap-3 px-4 py-3 border-b" style={{ background: '#FFFFFF', borderColor: v.border }}>
        <span className="text-lg font-extrabold bg-clip-text text-transparent" style={{ backgroundImage: v.gradient, fontFamily: v.heading }}>
          YourStore
        </span>
        <div className="hidden sm:flex items-center gap-4 text-xs font-medium">
          <span style={{ color: v.primary }}>Home</span>
          <span>Shop</span>
          <span>Blog</span>
        </div>
        <div className="flex items-center gap-3" style={{ color: v.fg }}>
          <Search size={15} />
          <Heart size={15} />
          <span className="relative">
            <ShoppingCart size={16} />
            <span className="absolute -top-1.5 -right-2 w-4 h-4 rounded-full text-[9px] font-bold flex items-center justify-center" style={{ background: v.primary, color: v.primaryFg }}>3</span>
          </span>
        </div>
      </div>

      <div className="p-4 space-y-4">
        {/* Hero */}
        <div className="p-5 text-white" style={{ backgroundImage: v.gradient, borderRadius: v.r2xl }}>
          <p className="text-[10px] font-semibold uppercase tracking-widest opacity-80">New season</p>
          <h3 className="text-xl font-bold leading-tight mt-1 mb-3" style={{ fontFamily: v.heading }}>Fresh deals, every day</h3>
          <div className="flex flex-wrap gap-2">
            <span className="px-3 py-1.5 text-xs font-semibold" style={{ background: '#FFFFFF', color: v.primary, borderRadius: v.rLg }}>Shop now</span>
            <span className="px-3 py-1.5 text-xs font-semibold border border-white/60" style={{ borderRadius: v.rLg }}>View offers</span>
          </div>
        </div>

        {/* Product cards */}
        <div className="grid grid-cols-2 gap-3">
          {[
            { name: 'Organic Honey 500g', price: '1,250', old: '1,600', badge: 'danger' as const },
            { name: 'Cotton Casual Shirt', price: '2,499', old: null, badge: 'success' as const },
          ].map((p) => (
            <div key={p.name} className="border overflow-hidden" style={{ background: '#FFFFFF', borderColor: v.border, borderRadius: v.rXl }}>
              <div className="relative h-20" style={{ background: v.section }}>
                <span
                  className="absolute top-2 left-2 px-1.5 py-0.5 text-[9px] font-bold"
                  style={p.badge === 'danger'
                    ? { background: v.red500, color: '#FFFFFF', borderRadius: v.rMd }
                    : { background: v.green50, color: v.green700, borderRadius: v.rMd }}
                >
                  {p.badge === 'danger' ? '-22%' : 'In stock'}
                </span>
              </div>
              <div className="p-2.5">
                <div className="flex gap-0.5 mb-1">
                  {[0, 1, 2, 3, 4].map((i) => (
                    <Star key={i} size={10} style={{ color: i < 4 ? v.yellow400 : v.gray400, fill: i < 4 ? v.yellow400 : 'none' }} />
                  ))}
                </div>
                <p className="text-xs font-semibold leading-snug line-clamp-1" style={{ fontFamily: v.heading }}>{p.name}</p>
                <p className="mt-1 text-sm font-bold" style={{ color: v.primary }}>
                  Rs {p.price}
                  {p.old && <span className="ml-1.5 text-[10px] font-normal line-through" style={{ color: v.muted }}>Rs {p.old}</span>}
                </p>
                <span className="mt-2 block text-center py-1.5 text-[11px] font-semibold" style={{ background: v.primary, color: v.primaryFg, borderRadius: v.rLg }}>
                  Add to cart
                </span>
              </div>
            </div>
          ))}
        </div>

        {/* Form + notices */}
        <div className="space-y-2">
          <div className="border px-3 py-2 text-xs" style={{ borderColor: v.border, borderRadius: v.rLg, color: v.muted, background: '#FFFFFF' }}>
            you@example.com
          </div>
          <div className="flex items-start gap-2 px-3 py-2 text-[11px]" style={{ background: v.blue50, color: v.blue700, borderRadius: v.rLg }}>
            <Info size={13} className="shrink-0 mt-px" /> Your order ships within 2 working days.
          </div>
          <div className="px-3 py-2 text-[11px]" style={{ background: v.red50, color: v.red500, borderRadius: v.rLg }}>
            Coupon code is invalid or expired.
          </div>
          <p className="text-xs" style={{ color: v.muted }}>
            Body text in the chosen font. <span style={{ color: v.primary, fontWeight: 600 }}>A link</span> and a{' '}
            <span className="px-1.5 py-0.5 text-[10px]" style={{ background: v.gray100, borderRadius: v.rMd }}>tag</span>.
          </p>
        </div>
      </div>

      {/* Footer */}
      <div className="px-4 py-3 text-[11px] text-white/80" style={{ background: v.footer }}>
        <span className="font-bold text-white" style={{ fontFamily: v.heading }}>YourStore</span> · Quick links · Contact
      </div>
    </div>
  );
}
