"use client";

import React, { useState } from "react";
import { Button } from "@/components/ui/Button";
import { Download, Copy, ChevronDown, ChevronUp } from "lucide-react";
import { downloadImage, copyToClipboard } from "@/lib/utils/export";
import { getImageUrl, getImageUrlFallback, formatRelativeDate } from "@/lib/utils/formatters";
import { toast } from "react-hot-toast";
import type { GenerateResponse, MatrixGenerateResponse } from "@/types/api";

interface AdPreviewProps {
  ad: GenerateResponse | MatrixGenerateResponse;
}

export const AdPreview: React.FC<AdPreviewProps> = ({ ad }) => {
  const [imageErrors, setImageErrors] = useState<Record<number, boolean>>({});
  const [isBodyStoryExpanded, setIsBodyStoryExpanded] = useState(false);

  // Debug: Log image details to verify uniqueness
  React.useEffect(() => {
    if (ad.images && ad.images.length > 1) {
      console.log(`AdPreview: Displaying ${ad.images.length} images`);
      console.log(`📅 Generation timestamp: ${ad.created_at || 'N/A'}`);
      ad.images.forEach((img, idx) => {
        console.log(`  Image ${idx + 1}:`, {
          filename: img.filename,
          image_url: img.image_url?.substring(0, 50) + '...',
          seed: img.seed,
        });
      });
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [ad.id, ad.images?.length, ad.created_at]); // Use stable dependencies: id, length, and timestamp

  const handleDownloadImage = async (
    imageUrl: string | null | undefined,
    filename: string | null | undefined,
    r2Url?: string | null
  ) => {
    if (!imageUrl && !filename && !r2Url && !ad.id) {
      toast.error("No image URL available");
      return;
    }
    
    try {
      const url = getImageUrl(imageUrl, filename, r2Url);
      // Use proxy endpoint with ad ID to avoid CORS issues
      await downloadImage(url, filename || `ad-${ad.id}.png`, ad.id);
      toast.success("Image downloaded");
    } catch (error) {
      toast.error("Failed to download image");
    }
  };

  const handleCopyText = async (text: string, label: string) => {
    try {
      await copyToClipboard(text);
      toast.success(`${label} copied`);
    } catch (error) {
      toast.error("Failed to copy");
    }
  };

  const handleImageError = (index: number, image: { image_url?: string | null; filename?: string | null; r2_url?: string | null }) => {
    if (!imageErrors[index]) {
      const { fallback } = getImageUrlFallback(image.image_url, image.filename, image.r2_url);
      if (fallback) {
        setImageErrors((prev) => ({ ...prev, [index]: true }));
      }
    }
  };

  return (
    <div className="space-y-6">
      {/* Images */}
      {ad.images && ad.images.length > 0 && (
        <div className="space-y-4">
          {ad.images.length > 1 && (
            <div className="flex items-center justify-between">
              <h3 className="text-sm font-semibold text-gray-700">
                Images ({ad.images.length})
              </h3>
              {ad.created_at && (
                <span className="text-xs text-gray-500 font-medium">
                  Generated {formatRelativeDate(ad.created_at)}
                </span>
              )}
            </div>
          )}
          {ad.images.length === 1 ? (
            // Single image - full width with better styling
            <div className="bg-white rounded-2xl shadow-lg shadow-blue-100/50 overflow-hidden ring-1 ring-blue-100">
              {(() => {
                const image = ad.images[0];
                const { primary, fallback } = getImageUrlFallback(image.image_url, image.filename, image.r2_url);
                const imageUrl = imageErrors[0] ? fallback : (primary || fallback);
                
                return imageUrl ? (
                  <div className="relative group">
                    <img
                      src={imageUrl}
                      alt={ad.headline || "Ad image"}
                      className="w-full h-auto"
                      onError={() => handleImageError(0, image)}
                    />
                    {ad.created_at && (
                      <div className="absolute top-4 left-4 bg-black/60 backdrop-blur-sm text-white text-xs font-medium px-2 py-1 rounded-md">
                        {formatRelativeDate(ad.created_at)}
                      </div>
                    )}
                    <div className="absolute top-4 right-4 opacity-0 group-hover:opacity-100 transition-opacity">
                      <div className="relative group/btn">
                        <Button
                          variant="primary"
                          size="sm"
                          onClick={() => handleDownloadImage(image.image_url, image.filename, image.r2_url)}
                          className="shadow-lg"
                        >
                          <Download className="h-4 w-4" />
                        </Button>
                        <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover/btn:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                          Download Image
                        </span>
                      </div>
                    </div>
                  </div>
                ) : (
                  <div className="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-2xl aspect-square flex items-center justify-center ring-1 ring-blue-100">
                    <div className="text-blue-300 text-center">
                      <svg className="w-16 h-16 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
                      </svg>
                      <p className="text-sm">No image</p>
                    </div>
                  </div>
                );
              })()}
              {ad.images[0].error && (
                <div className="p-4 bg-red-50 border-t border-red-200">
                  <p className="text-sm text-red-800">Error: {ad.images[0].error}</p>
                </div>
              )}
            </div>
          ) : (
            // Multiple images - grid layout
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {ad.images.map((image, index) => {
                const { primary, fallback } = getImageUrlFallback(image.image_url, image.filename, image.r2_url);
                const imageUrl = imageErrors[index] ? fallback : (primary || fallback);
                
                // Use unique key based on filename or URL to prevent duplicate rendering
                const uniqueKey = image.filename || image.image_url || `image-${index}`;
                
                return (
                  <div key={uniqueKey} className="bg-white rounded-2xl shadow-lg shadow-blue-100/50 overflow-hidden ring-1 ring-blue-100">
                    {imageUrl ? (
                      <div className="relative group">
                        <img
                          src={imageUrl}
                          alt={`Ad image ${index + 1}`}
                          className="w-full h-auto"
                          onError={() => handleImageError(index, image)}
                        />
                        {/* Image number badge */}
                        <div className="absolute top-2 left-2 bg-black/60 backdrop-blur-sm text-white text-xs font-bold px-2 py-1 rounded-md">
                          Image {index + 1}
                        </div>
                        <div className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity">
                          <div className="relative group/btn">
                            <Button
                              variant="primary"
                              size="sm"
                              onClick={() => handleDownloadImage(image.image_url, image.filename, image.r2_url)}
                              className="shadow-lg"
                            >
                              <Download className="h-4 w-4" />
                            </Button>
                            <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-gray-800 text-white text-xs rounded opacity-0 group-hover/btn:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                              Download
                            </span>
                          </div>
                        </div>
                      </div>
                    ) : (
                      <div className="bg-gradient-to-br from-blue-50 to-cyan-50 aspect-square flex items-center justify-center">
                        <div className="text-blue-300 text-center">
                          <svg className="w-12 h-12 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={1.5} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
                          </svg>
                          <p className="text-xs">No image</p>
                        </div>
                      </div>
                    )}
                    {image.error && (
                      <div className="p-3 bg-red-50 border-t border-red-200">
                        <p className="text-xs text-red-800">Error: {image.error}</p>
                      </div>
                    )}
                    {/* Image metadata footer */}
                    <div className="p-2 bg-gray-50 border-t border-gray-100 flex items-center justify-between text-xs">
                      <span className="text-gray-500">
                        {image.seed && `Seed: ${image.seed}`}
                      </span>
                      {image.filename && (
                        <span className="text-gray-400 font-mono text-[10px] truncate max-w-[150px]">
                          {image.filename.split('_').pop()?.split('.')[0]}
                        </span>
                      )}
                    </div>
                  </div>
                );
              })}
            </div>
          )}
        </div>
      )}

      {/* Ad Copy Section */}
      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {/* Left Column - Main Copy */}
        <div className="space-y-5">
          {/* Title */}
          {ad.title && (
            <div className="bg-white rounded-2xl shadow-md p-6 border-l-4 border-indigo-500">
              <div className="flex items-start justify-between gap-4 mb-3">
                <h3 className="text-xs font-bold text-indigo-600 uppercase tracking-wider">Title</h3>
                <div className="relative group">
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => handleCopyText(ad.title!, "Title")}
                    className="text-indigo-500 hover:bg-indigo-50"
                  >
                    <Copy className="h-4 w-4" />
                  </Button>
                  <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-indigo-600 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                    Copy Title
                  </span>
                </div>
              </div>
              <p className="text-lg font-semibold text-gray-800">{ad.title}</p>
            </div>
          )}

          {/* Description */}
          {ad.description && (
            <div className="bg-white rounded-2xl shadow-md p-6 border-l-4 border-violet-500">
              <div className="flex items-start justify-between gap-4 mb-3">
                <h3 className="text-xs font-bold text-violet-600 uppercase tracking-wider">Description</h3>
                <div className="relative group">
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => handleCopyText(ad.description!, "Description")}
                    className="text-violet-500 hover:bg-violet-50"
                  >
                    <Copy className="h-4 w-4" />
                  </Button>
                  <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-violet-600 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                    Copy Description
                  </span>
                </div>
              </div>
              <p className="text-gray-700 leading-relaxed">{ad.description}</p>
            </div>
          )}

          {/* Body Story */}
          {ad.body_story && (
            <div className="bg-white rounded-2xl shadow-md p-6 border-l-4 border-amber-500">
              <div className="flex items-start justify-between gap-4 mb-3">
                <h3 className="text-xs font-bold text-amber-600 uppercase tracking-wider">Body Story</h3>
                <div className="flex items-center gap-2">
                  <div className="relative group">
                    <Button
                      variant="ghost"
                      size="sm"
                      onClick={() => handleCopyText(ad.body_story!, "Body Story")}
                      className="text-amber-500 hover:bg-amber-50"
                    >
                      <Copy className="h-4 w-4" />
                    </Button>
                    <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-amber-600 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                      Copy Story
                    </span>
                  </div>
                </div>
              </div>
              <div className="relative">
                <p 
                  className={`text-gray-700 whitespace-pre-line leading-relaxed transition-all duration-300 ${
                    !isBodyStoryExpanded ? 'line-clamp-3' : ''
                  }`}
                >
                  {ad.body_story}
                </p>
                {ad.body_story.split('\n').length > 3 || ad.body_story.length > 200 ? (
                  <button
                    onClick={() => setIsBodyStoryExpanded(!isBodyStoryExpanded)}
                    className="mt-2 flex items-center gap-1 text-amber-600 hover:text-amber-700 text-sm font-medium transition-colors"
                  >
                    {isBodyStoryExpanded ? (
                      <>
                        <span>Show less</span>
                        <ChevronUp className="h-4 w-4" />
                      </>
                    ) : (
                      <>
                        <span>Read more</span>
                        <ChevronDown className="h-4 w-4" />
                      </>
                    )}
                  </button>
                ) : null}
              </div>
            </div>
          )}
        </div>

        {/* Right Column - CTA and Strategy */}
        <div className="space-y-5">
          {/* CTA */}
          {ad.cta && (
            <div className="bg-gradient-to-r from-emerald-50 to-teal-50 rounded-2xl shadow-md p-6 border border-emerald-200">
              <div className="flex items-start justify-between gap-4 mb-3">
                <h3 className="text-xs font-bold text-emerald-600 uppercase tracking-wider">Call to Action</h3>
                <div className="relative group">
                  <Button
                    variant="ghost"
                    size="sm"
                    onClick={() => handleCopyText(ad.cta!, "CTA")}
                    className="text-emerald-600 hover:bg-emerald-100"
                  >
                    <Copy className="h-4 w-4" />
                  </Button>
                  <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-emerald-600 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                    Copy CTA
                  </span>
                </div>
              </div>
              <p className="text-xl font-bold bg-gradient-to-r from-emerald-600 to-teal-600 bg-clip-text text-transparent">{ad.cta}</p>
            </div>
          )}

          {/* Psychological Angle */}
          <div className="bg-gradient-to-br from-pink-50 via-purple-50 to-blue-50 rounded-2xl p-6 border border-purple-200 shadow-md">
            <div className="flex items-start justify-between gap-4 mb-3">
              <h3 className="text-xs font-bold text-purple-600 uppercase tracking-wider">🧠 Psychological Angle</h3>
              <div className="relative group">
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={() => handleCopyText(ad.psychological_angle, "Angle")}
                  className="text-purple-500 hover:bg-purple-100"
                >
                  <Copy className="h-4 w-4" />
                </Button>
                <span className="absolute -bottom-8 left-1/2 -translate-x-1/2 px-2 py-1 bg-purple-600 text-white text-xs rounded opacity-0 group-hover:opacity-100 transition-opacity whitespace-nowrap pointer-events-none z-10">
                  Copy Angle
                </span>
              </div>
            </div>
            <p className="text-gray-700 leading-relaxed">{ad.psychological_angle}</p>
            {ad.why_it_works && (
              <div className="mt-4 pt-4 border-t border-purple-200">
                <p className="text-xs font-bold text-purple-600 uppercase tracking-wider mb-2">💡 Why It Works</p>
                <p className="text-gray-600 text-sm leading-relaxed">{ad.why_it_works}</p>
              </div>
            )}
          </div>

          {/* Matrix Details */}
          {"matrix" in ad && ad.matrix && (
            <div className="bg-white rounded-2xl shadow-md p-6 border border-gray-200">
              <h3 className="text-xs font-bold text-gray-600 uppercase tracking-wider mb-4">Matrix Details</h3>
              <div className="space-y-4">
                <div className="bg-gradient-to-br from-blue-50 to-cyan-50 rounded-xl p-4 border border-blue-100">
                  <p className="text-blue-500 text-xs font-medium mb-1">Angle</p>
                  <p className="font-semibold text-gray-800">{ad.matrix.angle.name}</p>
                  <p className="text-xs text-gray-500 mt-1">{ad.matrix.angle.trigger}</p>
                </div>
                <div className="bg-gradient-to-br from-violet-50 to-purple-50 rounded-xl p-4 border border-violet-100">
                  <p className="text-violet-500 text-xs font-medium mb-1">Concept</p>
                  <p className="font-semibold text-gray-800">{ad.matrix.concept.name}</p>
                  <p className="text-xs text-gray-500 mt-1">{ad.matrix.concept.structure}</p>
                </div>
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  );
};
