'use client';

import { Check, ExternalLink } from 'lucide-react';
import { HOME_LAYOUTS, HOME_LAYOUT_META, type HomeLayout } from '@/lib/home/layouts';

// Tiny schematic of each design — enough to tell them apart at a glance.
function Wireframe({ layout }: { layout: HomeLayout }) {
  const bar = 'rounded-sm';
  const muted = { background: 'var(--color-border)' };
  const accent = { background: 'var(--color-cta)' };

  if (layout === 'home2') {
    return (
      <div className="flex h-full flex-col gap-1.5 p-2">
        <div className={`h-1.5 ${bar}`} style={{ background: 'var(--color-text)' }} />
        <div className="flex items-center gap-1.5">
          <div className={`h-2 w-6 ${bar}`} style={accent} />
          <div className={`h-2.5 flex-1 rounded-full`} style={muted} />
        </div>
        <div className="grid flex-1 grid-cols-3 gap-1">
          <div className="col-span-2 rounded" style={accent} />
          <div className="grid grid-rows-2 gap-1">
            <div className="rounded" style={muted} />
            <div className="rounded" style={muted} />
          </div>
        </div>
        <div className="grid grid-cols-6 gap-1">
          {Array.from({ length: 6 }, (_, i) => <div key={i} className="aspect-square rounded-full" style={muted} />)}
        </div>
        <div className="grid grid-cols-4 gap-1">
          {Array.from({ length: 4 }, (_, i) => <div key={i} className="h-3 rounded" style={muted} />)}
        </div>
      </div>
    );
  }

  if (layout === 'home3') {
    return (
      <div className="flex h-full flex-col gap-1.5">
        <div className="h-1" style={{ background: 'var(--color-text)' }} />
        <div className="flex items-center justify-between px-2">
          <div className={`h-1.5 w-6 ${bar}`} style={{ background: 'var(--color-text)' }} />
          <div className="flex gap-1">
            {Array.from({ length: 3 }, (_, i) => <div key={i} className={`h-1 w-3 ${bar}`} style={muted} />)}
          </div>
          <div className={`h-1.5 w-5 rounded-full`} style={muted} />
        </div>
        <div className="grid flex-1 grid-cols-3 gap-1 p-1.5" style={{ background: 'var(--color-text)' }}>
          <div className="col-span-2 rounded-md" style={{ background: 'color-mix(in srgb, var(--color-cta) 45%, transparent)' }} />
          <div className="flex flex-col gap-1">
            <div className="flex-1 rounded-md bg-white/20" />
            <div className="h-2 rounded-md bg-white/10" />
          </div>
        </div>
        <div className="grid grid-cols-5 gap-1 px-2 pb-2">
          {Array.from({ length: 5 }, (_, i) => <div key={i} className="h-5 rounded-md" style={{ background: 'var(--color-text)', opacity: 0.8 }} />)}
        </div>
      </div>
    );
  }

  return (
    <div className="flex h-full flex-col gap-1.5 p-2">
      <div className="flex items-center gap-1.5">
        <div className={`h-2 w-6 ${bar}`} style={accent} />
        <div className="flex flex-1 justify-center gap-1">
          {Array.from({ length: 4 }, (_, i) => <div key={i} className={`h-1 w-3 ${bar}`} style={muted} />)}
        </div>
      </div>
      <div className="grid flex-1 grid-cols-2 items-center gap-1.5 rounded p-1.5" style={{ background: 'color-mix(in srgb, var(--color-cta) 12%, transparent)' }}>
        <div className="flex flex-col gap-1">
          <div className="h-1.5 w-full rounded-sm" style={{ background: 'var(--color-text)' }} />
          <div className="h-1 w-2/3 rounded-sm" style={muted} />
          <div className="h-1.5 w-8 rounded-full" style={accent} />
        </div>
        <div className="h-full rounded" style={muted} />
      </div>
      <div className="grid grid-cols-5 gap-1">
        {Array.from({ length: 5 }, (_, i) => <div key={i} className="h-3 rounded" style={muted} />)}
      </div>
      <div className="grid grid-cols-4 gap-1">
        {Array.from({ length: 4 }, (_, i) => <div key={i} className="h-3 rounded" style={muted} />)}
      </div>
    </div>
  );
}

interface HomeLayoutPickerProps {
  value: string;
  onChange: (layout: HomeLayout) => void;
  disabled?: boolean;
}

// Settings → General: pick the storefront design (homepage + header +
// footer). "Preview" opens the live store in that design in a new tab, for
// this admin only (/api/home-preview) — nothing changes for customers until
// the settings are saved.
export default function HomeLayoutPicker({ value, onChange, disabled }: HomeLayoutPickerProps) {
  return (
    <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
      {HOME_LAYOUTS.map((layout) => {
        const meta = HOME_LAYOUT_META[layout];
        const selected = value === layout;
        return (
          <div
            key={layout}
            className="relative flex flex-col overflow-hidden rounded-xl transition-all"
            style={{
              background: 'var(--color-surface)',
              border: `2px solid ${selected ? 'var(--color-cta)' : 'var(--color-border)'}`,
              boxShadow: selected ? '0 0 0 4px color-mix(in srgb, var(--color-cta) 15%, transparent)' : 'none',
            }}
          >
            <button
              type="button"
              onClick={() => onChange(layout)}
              disabled={disabled}
              aria-pressed={selected}
              className="text-start disabled:cursor-not-allowed"
            >
              <div className="aspect-[4/3] overflow-hidden" style={{ background: 'var(--color-surface-alt)' }}>
                <Wireframe layout={layout} />
              </div>
              <div className="p-4">
                <div className="flex items-center justify-between gap-2">
                  <span className="font-semibold" style={{ color: 'var(--color-text)' }}>{meta.name}</span>
                  {selected && (
                    <span
                      className="inline-flex items-center gap-1 rounded-full px-2 py-0.5 text-xs font-semibold text-white"
                      style={{ background: 'var(--color-cta)' }}
                    >
                      <Check size={12} /> Selected
                    </span>
                  )}
                </div>
                <p className="text-xs mt-0.5" style={{ color: 'var(--color-cta)' }}>{meta.tagline}</p>
                <p className="text-xs mt-2 leading-relaxed" style={{ color: 'var(--color-text-tertiary)' }}>{meta.description}</p>
              </div>
            </button>
            <div className="mt-auto px-4 pb-4">
              <a
                href={`/api/home-preview?layout=${layout}`}
                target="_blank"
                rel="noopener"
                className="inline-flex w-full items-center justify-center gap-2 rounded-lg px-3 py-2 text-sm font-medium transition-all hover:opacity-80"
                style={{ background: 'var(--color-surface-alt)', border: '1px solid var(--color-border)', color: 'var(--color-text)' }}
              >
                <ExternalLink size={14} /> Preview
              </a>
            </div>
          </div>
        );
      })}
    </div>
  );
}
