// Prode / Sports Pool Types

export enum ProdeErrorCode {
  PredictionLocked = 'PRODE_PREDICTION_LOCKED',
}

export type ProdeApiError = {
  code: ProdeErrorCode;
  message: string;
  metadata: Record<string, unknown>;
};

export enum MatchFilter {
  All = 'all',
  Upcoming = 'upcoming',
  Finished = 'finished',
  NeedsPrediction = 'needs_prediction',
}

export type CompetitionStatus =
  | 'DRAFT'
  | 'SCHEDULED'
  | 'ACTIVE'
  | 'FINISHED'
  | 'DELETED';

export type MatchStatus =
  | 'SCHEDULED'
  | 'LIVE'
  | 'FINISHED'
  | 'POSTPONED'
  | 'CANCELLED';

export type StageType = 'GROUP' | 'KNOCKOUT';

export type PositionDirection = 'UP' | 'DOWN' | 'SAME';

export type PrizeType = 'PODIUM' | 'INDIVIDUAL' | 'RANGE';

export type RankingCalculationStatus =
  | 'UP_TO_DATE'
  | 'CALCULATING'
  | 'PENDING_CALCULATION'
  | 'ERROR';

// Access Response
export interface ProdeAccessResponse {
  hasAccess: boolean;
  hasAssignedCompetitions: boolean;
  moduleName: string;
  moduleNameTranslations: Record<string, string> | null;
  isEnabled: boolean;
}

// Competition Types
export interface CompetitionPrizePreview {
  id: number;
  name: string;
  position: string;
}

export interface ParticipantAvatar {
  userId: number;
  avatarUrl: string | null;
}

export interface Competition {
  id: number;
  name: string;
  description: string | null;
  coverImageUrl: string | null;
  status: CompetitionStatus;
  startDate: string | null;
  endDate: string | null;
  participantCount: number;
  participantAvatars: ParticipantAvatar[];
  isUserParticipating: boolean;
  prizes: CompetitionPrizePreview[];
}

export interface CompetitionCreator {
  userId: number;
  name: string;
  avatarUrl: string | null;
}

export interface CompetitionDetail extends Competition {
  leagueId: number;
  leagueName: string | null;
  creator: CompetitionCreator | null;
  daysUntilStart: number | null;
  daysUntilEnd: number | null;
}

// Team Types
export interface Team {
  id: number;
  name: string;
  localizedName: string | null;
  shortName: string | null;
  countryCode: string | null;
  flagUrl: string | null;
  emoji: string | null;
}

export interface TeamWithGroup extends Team {
  countryCode: string | null;
  groupId: number | null;
}

// Venue Types
export interface Venue {
  name: string;
  city: string;
  country: string;
}

// Match Types
export interface Match {
  id: number;
  matchDate: string;
  matchTime: string | null;
  status: MatchStatus;
  stageId: number;
  stageName: string | null;
  stageType: StageType | null;
  groupId: number | null;
  groupName: string | null;
  round: number | null;
  homeTeam: Team | null;
  awayTeam: Team | null;
  homeTeamPlaceholder: string | null;
  awayTeamPlaceholder: string | null;
  homeScore: number | null;
  awayScore: number | null;
  homePenaltyScore: number | null;
  awayPenaltyScore: number | null;
  winnerId: number | null;
  venue: Venue | null;
  isPredictionOpen: boolean;
}

// Fixture Types
export interface Stage {
  id: number;
  name: string;
  shortName: string | null;
  type: StageType;
  order: number;
}

export interface Group {
  id: number;
  name: string;
}

export interface FixtureResponse {
  stages: Stage[];
  groups: Group[];
  rounds: number[];
  currentStageId: number | null;
  currentRound: number | null;
  matches: Match[];
}

// Matches by Date
export interface MatchesByDate {
  date: string;
  dateDisplay: string;
  matches: Match[];
}

// Prediction Types
export interface PointsBreakdown {
  exactScore: boolean;
  correctResult: boolean;
  penaltyBonus: number;
  basePoints: number;
  totalPoints: number;
}

