import React, { useEffect, useState } from 'react';
import Dashboard from '@/components/dashboard/Dashboard';
import { useApiFetch } from '@/utils/apiFetch';
import { fetchCurrentEvents, type Event } from '@/features/event/api/eventService';

interface DashboardContainerProps {
  onActionClick: (action: string) => void;
  hasActiveEvent: boolean;
}

const DashboardContainer: React.FC<DashboardContainerProps> = ({ onActionClick, hasActiveEvent }) => {
  const apiFetch = useApiFetch();
  const [currentEvent, setCurrentEvent] = useState<Event | null>(null);

  useEffect(() => {
    let isMounted = true;
    (async () => {
      try {
        const events = await fetchCurrentEvents(apiFetch);
        if (!isMounted) return;
        const list = Array.isArray(events) ? events : [];
        const ev = (list.find((e: any) => e && (e.id ?? e._id ?? e.uuid)) as Event) || null;
        setCurrentEvent(ev);
      } catch {
        if (!isMounted) return;
        setCurrentEvent(null);
      }
    })();
    return () => {
      isMounted = false;
    };
  }, [apiFetch]);

  return (
    <Dashboard 
      onActionClick={onActionClick}
      hasActiveEvent={hasActiveEvent}
      currentEvent={currentEvent}
    />
  );
};

export default DashboardContainer;
