"use client";

import { useEffect, useRef, useState } from "react";
import Image from "next/image";
import { ChevronLeft, ChevronRight, MapPin } from "lucide-react";
import { getCities } from "@/api/exploreApi";

interface ExploreCitiesProps {
  onCitySelect?: (city: string) => void;
}

export default function ExploreCities({
  onCitySelect,
}: ExploreCitiesProps) {
  const [cities, setCities] = useState<string[]>([]);
  const [loading, setLoading] = useState(true);

  const scrollRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    loadCities();
  }, []);

  const loadCities = async () => {
    try {
      const data = await getCities();
      setCities(data);
    } catch (error) {
      console.error("Failed to load cities", error);
    } finally {
      setLoading(false);
    }
  };

  const scroll = (direction: "left" | "right") => {
    if (!scrollRef.current) return;

    scrollRef.current.scrollBy({
      left: direction === "left" ? -320 : 320,
      behavior: "smooth",
    });
  };

  if (!loading && cities.length === 0) return null;

  return (
    <section className="py-14 bg-white">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-12 relative">

        {/* Heading */}
        <div className="mb-8">
          <h2 className="text-2xl md:text-3xl font-semibold">
            Explore Cities
          </h2>

          <p className="text-gray-500 mt-2">
            Choose your favourite city and start your ride.
          </p>
        </div>

        <div className="relative">

          {/* Left Arrow */}
          <button
            onClick={() => scroll("left")}
            className="hidden md:flex absolute -left-5 top-1/2 -translate-y-1/2
                       w-10 h-10 rounded-full bg-white shadow-lg border
                       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-[180px] animate-pulse"
                  >
                    <div className="h-[150px] rounded-xl bg-gray-200" />

                    <div className="h-5 w-24 bg-gray-200 rounded mt-4 mx-auto" />
                  </div>
                ))
              : cities.map((city) => (
                  <div
                    key={city}
                    onClick={() => onCitySelect?.(city)}
                    className="group cursor-pointer flex-shrink-0 w-[180px]"
                  >
                    <div className="relative h-[150px] rounded-xl overflow-hidden shadow">

                      <Image
                        src="/location.svg"
                        alt={city}
                        fill
                        className="object-cover group-hover:scale-110 transition duration-500"
                      />

                      <div className="absolute inset-0 bg-black/35" />

                      <div className="absolute bottom-4 left-4 flex items-center gap-2 text-white">

                        <MapPin size={18} />

                        <span className="font-semibold">
                          {city}
                        </span>

                      </div>

                    </div>
                  </div>
                ))}
          </div>

          {/* Right Arrow */}
          <button
            onClick={() => scroll("right")}
            className="hidden md:flex absolute -right-5 top-1/2 -translate-y-1/2
                       w-10 h-10 rounded-full bg-white shadow-lg border
                       items-center justify-center z-20 hover:scale-105 transition"
          >
            <ChevronRight size={22} />
          </button>
        </div>
      </div>
    </section>
  );
}