/**
 * Driver statistics and ranking data used for demo seeding.
 * Split out from the legacy DemoData module to keep responsibilities focused.
 */
export interface DriverStats {
  driverId: string;
  rating: number;
  totalRaces: number;
  wins: number;
  podiums: number;
  dnfs: number;
  avgFinish: number;
  bestFinish: number;
  worstFinish: number;
  consistency: number;
  overallRank: number;
  percentile: number;
}

/**
 * Create demo driver statistics based on seed data.
 * This is deterministic for a given driver ordering so it can be reused
 * by any in-memory repository wiring.
 */
export function createDemoDriverStats(drivers: Array<{ id: string }>): Record<string, DriverStats> {
  const stats: Record<string, DriverStats> = {};

  drivers.forEach((driver, index) => {
    const totalRaces = 40 + index * 5;
    const wins = Math.max(0, Math.floor(totalRaces * 0.2) - index);
    const podiums = Math.max(wins * 2, 0);
    const dnfs = Math.max(0, Math.floor(index / 2));
    const rating = 1500 + index * 25;

    stats[driver.id] = {
      driverId: driver.id,
      rating,
      totalRaces,
      wins,
      podiums,
      dnfs,
      avgFinish: 4,
      bestFinish: 1,
      worstFinish: 20,
      consistency: 80,
      overallRank: index + 1,
      percentile: Math.max(0, 100 - index),
    };
  });

  return stats;
}

/**
 * Get league-specific rankings for a driver (demo implementation).
 * In production this would be calculated from actual league membership
 * and results; here we keep a very small static example for UI wiring.
 */
export function getDemoLeagueRankings(driverId: string, leagueId: string): {
  rank: number;
  totalDrivers: number;
  percentile: number;
} {
  // Mock league rankings (in production, calculate from actual league membership)
  const mockLeagueRanks: Record<string, Record<string, any>> = {
    'league-1': {
      'driver-1': { rank: 1, totalDrivers: 12, percentile: 92 },
      'driver-2': { rank: 2, totalDrivers: 12, percentile: 84 },
      'driver-3': { rank: 4, totalDrivers: 12, percentile: 67 },
      'driver-4': { rank: 5, totalDrivers: 12, percentile: 58 },
    },
  };

  return mockLeagueRanks[leagueId]?.[driverId] || { rank: 0, totalDrivers: 0, percentile: 0 };
}