"use client";

import { useState, useEffect, useCallback } from "react";
import Image, { type StaticImageData } from "next/image";

interface CarouselImage {
  id: number;
  src: string | StaticImageData;
  alt: string;
  caption?: string;
}

export const placeholderImages: CarouselImage[] = [
  {
    id: 1,
    src: "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=800&h=1000&fit=crop&q=80",
    alt: "Professional man smiling",
  },
  {
    id: 2,
    src: "https://images.unsplash.com/photo-1494790108377-be9c29b29330?w=800&h=1000&fit=crop&q=80",
    alt: "Professional woman smiling",
  },
  {
    id: 3,
    src: "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=800&h=1000&fit=crop&q=80",
    alt: "Professional person portrait",
  },
  {
    id: 4,
    src: "https://images.unsplash.com/photo-1438761681033-6461ffad8d80?w=800&h=1000&fit=crop&q=80",
    alt: "Happy professional",
  },
];

interface ImageCarouselProps {
  images?: CarouselImage[];
  autoPlayInterval?: number;
  grayscale?: boolean;
}

export default function ImageCarousel({
  images = placeholderImages,
  autoPlayInterval = 5000,
  grayscale = true,
}: ImageCarouselProps) {
  const [currentIndex, setCurrentIndex] = useState(0);

  const goToNext = useCallback(() => {
    setCurrentIndex((prevIndex) => (prevIndex + 1) % images.length);
  }, [images.length]);

  const goToSlide = (index: number) => {
    setCurrentIndex(index);
  };

  useEffect(() => {
    const timer = setInterval(goToNext, autoPlayInterval);
    return () => clearInterval(timer);
  }, [goToNext, autoPlayInterval]);

  return (
    <div className="relative w-full h-full min-h-[600px] rounded-2xl overflow-hidden">
      {/* Images */}
      <div className="relative w-full h-full">
        {images.map((image, index) => (
          <div
            key={image.id}
            className={`absolute inset-0 transition-opacity duration-700 ease-in-out ${
              index === currentIndex ? "opacity-100" : "opacity-0"
            }`}
          >
            <Image
              src={image.src}
              alt={image.alt}
              fill
              className={`object-cover ${grayscale ? "grayscale" : ""}`}
            />
            {image.caption && (
              <div className="absolute inset-x-0 bottom-0 bg-gradient-to-t from-black/70 via-black/30 to-transparent px-8 pb-20 pt-16">
                <p className="max-w-sm text-2xl font-semibold leading-snug text-white drop-shadow-sm">
                  {image.caption}
                </p>
              </div>
            )}
          </div>
        ))}
      </div>

      {/* Pagination Dots */}
      {images.length > 1 && (
        <div className="absolute bottom-6 right-6 flex items-center gap-2">
          {images.map((_, index) => (
            <button
              key={index}
              onClick={() => goToSlide(index)}
              className={`rounded-full transition-all duration-300 ${
                index === currentIndex
                  ? "bg-[#3B9EC9] w-3 h-3"
                  : "bg-white/70 w-2.5 h-2.5 hover:bg-white"
              }`}
              aria-label={`Go to slide ${index + 1}`}
            />
          ))}
        </div>
      )}
    </div>
  );
}
