"use client";

import { cn } from "@/lib/utils";
import { Monitor, Tablet, Smartphone, Sparkles } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { useState } from "react";

type PreviewView = "desktop" | "tablet" | "mobile";

interface BannerPreviewProps {
  data: {
    title?: string;
    subtitle?: string | null;
    description?: string | null;
    desktop_image?: string | null;
    tablet_image?: string | null;
    mobile_image?: string | null;
    primary_button_text?: string | null;
    secondary_button_text?: string | null;
  };
  /** When provided, view is controlled by parent (no internal header/toggles) */
  activeView?: PreviewView;
  onViewChange?: (view: PreviewView) => void;
  /** When true (e.g. create banner), show highlighted areas for Tag, Description, and button placement */
  showPlacementHints?: boolean;
}

export function BannerPreview({
  data,
  activeView: controlledView,
  onViewChange,
  showPlacementHints = false,
}: BannerPreviewProps) {
  const [internalView, setInternalView] = useState<PreviewView>("desktop");
  const activeView = controlledView ?? internalView;
  const setActiveView = onViewChange ?? setInternalView;
  const showHeader = controlledView === undefined && onViewChange === undefined;

  const views = [
    {
      id: "desktop" as const,
      name: "Desktop View",
      icon: Monitor,
      width: "100%",
      height: "420px",
    },
    {
      id: "tablet" as const,
      name: "Tablet View",
      icon: Tablet,
      width: "70%",
      height: "300px",
    },
    {
      id: "mobile" as const,
      name: "Mobile View",
      icon: Smartphone,
      width: "350px",
      height: "450px",
    },
  ];

  const currentView = views.find((v) => v.id === activeView) ?? views[0];
  const viewImage =
    (activeView === "desktop" && data.desktop_image) ||
    (activeView === "tablet" && (data.tablet_image || data.desktop_image)) ||
    (activeView === "mobile" &&
      (data.mobile_image || data.tablet_image || data.desktop_image));
  const viewImageSrc =
    (activeView === "mobile"
      ? data.mobile_image
      : activeView === "tablet"
        ? data.tablet_image
        : data.desktop_image) ||
    data.desktop_image ||
    "";

  return (
    <div className="space-y-6">
      {showHeader && (
        <div className="flex items-center justify-between border-b pb-4">
          <h2 className="text-lg font-semibold">Live Previews</h2>
          <div className="flex items-center gap-2 bg-muted/30 p-1 rounded-lg">
            {views.map((view) => {
              const Icon = view.icon;
              return (
                <Button
                  key={view.id}
                  variant={activeView === view.id ? "secondary" : "ghost"}
                  size="sm"
                  className="h-8 w-8 p-0 rounded-md"
                  onClick={() => setActiveView(view.id)}
                  title={view.name}
                >
                  <Icon className="h-4 w-4" />
                </Button>
              );
            })}
          </div>
        </div>
      )}

      <div
        className={cn(
          "w-full flex flex-col items-center",
          showHeader ? "pt-2" : ""
        )}
      >
        {/* Single fixed header row - same position for all views */}
        <div className="w-full mb-3 flex items-center justify-between px-2">
          <span className="text-xs font-medium text-muted-foreground uppercase tracking-wider flex items-center gap-2">
            <currentView.icon className="h-3 w-3" />
            {currentView.name}
          </span>
          <span className="text-[10px] text-muted-foreground">
            {currentView.id === "desktop"
              ? "> 1024px"
              : currentView.id === "tablet"
                ? "768px - 1023px"
                : "< 768px"}
          </span>
        </div>

        {/* Single banner container - dimensions and content by activeView */}
        <div
          className="relative rounded-xl overflow-hidden shadow-2xl border border-border/50 bg-black group"
          style={{ width: currentView.width, height: currentView.height }}
        >
          <div className="absolute inset-0 bg-linear-to-t from-black/80 via-black/40 to-transparent z-10" />

          {viewImage ? (
            <img
              src={viewImageSrc}
              alt="Banner Draft"
              className="w-full h-full object-cover"
            />
          ) : (
            <div className="w-full h-full flex flex-col items-center justify-center bg-muted/20 text-muted-foreground gap-3">
              <currentView.icon className="h-10 w-10 opacity-20" />
              <p className="text-xs opacity-50 italic">
                Upload an image to preview
              </p>
            </div>
          )}

          <div
            className={cn(
              "absolute inset-0 z-20 flex flex-col justify-center p-8 md:p-10 text-white",
              currentView.id === "mobile"
                ? "items-center text-center"
                : "items-start"
            )}
          >
            <div
              className={cn(
                "max-w-[85%] md:max-w-[65%] space-y-4",
                currentView.id === "mobile" && "max-w-full"
              )}
            >
              {/* Tag placement — show badge or highlighted placeholder */}
              {(data.subtitle || showPlacementHints) &&
                (data.subtitle ? (
                  <Badge
                    variant="secondary"
                    className="rounded-lg bg-white/15 text-white border border-white/25 backdrop-blur-sm px-4 py-2 font-medium text-xs uppercase tracking-wider"
                  >
                    <Sparkles className="h-3.5 w-3.5 mr-2 opacity-90" />
                    {data.subtitle}
                  </Badge>
                ) : showPlacementHints ? (
                  <Badge
                    variant="secondary"
                    className="rounded-lg bg-white/15 text-white border border-white/25 backdrop-blur-sm px-4 py-2 font-medium text-xs uppercase tracking-wider"
                  >
                    <Sparkles className="h-3.5 w-3.5 mr-2 opacity-90" />
                    Tag
                  </Badge>
                ) : null)}

              {/* Title — always shown with placeholder or value */}
              <h3
                className={cn(
                  "font-bold text-white leading-tight",
                  currentView.id === "mobile"
                    ? "text-2xl"
                    : "text-3xl md:text-4xl lg:text-5xl"
                )}
              >
                {data.title || "Banner Title"}
              </h3>

              {/* Description placement — show text or highlighted placeholder */}
              {(data.description || showPlacementHints) &&
                (data.description ? (
                  <p
                    className={cn(
                      "text-sm md:text-base text-white/90 leading-relaxed",
                      currentView.id === "mobile" ? "max-w-full" : "max-w-xl"
                    )}
                  >
                    {data.description}
                  </p>
                ) : showPlacementHints ? (
                  <p className="text-sm md:text-base text-white/70 leading-relaxed">
                    Description
                  </p>
                ) : null)}

              {/* Primary / Secondary button placement — show buttons or highlighted placeholders */}
              {(data.primary_button_text ||
                data.secondary_button_text ||
                showPlacementHints) && (
                <div
                  className={cn(
                    "flex flex-wrap gap-3 pt-4",
                    currentView.id === "mobile" ? "flex-col" : "flex-row",
                    showPlacementHints && "gap-4"
                  )}
                >
                  {data.primary_button_text ? (
                    <Button
                      size={currentView.id === "mobile" ? "sm" : "default"}
                      className="rounded-xl px-6 font-semibold shadow-lg bg-primary hover:bg-primary/90 text-primary-foreground border-0"
                    >
                      {data.primary_button_text}
                    </Button>
                  ) : showPlacementHints ? (
                    <span className="inline-flex rounded-xl px-6 py-2.5 text-sm font-semibold shadow-lg bg-primary text-primary-foreground border-0 cursor-default">
                      Primary button
                    </span>
                  ) : null}
                  {data.secondary_button_text ? (
                    <Button
                      variant="outline"
                      size={currentView.id === "mobile" ? "sm" : "default"}
                      className="rounded-xl px-6 font-semibold bg-white/10 hover:bg-white/20 border-white/30 text-white backdrop-blur-sm"
                    >
                      {data.secondary_button_text}
                    </Button>
                  ) : showPlacementHints ? (
                    <span className="inline-flex rounded-xl px-6 py-2.5 text-sm font-semibold bg-white/10 border border-white/30 text-white backdrop-blur-sm cursor-default">
                      Secondary button
                    </span>
                  ) : null}
                </div>
              )}
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}
