"use client";

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

import {
  getPickupPoints,
  PickupPoint,
} from "@/api/exploreApi";

interface Props {
  city?: string;
  category?: string;
}

export default function ExplorePickupPoints({
  city,
  category,
}: Props) {
  const [pickupPoints, setPickupPoints] = useState<PickupPoint[]>([]);
  const [loading, setLoading] = useState(true);

  const scrollRef = useRef<HTMLDivElement>(null);

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

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

      const data = await getPickupPoints(city, category);

      setPickupPoints(data);
    } catch (error) {
      console.error(error);
    } finally {
      setLoading(false);
    }
  };

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

  if (!loading && pickupPoints.length === 0) return null;

  return (
    <section className="bg-gray-50 py-14">
      <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">
            Pickup Locations
          </h2>

          <p className="text-gray-500 mt-2">
            Find verified pickup locations near you.
          </p>
        </div>

        <div className="relative">

          {/* Left */}

          <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 border shadow-lg
            items-center justify-center z-20 hover:scale-105 transition"
          >
            <ChevronLeft size={22} />
          </button>

          {/* Cards */}

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

                    <div className="h-5 bg-gray-200 rounded mt-4 w-40" />

                    <div className="h-4 bg-gray-200 rounded mt-3 w-52" />

                    <div className="h-4 bg-gray-200 rounded mt-2 w-32" />
                  </div>
                ))
              : pickupPoints.map((place) => (
                  <div
                    key={place.id}
                    className="flex-shrink-0 w-[300px] bg-white rounded-2xl overflow-hidden shadow hover:shadow-xl transition"
                  >
                    {/* Image */}

                    <div className="relative h-[190px]">

                      <Image
                        // src={
                        //   place.images?.length
                        //     ? place.images[0]
                        //     : "/places/default.jpg"
                        // }

                         src="/pickup2.webp"
                        alt={place.name}
                        fill
                        className="object-cover"
                      />

                    </div>

                    {/* Content */}

                    <div className="p-5">

                      <h3 className="font-semibold text-lg line-clamp-1">
                        {place.name}
                      </h3>

                      <div className="flex items-start gap-2 mt-3">

                        <MapPin
                          className="text-primary mt-1"
                          size={18}
                        />

                        <p className="text-sm text-gray-600 line-clamp-2">
                          {place.address}
                        </p>

                      </div>

                      <div className="flex items-center gap-2 mt-4">

                        <Building2
                          className="text-primary"
                          size={18}
                        />

                        <span className="text-sm font-medium">
                          {place.partner_name}
                        </span>

                      </div>

                      <div className="mt-5 flex justify-between text-sm">

                        <span className="text-gray-500">
                          {place.city}
                        </span>

                        <span className="font-medium text-primary">
                          {place.state}
                        </span>

                      </div>

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

          {/* Right */}

          <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 border shadow-lg
            items-center justify-center z-20 hover:scale-105 transition"
          >
            <ChevronRight size={22} />
          </button>
        </div>
      </div>
    </section>
  );
}