"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("/data/places.json");
        const data: Place[] = await response.json();
        
        // Filtrer par catégorie si nécessaire
        const filteredPlaces = category === "all" 
          ? data 
          : data.filter((place) => place.category === category);
        
        setPlaces(filteredPlaces);
      } 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>
  );
}

