"use client";

import { useEffect, useState } from "react";
import PlaceCard from "./PlaceCard";
import { Place } from "@/lib/types/place";

interface PlaceListProps {
  category: string;
}

export default function PlaceList({ category }: PlaceListProps) {
  const [places, setPlaces] = useState<Place[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const fetchPlaces = async () => {
      setLoading(true);
      try {
        const response = await fetch(
          `/api/places${category !== "all" ? `?category=${category}` : ""}`
        );
        const data = await response.json();
        setPlaces(data);
      } catch (error) {
        console.error("Erreur lors du chargement des lieux:", error);
      } finally {
        setLoading(false);
      }
    };

    fetchPlaces();
  }, [category]);

  if (loading) {
    return (
      <div className="flex items-center justify-center py-12">
        <p className="text-gray-600">Chargement...</p>
      </div>
    );
  }

  if (places.length === 0) {
    return (
      <div className="flex items-center justify-center py-12">
        <p className="text-gray-600">Aucun lieu trouvé dans cette catégorie.</p>
      </div>
    );
  }

  return (
    <div className="space-y-6 px-4 pb-6">
      {places.map((place) => (
        <PlaceCard key={place.id} place={place} />
      ))}
    </div>
  );
}

