
interface TeamResult {
  team_id: string;
  team_name: string;
  totalIncome: number;
  battle_list: Array<{
    battleNo: number;
    seq: string;
    score: number;
    rank: number;
    income?: number;
    totalIncome?: number;
    totalElimination?: number;
    user_result_list: Array<{ uid: string }>;
  }>;
  isMyTeam?: boolean;
  rank?: number;
}

interface SubwayResults {
  totalTeamOrPersonalList: TeamResult[];
  myTotalTeamResult: TeamResult | null;
}

interface BattleResult {
  team_id: string;
  team_name: string;
  score: number;
  rank: number;
  income?: number;
  total_income?: number;
  total_elimination?: number;
  user_result_list: Array<{ uid: string }>;
}

interface BattleEntry {
  battleNo: number;
  seq: string;
  score: number;
  rank: number;
  user_result_list: Array<{ uid: string }>;
  income?: number;
  totalIncome?: number;
  totalElimination?: number;
}

// 设置地铁模式总排名
export const processSubwayResults = (
  data: { subway_result_list?: Array<any> },
  userId: string,

): SubwayResults | null => {
  if (!data.subway_result_list) {
    return null;
  }

  const teams: Record<string, TeamResult> = {};
  let myTeamTotal: TeamResult | null = null;

  data.subway_result_list.forEach((battle) => {
    const battleNo: keyof typeof MATCH_SEQ_MAP = battle.battle_no;


    battle.team_result_list.forEach((teamResult: BattleResult) => {
      const teamId = teamResult.team_id;
      const isMyTeam = teamResult.user_result_list.some(user => user.uid === userId);

      if (!teams[teamId]) {
        teams[teamId] = {
          team_id: teamId,
          team_name: teamResult.team_name,
          totalIncome: 0,
          battle_list: [],
        };
      }

      const battleEntry: BattleEntry = {
        battleNo,
        seq: MATCH_SEQ_MAP[battleNo],
        score: teamResult.score,
        rank: teamResult.rank,
        user_result_list: teamResult.user_result_list,
        income: teamResult.income,
        totalIncome: teamResult.total_income,
        totalElimination: teamResult.total_elimination,
      };

      teams[teamId].totalIncome += teamResult.total_income || 0;
      teams[teamId].battle_list.push(battleEntry);
      teams[teamId].isMyTeam = isMyTeam;
    });
  });

  // 排序战斗列表和团队总收入
  const teamList = Object.values(teams)
    .map(team => ({
      ...team,
      battle_list: team.battle_list.sort((a, b) => a.battleNo - b.battleNo),
    }))
    .sort((a, b) => b.totalIncome - a.totalIncome);

  // 添加排名
  teamList.forEach((team, index) => {
    team.rank = index + 1;
    if (team.isMyTeam) {
      myTeamTotal = { ...team };
    }
  });

  return {
    totalTeamOrPersonalList: teamList,
    myTotalTeamResult: myTeamTotal,
  };
};


const MATCH_SEQ_MAP = {
  1: '第一场',
  2: '第二场',
  3: '第三场',
  4: '第四场',
  5: '第五场',
  6: '第六场',
  7: '第七场',
  8: '第八场',
  9: '第九场',
  10: '第十场',
  11: '第十一场',
  12: '第十二场',
  13: '第十三场',
  14: '第十四场',
  15: '第十五场',
  16: '第十六场',
  17: '第十七场',
  18: '第十八场',
  19: '第十九场',
  20: '第二十场',
  21: '第二十一场',
  22: '第二十二场',
  23: '第二十三场',
  24: '第二十四场',
  25: '第二十五场',
  26: '第二十六场',
  27: '第二十七场',
  28: '第二十八场',
  29: '第二十九场',
  30: '第三十场',
  31: '第三十一场',
  32: '第三十二场',
  33: '第三十三场',
  34: '第三十四场',
  35: '第三十五场',
  36: '第三十六场',
  37: '第三十七场',
  38: '第三十八场',
  39: '第三十九场',
  40: '第四十场',
};
