export type ModeSelection =
  | 'beatmap_only'
  | 'mapper_only'
  | 'artist_only'
  | 'mixed';

export type TimerProfile = 'normal' | 'insane';

export type GameplayBeatmapCard = {
  title: string;
  artistName: string;
  mapperName: string;
  mapperAvatarUrl?: string | null;
  difficultyName: string;
  status: string;
  backgroundImageUrl?: string | null;
};

export type GameplayMapperCard = {
  name: string;
  avatarUrl?: string | null;
  bannerUrl?: string | null;
  fallbackVisual?: string | null;
};

export type GameplayArtistCard = {
  name: string;
  imageUrl?: string | null;
  fallbackVisual?: string | null;
};

export type GameplayRoundCard =
  | GameplayBeatmapCard
  | GameplayMapperCard
  | GameplayArtistCard;

export type GameplayReveal = {
  roundMode: 'beatmap' | 'mapper' | 'artist';
  modeLabel: string;
  valueSuffix: string;
  correctChoice: 'a' | 'b';
  winningChoice: 'a' | 'b';
  playerChoice: 'a' | 'b' | 'none';
  isCorrect: boolean;
  loseReason: 'wrong' | 'timeout' | 'none';
  values: {
    a: number;
    b: number;
  };
  answeredAt?: string;
};

export type GameplayRound = {
  id: string;
  roundNumber: number;
  timeLimitMs: number;
  presentedAt: string;
  expiresAt: string;
  cards: {
    a: GameplayRoundCard;
    b: GameplayRoundCard;
  };
  reveal?: GameplayReveal;
};

export type GameplaySession = {
  id: string;
  status: 'active' | 'finished' | 'abandoned';
  modeSelection: ModeSelection;
  timerProfile: TimerProfile;
  sessionToken: string;
  startingLives: number;
  livesRemaining: number;
  streak: number;
  bestStreak: number;
  startedAt: string;
  endedAt?: string | null;
  roundsPlayed: number;
  correctGuesses: number;
};

export type HistorySession = GameplaySession & {
  leaderboardEntry?: {
    score: number;
    bestStreak: number;
  } | null;
};

export type GameplayLeaderboardEntry = {
  id: string;
  score: number;
  bestStreak: number;
  roundsSurvived: number;
  achievedAt: string;
  user: {
    id?: string;
    name: string;
  };
};

export const isBeatmapCard = (
  card: GameplayRoundCard,
): card is GameplayBeatmapCard => 'title' in card;

export const isMapperCard = (
  card: GameplayRoundCard,
): card is GameplayMapperCard => 'bannerUrl' in card;

export const isArtistCard = (
  card: GameplayRoundCard,
): card is GameplayArtistCard => !isBeatmapCard(card) && !isMapperCard(card);
