'use client';

import { Badge } from '@/ui/Badge';
import { Icon } from '@/ui/Icon';
import { Link } from '@/ui/Link';
import { Text } from '@/ui/Text';
import { RaceCard, RaceTimeColumn, RaceInfo } from '@/ui/RaceCard';
import { Car, Trophy, Zap, ArrowRight, Clock, PlayCircle, CheckCircle2, XCircle, HelpCircle } from 'lucide-react';
import React from 'react';

const ICON_MAP = {
  Clock,
  PlayCircle,
  CheckCircle2,
  XCircle,
  HelpCircle,
};

interface RaceListItemProps {
  track: string;
  car: string;
  timeLabel?: string;
  relativeTimeLabel?: string;
  dateLabel?: string;
  dayLabel?: string;
  status: string;
  statusLabel: string;
  statusVariant: 'default' | 'primary' | 'success' | 'warning' | 'danger' | 'info';
  statusIconName: string;
  leagueName?: string | null;
  leagueHref?: string;
  strengthOfField?: number | null;
  onClick: () => void;
}

export function RaceListItem({
  track,
  car,
  timeLabel,
  relativeTimeLabel,
  dateLabel,
  dayLabel,
  status,
  statusLabel,
  statusVariant,
  statusIconName,
  leagueName,
  leagueHref,
  strengthOfField,
  onClick,
}: RaceListItemProps) {
  const isLive = status === 'running';
  const StatusIcon = ICON_MAP[statusIconName as keyof typeof ICON_MAP] || HelpCircle;

  return (
    <RaceCard onClick={onClick} isLive={isLive}>
      <RaceTimeColumn
        date={dateLabel}
        time={dayLabel || timeLabel || ''}
        relativeTime={relativeTimeLabel || timeLabel}
        isLive={isLive}
      />

      <div style={{ width: '1px', height: '2.5rem', backgroundColor: 'var(--ui-color-border-muted)', opacity: 0.2 }} />

      <RaceInfo
        title={track}
        subtitle={car}
        badge={
          <Badge variant={statusVariant}>
            <Icon icon={StatusIcon} size={3.5} />
            {statusLabel}
          </Badge>
        }
        meta={
          <div style={{ display: 'flex', alignItems: 'center', gap: '1rem' }}>
            {strengthOfField && (
              <div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
                <Icon icon={Zap} size={3.5} intent="warning" />
                <Text size="sm" variant="low">SOF {strengthOfField}</Text>
              </div>
            )}
            {leagueName && leagueHref && (
              <Link
                href={leagueHref}
                onClick={(e) => e.stopPropagation()}
                variant="primary"
              >
                <div style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
                  <Icon icon={Trophy} size={3.5} intent="primary" />
                  <Text size="sm" variant="primary">{leagueName}</Text>
                  <Icon icon={ArrowRight} size={3} intent="primary" />
                </div>
              </Link>
            )}
          </div>
        }
      />
    </RaceCard>
  );
}
