"use client";

import { useEffect, useRef, useState } from "react";
import Image from "next/image";
import { ChevronLeft, ChevronRight, MapPin } from "lucide-react";

import {
  getTouristPlaces,
  TouristPlace,
} from "@/api/exploreApi";

interface Props {
  city?: string;
}

export default function ExplorePlaces({
  city = "Indore",
}: Props) {
  const [places, setPlaces] = useState<TouristPlace[]>([]);
  const [loading, setLoading] = useState(true);

  const scrollRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    fetchPlaces();
  }, [city]);

  const fetchPlaces = async () => {
    try {
      setLoading(true);

      const data = await getTouristPlaces(city);

      setPlaces(data);
    } catch (error) {
      console.error("Failed to fetch tourist places", error);
    } finally {
      setLoading(false);
    }
  };

  const scroll = (direction: "left" | "right") => {
    scrollRef.current?.scrollBy({
      left: direction === "left" ? -320 : 320,
      behavior: "smooth",
    });
  };

  if (!loading && places.length === 0) {
    return null;
  }

  return (

        <section className="bg-gray-100 py-12">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-12 relative">

        {/* Title */}
        <h2 className="text-xl sm:text-2xl md:text-3xl font-semibold mb-8">
          Places to explore in{" "}
          <span className="relative text-primary">
            {city}
            <svg
              className="absolute left-0 -bottom-2 w-full"
              height="12"
              viewBox="0 0 168 12"
              fill="none"
            >
              <path
                d="M2 10C50 2 120 2 166 10"
                stroke="#FFCB20"
                strokeWidth="4"
                strokeLinecap="round"
              />
            </svg>
          </span>
        </h2>

        <div className="relative">

          {/* Left Arrow */}
          <button
            onClick={() => scroll("left")}
            className="hidden md:flex absolute -left-6 lg:-left-10 top-1/2
                       -translate-y-1/2 bg-white shadow-lg border
                       w-10 h-10 rounded-full items-center justify-center
                       z-20 hover:scale-105 transition"
          >
            <ChevronLeft size={22} />
          </button>

          {/* Scroll Container */}
          <div
            ref={scrollRef}
            className="flex gap-6 overflow-x-auto scroll-smooth scrollbar-hide"
          >
            {loading
              ? Array.from({ length: 6 }).map((_, index) => (
                  <div
                    key={index}
                    className="flex-shrink-0 w-[220px] sm:w-[240px] md:w-[260px]"
                  >
                    <div className="h-[200px] rounded-xl bg-gray-200 animate-pulse" />

                    <div className="mt-4 h-5 w-28 mx-auto rounded bg-gray-200 animate-pulse" />
                  </div>
                ))
              : places.map((place) => {
                  const image =
                    place.image_url || "/places/default.jpg";

                  return (
                    <div
                      key={place.id}
                      className="group flex-shrink-0 w-[220px] sm:w-[240px] md:w-[260px]"
                    >
                      <div className="relative h-[170px] sm:h-[190px] md:h-[210px] rounded-xl overflow-hidden shadow">

                        <Image
                          src={image}
                          alt={place.name}
                          fill
                          className="object-cover transition duration-500 group-hover:scale-110"
                          unoptimized
                        />

                        <div className="absolute inset-0 bg-gradient-to-t from-black/60 via-black/20 to-transparent" />

                        <div className="absolute bottom-3 left-3 right-3 flex items-center gap-2">

                          <MapPin
                            size={18}
                            className="text-white flex-shrink-0"
                          />

                          <span className="text-white font-medium text-sm line-clamp-1">
                            {place.name}
                          </span>

                        </div>

                      </div>
                    </div>
                  );
                })}
          </div>

          {/* Right Arrow */}
          <button
            onClick={() => scroll("right")}
            className="hidden md:flex absolute -right-6 lg:-right-10 top-1/2
                       -translate-y-1/2 bg-white shadow-lg border
                       w-10 h-10 rounded-full items-center justify-center
                       z-20 hover:scale-105 transition"
          >
            <ChevronRight size={22} />
          </button>

        </div>
      </div>
    </section>
  );
}

