import React from "react";
import {
  Box,
  CalendarDays,
  Check,
  CreditCard,
  Layers,
  Package,
  Repeat2,
  SlidersHorizontal,
  Wallet,
} from "lucide-react";

const Frame = ({ children }) => (
  <svg viewBox="0 0 280 112" fill="none" aria-hidden="true" focusable="false">
    {children}
  </svg>
);

// Explicit icon dimensions keep the shared image-picker SVG rules on the frame.
const Glyph = ({ icon: Icon, width = 24, height = width, ...props }) => (
  <Icon {...props} width={width} height={height} style={{ width, height }} />
);

const Tile = ({
  x,
  y = 24,
  width = 60,
  icon: Icon = Package,
  active = false,
}) => (
  <g>
    <rect
      x={x}
      y={y}
      width={width}
      height="64"
      rx="4"
      fill={active ? "currentColor" : "white"}
      fillOpacity={active ? ".12" : "1"}
      stroke="currentColor"
      strokeOpacity={active ? ".7" : ".25"}
    />
    <Glyph icon={Icon} x={x + width / 2 - 12} y={y + 12} strokeWidth="1.6" />
    <path
      d={`M${x + 14} ${y + 48}h${width - 28}`}
      stroke="currentColor"
      strokeOpacity=".3"
      strokeWidth="4"
      strokeLinecap="round"
    />
  </g>
);

export function ProductTypePreview({ type }) {
  if (type === "simple")
    return (
      <Frame>
        <Tile x={24} width={76} active />
        <path
          d="M112 56h28m-6-5 6 5-6 5"
          stroke="currentColor"
          strokeWidth="1.8"
        />
        {[166, 204, 242].map((x, i) => (
          <g key={x}>
            <circle
              cx={x}
              cy="56"
              r="14"
              fill="currentColor"
              fillOpacity={i === 0 ? ".14" : ".05"}
              stroke="currentColor"
              strokeOpacity=".35"
            />
            <Glyph icon={Check} x={x - 7} y="49" width={14} strokeWidth="2" />
            {i < 2 && (
              <path
                d={`M${x + 15} 56h8`}
                stroke="currentColor"
                strokeOpacity=".4"
              />
            )}
          </g>
        ))}
      </Frame>
    );
  if (type === "variable")
    return (
      <Frame>
        <Tile x={24} width={64} active />
        <path
          d="M88 56h28V32h28m-28 24v24h28"
          stroke="currentColor"
          strokeWidth="1.5"
          strokeOpacity=".5"
        />
        {[12, 62].map((y, i) => (
          <g key={y}>
            <rect
              x="144"
              y={y}
              width="110"
              height="38"
              rx="4"
              fill="white"
              stroke="currentColor"
              strokeOpacity=".35"
            />
            <circle
              cx="164"
              cy={y + 19}
              r="6"
              fill="currentColor"
              fillOpacity={i ? ".35" : ".8"}
            />
            <path
              d={`M180 ${y + 15}h54m-54 9h34`}
              stroke="currentColor"
              strokeOpacity=".3"
              strokeWidth="3"
              strokeLinecap="round"
            />
          </g>
        ))}
      </Frame>
    );
  if (type === "box" || type === "bundle")
    return (
      <Frame>
        <rect
          x="16"
          y="12"
          width="248"
          height="88"
          rx="4"
          stroke="currentColor"
          strokeOpacity=".25"
          strokeDasharray={type === "box" ? "4 4" : undefined}
        />
        <Tile x={30} y={24} icon={Box} active />
        <Tile x={110} y={24} icon={Package} active={type === "bundle"} />
        <Tile x={190} y={24} icon={Layers} active={type === "bundle"} />
        {type === "box" && (
          <Glyph icon={Check} x="74" y="18" width={18} fill="white" />
        )}
      </Frame>
    );
  return (
    <Frame>
      <rect
        x="70"
        y="20"
        width="140"
        height="76"
        rx="4"
        fill="white"
        stroke="currentColor"
        strokeOpacity=".45"
      />
      <Glyph icon={Wallet} x="88" y="39" width={36} strokeWidth="1.5" />
      <path
        d="M140 44h48m-48 12h32m-32 12h40"
        stroke="currentColor"
        strokeOpacity=".3"
        strokeWidth="4"
        strokeLinecap="round"
      />
      <circle
        cx="210"
        cy="30"
        r="16"
        fill="currentColor"
        fillOpacity=".12"
        stroke="currentColor"
        strokeOpacity=".45"
      />
      <Glyph icon={Check} x="202" y="22" width={16} strokeWidth="2" />
    </Frame>
  );
}

export function BillingModePreview({ mode }) {
  const flexible = mode !== "fixed";
  return (
    <Frame>
      <path
        d="M40 56h200"
        stroke="currentColor"
        strokeOpacity=".25"
        strokeWidth="2"
        strokeDasharray={flexible ? "5 5" : undefined}
      />
      {[52, 140, 228].map((x, i) => (
        <g key={x}>
          <rect
            x={x - 23}
            y="32"
            width="46"
            height="48"
            rx="4"
            fill="white"
            stroke="currentColor"
            strokeOpacity=".4"
          />
          <Glyph
            icon={mode === "full_flexible" ? CalendarDays : CreditCard}
            x={x - 11}
            y="45"
            width={22}
            strokeWidth="1.6"
          />
          {flexible && i === 2 && (
            <circle cx={x + 23} cy="32" r="9" fill="currentColor" />
          )}
        </g>
      ))}
      {flexible && (
        <Glyph
          icon={SlidersHorizontal}
          x="245"
          y="26"
          width={12}
          color="white"
          strokeWidth="2"
        />
      )}
    </Frame>
  );
}

export function ScheduleChoicePreview({ recurring = false, shipping = false }) {
  const Icon = shipping ? Package : CalendarDays;
  return (
    <Frame>
      <Tile x={34} width={80} icon={Icon} active />
      {recurring ? (
        <>
          <Glyph icon={Repeat2} x="130" y="42" width={28} strokeWidth="1.6" />
          <Tile x={174} width={72} icon={Icon} />
        </>
      ) : (
        <>
          <path
            d="M128 56h46"
            stroke="currentColor"
            strokeOpacity=".4"
            strokeWidth="2"
          />
          <circle
            cx="210"
            cy="56"
            r="23"
            fill="currentColor"
            fillOpacity=".1"
            stroke="currentColor"
            strokeOpacity=".4"
          />
          <Glyph icon={Check} x="197" y="43" width={26} strokeWidth="1.6" />
        </>
      )}
    </Frame>
  );
}