// Match Breakdown Types (from API)
export type BreakdownReason =
  | 'EXACT_SCORE'
  | 'CLOSE_PREDICTION_DRAW'
  | 'CLOSE_PREDICTION_GOAL_DIFF'
  | 'CORRECT_RESULT'
  | 'PARTIAL_SCORE'
  | 'CORRECT_QUALIFIER';

export interface BreakdownItem {
  points: number;
  reason: BreakdownReason;
  description: string;
}

export interface MatchBreakdownResponse {
  totalPoints: number;
  items: BreakdownItem[];
}

export interface Prediction {
  id: number;
  matchId: number;
  predictedHomeScore: number;
  predictedAwayScore: number;
  predictedQualifierId: number | null;
  predictedTotalPenalties: number | null;
  pointsEarned: number | null;
  pointsBreakdown: PointsBreakdown | null;
  isLocked: boolean;
  createdAt: string;
  updatedAt: string;
}

// Match data as returned inside MatchWithPrediction response
export type MatchInPrediction = Omit<Match, 'isPredictionOpen'> & {
  stageShortName: string | null;
};

export interface MatchWithPrediction {
  match: MatchInPrediction;
  prediction: Prediction | null;
  isPredictionOpen: boolean;
}

export type MatchesPage = {
  data: MatchWithPrediction[];
  nextCursor: string | null;
};

export interface SavePredictionRequest {
  matchId: number;
  predictedHomeScore: number;
  predictedAwayScore: number;
  predictedQualifierId?: number;
  predictedTotalPenalties?: number;
}

// Tournament Prediction Types
export interface TournamentPredictionTeam {
  id: number;
  name: string;
  localizedName: string | null;
  countryCode: string | null;
  flagUrl: string | null;
  emoji: string | null;
}

export interface TournamentPrediction {
  id: number;
  championTeam: TournamentPredictionTeam | null;
  runnerUpTeam: TournamentPredictionTeam | null;
  thirdPlaceTeam: TournamentPredictionTeam | null;
  championPoints: number | null;
  runnerUpPoints: number | null;
  thirdPlacePoints: number | null;
  isLocked: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface TournamentPredictionResponse {
  isOpen: boolean;
  prediction: TournamentPrediction | null;
}

export interface SaveTournamentPredictionRequest {
  championTeamId: number;
  runnerUpTeamId: number;
  thirdPlaceTeamId: number;
}

// Ranking Types
export interface RankingEntry {
  position: number;
  positionChange: number;
  positionDirection: PositionDirection;
  userId: number;
  userName: string | null;
  userAvatarUrl: string | null;
  totalPoints: number;
  exactResults: number;
}

export interface RankingPagination {
  total: number;
  offset: number;
  limit: number;
}

export interface RankingPage {
  data: RankingEntry[];
  pagination: RankingPagination;
}

export interface UserRankingSummary {
  position: number | null;
  previousPosition: number | null;
  positionChange: number;
  positionDirection: PositionDirection;
  totalPoints: number;
  exactResults: number;
  correctResults: number;
  totalParticipants: number;
}

// Prize Types
export interface Prize {
  id: number;
  positionStart: number;
  positionEnd: number | null;
  positionDisplay: string;
  type: PrizeType;
  name: string;
  description: string | null;
  imageUrl: string | null;
  positionCaption: string | null;
}

export interface PrizesResponse {
  podiumPrizes: Prize[];
  otherPrizes: Prize[];
}

// Rules Types
export interface FaqItem {
  question: string;
  answer: string;
}

export interface CompetitionRules {
  rulesContent: string | null;
  considerations: string | null;
  faq: FaqItem[];
}

// User Stats Types
export type UserStatsResponse = {
  ranking: {
    position: number;
    previousPosition: number;
    positionChange: number;
    positionDirection: PositionDirection;
    totalPoints: number;
    exactResults: number;
    correctResults: number;
    totalParticipants: number;
  };
  tournamentPrediction: {
    isOpen: boolean;
    prediction: TournamentPrediction | null;
  };
};

// Ranking Status Types
export interface RankingStatus {
  status: RankingCalculationStatus;
  lastUpdatedAt: string | null;
  pendingMatchIds: number[];
  message: string | null;
}
