

import { RaceListItem } from '@/components/races/RaceListItem';
import { routes } from '@/lib/routing/RouteConfig';
import type { RaceViewData } from '@/lib/view-data/RacesViewData';
import { DateHeader } from '@/ui/DateHeader';
import { EmptyState } from '@/ui/EmptyState';
import { Stack } from '@/ui/Stack';
import { Calendar, CheckCircle2, Clock, PlayCircle, XCircle } from 'lucide-react';

interface RaceListProps {
  racesByDate: Array<{
    dateKey: string;
    dateLabel: string;
    races: RaceViewData[];
  }>;
  totalCount: number;
  onRaceClick: (raceId: string) => void;
}

export function RaceList({ racesByDate, totalCount, onRaceClick }: RaceListProps) {
  const statusConfig = {
    scheduled: {
      icon: Clock,
      variant: 'primary' as const,
      label: 'Scheduled',
    },
    running: {
      icon: PlayCircle,
      variant: 'success' as const,
      label: 'LIVE',
    },
    completed: {
      icon: CheckCircle2,
      variant: 'default' as const,
      label: 'Completed',
    },
    cancelled: {
      icon: XCircle,
      variant: 'warning' as const,
      label: 'Cancelled',
    },
  };

  if (racesByDate.length === 0) {
    return (
      <EmptyState
        icon={Calendar}
        title="No races found"
        description={totalCount === 0 ? 'No races have been scheduled yet' : 'Try adjusting your filters'}
      />
    );
  }

  return (
    <Stack gap={4}>
      {racesByDate.map((group) => (
        <Stack key={group.dateKey} gap={3}>
          <DateHeader 
            date={group.dateLabel} 
          />

          <Stack gap={2}>
            {group.races.map((race) => {
              const config = statusConfig[race.status as keyof typeof statusConfig] || statusConfig.scheduled;

              return (
                <RaceListItem
                  key={race.id}
                  track={race.track}
                  car={race.car}
                  timeLabel={race.timeLabel}
                  relativeTimeLabel={race.relativeTimeLabel}
                  status={race.status}
                  leagueName={race.leagueName ?? 'Unknown League'}
                  leagueHref={routes.league.detail(race.leagueId ?? '')}
                  strengthOfField={race.strengthOfField}
                  onClick={() => onRaceClick(race.id)}
                  statusConfig={config}
                />
              );
            })}
          </Stack>
        </Stack>
      ))}
    </Stack>
  );
}
