import type { ImageServicePort } from '@core/media';

const MALE_DEFAULT_AVATAR = '/images/avatars/male-default-avatar.jpg';
const FEMALE_DEFAULT_AVATAR = '/images/avatars/female-default-avatar.jpeg';

export class DemoImageServiceAdapter implements ImageServicePort {
  getDriverAvatar(driverId: string): string {
    const numericSuffixMatch = driverId.match(/(\d+)$/);
    if (numericSuffixMatch) {
      const numericSuffixString = numericSuffixMatch[1] ?? '';
      const numericSuffix = Number.parseInt(numericSuffixString, 10);
      return numericSuffix % 2 === 0 ? FEMALE_DEFAULT_AVATAR : MALE_DEFAULT_AVATAR;
    }

    const seed = stableHash(driverId);
    return seed % 2 === 0 ? FEMALE_DEFAULT_AVATAR : MALE_DEFAULT_AVATAR;
  }

  getTeamLogo(teamId: string): string {
    const seed = stableHash(teamId);
    return `https://picsum.photos/seed/team-${seed}/256/256`;
  }

  getLeagueCover(leagueId: string): string {
    const seed = stableHash(leagueId);
    return `https://picsum.photos/seed/league-cover-${seed}/1200/280?blur=2`;
  }

  getLeagueLogo(leagueId: string): string {
    const seed = stableHash(leagueId);
    return `https://picsum.photos/seed/league-logo-${seed}/160/160`;
  }
}

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