import { faker } from '../faker/faker';

const DRIVER_AVATARS = [
  '/images/avatars/avatar-1.svg',
  '/images/avatars/avatar-2.svg',
  '/images/avatars/avatar-3.svg',
  '/images/avatars/avatar-4.svg',
  '/images/avatars/avatar-5.svg',
  '/images/avatars/avatar-6.svg',
] as const;

const TEAM_LOGOS = [
  '/images/logos/team-1.svg',
  '/images/logos/team-2.svg',
  '/images/logos/team-3.svg',
  '/images/logos/team-4.svg',
] as const;

const LEAGUE_BANNERS = [
  '/images/header.jpeg',
  '/images/ff1600.jpeg',
  '/images/lmp3.jpeg',
  '/images/porsche.jpeg',
] as const;

function hashString(input: string): number {
  let hash = 0;
  for (let i = 0; i < input.length; i += 1) {
    hash = (hash * 31 + input.charCodeAt(i)) | 0;
  }
  return Math.abs(hash);
}

export function getDriverAvatar(driverId: string): string {
  const index = hashString(driverId) % DRIVER_AVATARS.length;
  const avatar = DRIVER_AVATARS[index] ?? DRIVER_AVATARS[0];
  return avatar;
}

export function getTeamLogo(teamId: string): string {
  const index = hashString(teamId) % TEAM_LOGOS.length;
  const logo = TEAM_LOGOS[index] ?? TEAM_LOGOS[0];
  return logo;
}

export function getLeagueBanner(leagueId: string): string {
  const index = hashString(leagueId) % LEAGUE_BANNERS.length;
  const banner = LEAGUE_BANNERS[index] ?? LEAGUE_BANNERS[0];
  return banner;
}

export interface LeagueCoverImage {
  url: string;
  alt: string;
}

export function getLeagueCoverImage(leagueId: string): LeagueCoverImage {
  const seed = hashString(leagueId);

  faker.seed(seed);
  const alt = faker.lorem.words(3);

  const url = `https://picsum.photos/seed/${seed}/1200/280?blur=2`;

  return { url, alt };
}

export { DRIVER_AVATARS, TEAM_LOGOS, LEAGUE_BANNERS };