// ========================
// Enums & Constants
// ========================

export type SportType = "football" | "basketball";

export type MatchStatus =
  | "LIVE"
  | "Finished"
  | "Not Started"
  | "UPCOMING"
  | "NOT_STARTED"
  | string;

// ========================
// Query DTOs
// ========================

export interface TeamFilterDto {
  name?: string;
  id?: string;
}

export interface OddFilterDto {
  market: string;
  minOdd?: number;
  maxOdd?: number;
}

export interface DateRangeDto {
  from: string;
  to: string;
}

export interface MatchQueryDto {
  sport: SportType;
  limit?: number;
  leagueId?: string;
  status?: MatchStatus;
  date?: string; // YYYY-MM-DD
  team?: TeamFilterDto;
  odds?: OddFilterDto[];
  dateRange?: DateRangeDto;
}

export interface MatchListParams {
  page?: number;
  limit?: number;
  sport?: SportType;
}

// ========================
// Response DTOs
// ========================

export interface MatchResponseDto {
  id: string;
  matchName: string;
  matchSlug?: string;
  mstUtc: number | string; // Timestamp
  date?: string | Date;
  status: MatchStatus;
  state?: string;

  homeTeamId?: string;
  awayTeamId?: string;

  // Scores
  scoreHome?: number;
  scoreAway?: number;
  score?: {
    home: number;
    away: number;
    htHome?: number | null;
    htAway?: number | null;
  };
  htScoreHome?: number;
  htScoreAway?: number;

  // Teams
  homeTeamName: string;
  homeTeamLogo?: string;
  awayTeamName: string;
  awayTeamLogo?: string;

  // League & Country
  leagueName?: string;
  countryName?: string;

  // Odds (dynamic key-value)
  odds?: Record<string, Record<string, { odd: string }>>;

  // Nested Objects (from Backend include)
  homeTeam?: { id?: string; name: string; logo?: string };
  awayTeam?: { id?: string; name: string; logo?: string };
  league?: {
    name: string;
    country?: { name: string; flag?: string };
  };
  lineups?: {
    home: Array<{
      player?: { name: string; id: string };
      position?: string | null;
      shirtNumber?: number | null;
      isStarting?: boolean;
      isProbable?: boolean;
      lineupSource?: string;
      projectionConfidence?: number;
    }>;
    away: Array<{
      player?: { name: string; id: string };
      position?: string | null;
      shirtNumber?: number | null;
      isStarting?: boolean;
      isProbable?: boolean;
      lineupSource?: string;
      projectionConfidence?: number;
    }>;
  };

  // Match detail enrichments
  refereeName?: string;
  sidelined?: Record<string, unknown>;
  events?: MatchEvent[];
  playerEvents?: MatchEvent[];

  // Additional fields from backend detail endpoint
  lineupSource?: string;
  stats?: {
    home?: TeamStats;
    away?: TeamStats;
  };
  footballTeamStats?: TeamStats[];
  officials?: MatchOfficial[];
  winner?: string;
}

export interface MatchEvent {
  id: number;
  playerId?: string;
  teamId?: string;
  eventType: "goal" | "card" | "substitute" | string;
  eventSubtype?: string | null;
  timeMinute: string;
  timeSeconds?: number | null;
  periodId?: number;
  assistPlayerId?: string | null;
  scoreAfter?: string | null;
  playerOutId?: string | null;
  position?: "home" | "away" | string;
  player?: { id: string; name: string };
  assistPlayer?: { id: string; name: string } | null;
  substitutedOut?: { id: string; name: string } | null;
}

export interface TeamStats {
  id?: number;
  teamId?: string;
  possessionPercentage?: number | null;
  shotsOnTarget?: number | null;
  shotsOffTarget?: number | null;
  totalShots?: number | null;
  totalPasses?: number | null;
  corners?: number | null;
  fouls?: number | null;
  offsides?: number | null;
}

export interface MatchOfficial {
  id: number;
  name: string;
  roleId: number;
}

export interface ActiveLeagueDto {
  id: string;
  name: string;
  code?: string;
  countryName?: string;
  countryFlag?: string;
  matchCount: number;
  liveCount: number;
}

export interface LeagueWithMatchesDto {
  id: string;
  name: string;
  code?: string;
  country: {
    id: string;
    name: string;
    flagUrl?: string; // Backend uses flagUrl
  };
  sport: SportType;
  matches: MatchResponseDto[];
}
