"use client";

import { motion, AnimatePresence } from "framer-motion";
import { X } from "lucide-react";
import type { Product } from "@/lib/products";
import { useLanguage } from "@/context/language-context";
import Link from "next/link";
import { useProductStore } from "@/lib/productZustand";
import Image from "next/image";
import { usePathname } from "next/navigation";

interface ProductModalProps {
  product: Product;
  onClose: () => void;
}

export function ProductModal({ product, onClose }: ProductModalProps) {
  const { t, language } = useLanguage();
  const setProductName = useProductStore((state) => state.setProductName);
  const languageIndex = language === "uz" ? true : false;
  const pathName = usePathname();
  const isPathName = pathName === "/product";


  return (
    <AnimatePresence>
      <motion.div
        initial={{ opacity: 0 }}
        animate={{ opacity: 1 }}
        exit={{ opacity: 0 }}
        onClick={onClose}
        className="fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4"
      >
        <motion.div
          initial={{ scale: 0.95, opacity: 0 }}
          animate={{ scale: 1, opacity: 1 }}
          exit={{ scale: 0.95, opacity: 0 }}
          onClick={(e) => e.stopPropagation()}
          className="bg-white rounded-lg max-w-4xl w-full max-h-[90vh] overflow-y-auto"
        >
          {/* Header */}
          <div className="sticky z-10 top-0 bg-white border-b border-gray-200 sm:p-6 p-2 flex justify-between items-center">
            <h2 className="md:text-2xl text-lg font-bold text-gray-900">
              {languageIndex ? product.name_uz : product.name_ru}
            </h2>
            <motion.button
              whileHover={{ scale: 1.1 }}
              whileTap={{ scale: 0.95 }}
              onClick={onClose}
              className="p-2 hover:bg-gray-100 rounded-lg transition-colors"
            >
              <X size={24} />
            </motion.button>
          </div>

          {/* Content */}
          <div className="sm:p-6 p-2">
            <div className="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8">
              {/* Image */}
              <div className="relative max-sm:w-full max-md:h-50">
                <Image
                  src={`https://api.serenmebel.uz${product.image}`}
                  alt="image"
                  fill
                  className="object-contain max-md:h-50"
                />
              </div>

              {/* Specifications */}
              <div>
                <div className="mb-8">
                  <h3 className="text-lg font-semibold text-gray-900 mb-4">
                    {t.features}
                  </h3>
                  <div className="space-y-3">
                    {product.features.map((spec, idx) => (
                      <div
                        key={idx}
                        className="flex max-sm:flex-col justify-between py-2 border-b border-gray-100"
                      >
                        <span className="text-gray-600">
                          {languageIndex ? spec.key_uz : spec.key_ru}:
                        </span>
                        <span className="text-gray-600">
                          {languageIndex ? spec.value_uz : spec.value_ru}
                        </span>
                      </div>
                    ))}
                  </div>
                </div>

                {/* CTA Buttons */}
                <div className="space-y-3">
                  <Link href={isPathName ? "/#contact" : "#contact"} >
                    <motion.button
                      onClick={() => {
                        onClose();
                        setProductName(
                          languageIndex ? product.name_uz : product.name_ru
                        );
                      }}
                      whileHover={{ scale: 1.05 }}
                      whileTap={{ scale: 0.95 }}
                      className="w-full px-6 py-3 bg-primary/80 text-white rounded-lg font-semibold hover:bg-primary transition-colors"
                    >
                      {t.contact.send}
                    </motion.button>
                  </Link>
                </div>
              </div>
            </div>
            <div className="text-gray-800 max-sm:text-[14px]">
              {languageIndex ? product.description_uz : product.description_ru}
            </div>
          </div>
        </motion.div>
      </motion.div>
    </AnimatePresence>
  );
}
