import React, { useState } from 'react';
import { motion } from 'framer-motion';
import { Wand2, Download, Star } from 'lucide-react';
import { Button } from './ui/Button';

export const Gallery = ({ items, title = "Most Popular Printables", onRestore }) => {
    return (
        <section className="py-12">
            <div className="flex items-center gap-2 mb-8">
                <h2 className="text-2xl font-bold text-slate-900">{title}</h2>
                <Star className="text-yellow-400 fill-yellow-400" size={24} />
            </div>

            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                {items.map((item, index) => (
                    <motion.div
                        key={item.id}
                        initial={{ opacity: 0, y: 20 }}
                        animate={{ opacity: 1, y: 0 }}
                        transition={{ delay: index * 0.1 }}
                        className="group bg-white rounded-3xl border-2 border-slate-100 overflow-hidden hover:border-primary-200 hover:shadow-xl hover:shadow-primary-100/50 transition-all duration-300"
                    >
                        <div className={`aspect-[3/4] p-6 flex flex-col items-center justify-center relative ${item.bgColor}`}>
                            {item.sections && item.sections.length > 0 && item.sections[0].content_svg ? (
                                <div className="w-full h-full bg-white rounded-xl shadow-sm border border-black/5 overflow-hidden relative">
                                    <div
                                        className="w-full h-full transform scale-90 origin-center"
                                        dangerouslySetInnerHTML={{ __html: item.sections[0].content_svg }}
                                    />
                                    {/* Overlay to prevent interaction with SVG parts */}
                                    <div className="absolute inset-0 z-10"></div>
                                </div>
                            ) : (
                                <div className="w-full h-full bg-white rounded-xl shadow-sm border border-black/5 flex flex-col items-center justify-center p-4 text-center">
                                    {item.image ? (
                                        <img src={item.image} alt={item.title} className="w-full h-full object-cover rounded-lg" />
                                    ) : (
                                        <>
                                            <h3 className="font-bold text-lg mb-2">{item.title}</h3>
                                            <p className="text-sm text-slate-500 mb-4">{item.subtitle}</p>
                                            <div className="text-4xl font-mono opacity-20">123</div>
                                        </>
                                    )}
                                </div>
                            )}

                            <div className="absolute inset-0 bg-black/0 group-hover:bg-black/5 transition-colors flex items-center justify-center opacity-0 group-hover:opacity-100 z-20">
                                <Button
                                    className="shadow-xl"
                                    onClick={() => onRestore && onRestore(item)}
                                >
                                    <Wand2 className="mr-2" size={18} />
                                    {onRestore ? "Open in Editor" : "View Sheet"}
                                </Button>
                            </div>
                        </div>

                        <div className="p-5">
                            <h3 className="font-bold text-lg text-slate-900 mb-1">{item.title}</h3>
                            <div className="flex items-center justify-between text-sm text-slate-500">
                                <span className="bg-slate-100 px-2 py-1 rounded-md font-medium text-slate-600">Age {item.age}</span>
                                <span className="flex items-center gap-1">
                                    <Download size={14} /> {item.downloads} downloads
                                </span>
                            </div>
                        </div>
                    </motion.div>
                ))}
            </div>
        </section>
    );
};
