// ============================================================
// Staffbase JWT Payload
// ============================================================
export interface StaffbaseTokenData {
  /** Staffbase internal user ID */
  sub: string;
  /** Full name of the user */
  name?: string;
  /** User's preferred locale (e.g. "de_DE") */
  locale?: string;
  /** Branch/department ID */
  branchId?: string;
  /** User's role in the plugin (e.g. "editor", "viewer") */
  role?: string;
  /** Token issued at (unix timestamp) */
  iat?: number;
  /** Token expiry (unix timestamp) */
  exp?: number;
  /** Plugin instance ID */
  aud?: string;
}

// Note: Express Request extensions (staffbaseUser, sbSSO) are declared
// in src/middleware/staffbaseAuth.ts

// ============================================================
// Database Models
// ============================================================
export interface DbUser {
  id: string;           // = Staffbase userId (sub)
  full_name: string;
  locale: string;
  branch_id: string | null;
  role: string;
  created_at: Date;
  updated_at: Date;
}

export interface DbMatch {
  id: number;
  external_id: number;  // football-data.org matchId
  utc_date: Date;
  status: MatchStatus;
  stage: string;
  group_name: string | null;
  home_team_id: number;
  away_team_id: number;
  home_team_name: string;
  away_team_name: string;
  home_team_short: string;
  away_team_short: string;
  home_team_crest: string | null;
  away_team_crest: string | null;
  score_home: number | null;
  score_away: number | null;
  created_at: Date;
  updated_at: Date;
}

export interface DbTip {
  id: number;
  user_id: string;
  match_id: number;
  tip_home: number;
  tip_away: number;
  points: number | null;  // null = not yet evaluated
  created_at: Date;
  updated_at: Date;
}

export interface DbLeaderboardEntry {
  user_id: string;
  full_name: string;
  team: string | null;
  total_points: number;
  tips_count: number;
  exact_count: number;
  tendency_count: number;
  rank: number;
}

// ============================================================
// API Request/Response Types
// ============================================================
export type MatchStatus =
  | 'SCHEDULED'
  | 'TIMED'
  | 'IN_PLAY'
  | 'PAUSED'
  | 'FINISHED'
  | 'SUSPENDED'
  | 'POSTPONED'
  | 'CANCELLED'
  | 'AWARDED';

export interface MatchResponse {
  id: number;
  externalId: number;
  utcDate: string;
  status: MatchStatus;
  stage: string;
  group: string | null;
  homeTeam: TeamInfo;
  awayTeam: TeamInfo;
  score: ScoreInfo;
  userTip?: TipInfo | null;
  /** Minutes until kickoff (negative = past) */
  minutesUntilKickoff: number;
  /** Can the user still place a tip? */
  tippable: boolean;
}

export interface TeamInfo {
  name: string;
  shortName: string;
  crest: string | null;
}

export interface ScoreInfo {
  home: number | null;
  away: number | null;
}

export interface TipInfo {
  home: number;
  away: number;
  points: number | null;
}

export interface TipSubmitRequest {
  matchId: number;
  tipHome: number;
  tipAway: number;
}

export interface TipSubmitResponse {
  success: boolean;
  tip: TipInfo;
  message?: string;
}

export interface LeaderboardResponse {
  entries: DbLeaderboardEntry[];
  currentUserRank: number | null;
  currentUserId: string | null;
  totalParticipants: number;
  lastUpdated: string;
}

export interface UserStatsResponse {
  userId: string;
  fullName: string;
  team: string | null;
  totalPoints: number;
  rank: number | null;
  tipsCount: number;
  exactCount: number;
  tendencyCount: number;
  wrongCount: number;
  accuracy: number;
}

// ============================================================
// football-data.org API Types
// ============================================================
export interface FootballApiMatch {
  id: number;
  utcDate: string;
  status: MatchStatus;
  stage: string;
  group: string | null;
  homeTeam: {
    id: number;
    name: string;
    shortName: string;
    tla: string;
    crest: string;
  };
  awayTeam: {
    id: number;
    name: string;
    shortName: string;
    tla: string;
    crest: string;
  };
  score: {
    fullTime: { home: number | null; away: number | null };
    halfTime: { home: number | null; away: number | null };
  };
}

export interface FootballApiResponse {
  matches: FootballApiMatch[];
  resultSet: {
    count: number;
    competitions: string;
    first: string;
    last: string;
    played: number;
  };
}

// ============================================================
// Points calculation
// ============================================================
export type PointsResult = 'exact' | 'tendency' | 'wrong' | 'pending';

export interface PointsCalculation {
  result: PointsResult;
  points: number;
}

export const POINTS = {
  EXACT: 3,
  TENDENCY: 1,
  WRONG: 0,
} as const;
