import React, { useCallback, useState } from 'react';
import EventWizard from '@/features/event/components/EventWizard';
import { useApiFetch } from '@/utils/apiFetch';
import { fetchEvents, type Event } from '@/features/event/api/eventService';
import { toast } from 'sonner';

interface EventWizardContainerProps {
  onComplete: () => void;
  onBackToOverview: () => void;
}

const EventWizardContainer: React.FC<EventWizardContainerProps> = ({ onComplete, onBackToOverview }) => {
  const apiFetch = useApiFetch();
  const [events, setEvents] = useState<Event[]>([]);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);

  const reload = useCallback(async () => {
    setLoading(true);
    setError(null);
    try {
      const list = await fetchEvents(apiFetch);
      setEvents(Array.isArray(list) ? list : []);
    } catch (e: any) {
      const msg = e?.message || 'Fehler beim Laden der Veranstaltungen';
      setError(msg);
      toast.error(msg);
    } finally {
      setLoading(false);
    }
  }, [apiFetch]);

  return (
    <EventWizard
      onComplete={onComplete}
      onBackToOverview={onBackToOverview}
      events={events}
      loading={loading}
      error={error}
      onReload={reload}
    />
  );
};

export default EventWizardContainer;
