/// 参赛队伍详情
class CompetitionTeamDetail {
  String? scheduleId;
  String? itemId;
  String? eventId;
  String? itemName;
  String? groupName;
  String? matchPlace;
  String? matchStartTime;
  String? matchEndTime;
  List<Matchup>? matchups;

  CompetitionTeamDetail({
    this.scheduleId,
    this.itemId,
    this.eventId,
    this.itemName,
    this.groupName,
    this.matchPlace,
    this.matchStartTime,
    this.matchEndTime,
    this.matchups,
  });

  factory CompetitionTeamDetail.fromJson(Map<String, dynamic> json) =>
      CompetitionTeamDetail(
        scheduleId: json['scheduleId']?.toString(),
        itemId: json['itemId']?.toString(),
        eventId: json['eventId']?.toString(),
        itemName: json['itemName']?.toString(),
        groupName: json['groupName']?.toString(),
        matchPlace: json['matchPlace']?.toString(),
        matchStartTime: json['matchStartTime']?.toString(),
        matchEndTime: json['matchEndTime']?.toString(),
        matchups: json['matchups'] == null
            ? const []
            : List<Matchup>.from(
                (json['matchups'] as List).whereType<Map>().map(
                  (x) => Matchup.fromJson(Map<String, dynamic>.from(x)),
                ),
              ),
      );

  /// 兼容 data 为对象或数组（取首项）
  factory CompetitionTeamDetail.fromResponse(dynamic json) {
    if (json is List) {
      if (json.isEmpty) return CompetitionTeamDetail(matchups: const []);
      final first = json.first;
      if (first is Map) {
        return CompetitionTeamDetail.fromJson(Map<String, dynamic>.from(first));
      }
      return CompetitionTeamDetail(matchups: const []);
    }
    if (json is Map) {
      return CompetitionTeamDetail.fromJson(Map<String, dynamic>.from(json));
    }
    return CompetitionTeamDetail(matchups: const []);
  }

  Map<String, dynamic> toJson() => {
    'scheduleId': scheduleId,
    'itemId': itemId,
    'eventId': eventId,
    'itemName': itemName,
    'groupName': groupName,
    'matchPlace': matchPlace,
    'matchStartTime': matchStartTime,
    'matchEndTime': matchEndTime,
    'matchups': matchups == null
        ? []
        : List<dynamic>.from(matchups!.map((x) => x.toJson())),
  };
}

class Matchup {
  String? matchTitle;
  String? opponentId;
  List<Team>? teamA;
  List<Team>? teamB;

  Matchup({this.matchTitle, this.opponentId, this.teamA, this.teamB});

  factory Matchup.fromJson(Map<String, dynamic> json) => Matchup(
    matchTitle: json['matchTitle']?.toString(),
    opponentId: json['opponentId']?.toString(),
    teamA: json['teamA'] == null
        ? const []
        : List<Team>.from(
            (json['teamA'] as List).whereType<Map>().map(
              (x) => Team.fromJson(Map<String, dynamic>.from(x)),
            ),
          ),
    teamB: json['teamB'] == null
        ? const []
        : List<Team>.from(
            (json['teamB'] as List).whereType<Map>().map(
              (x) => Team.fromJson(Map<String, dynamic>.from(x)),
            ),
          ),
  );

  Map<String, dynamic> toJson() => {
    'matchTitle': matchTitle,
    'opponentId': opponentId,
    'teamA': teamA == null
        ? []
        : List<dynamic>.from(teamA!.map((x) => x.toJson())),
    'teamB': teamB == null
        ? []
        : List<dynamic>.from(teamB!.map((x) => x.toJson())),
  };
}

class Team {
  String? teamName;
  List<Player>? players;

  Team({this.teamName, this.players});

  String get playerNames => (players ?? const <Player>[])
      .map((player) => player.name?.trim() ?? '')
      .where((name) => name.isNotEmpty)
      .join('、');

  /// 队长 Player.id；无队长则取首个选手
  String? get leaderUserId {
    final list = players ?? const <Player>[];
    for (final player in list) {
      final id = player.id?.trim() ?? '';
      if (player.isLeader == true && id.isNotEmpty) return id;
    }
    if (list.isEmpty) return null;
    final firstId = list.first.id?.trim() ?? '';
    return firstId.isEmpty ? null : firstId;
  }

  factory Team.fromJson(Map<String, dynamic> json) => Team(
    teamName: json['teamName']?.toString(),
    players: json['players'] == null
        ? const []
        : List<Player>.from(
            (json['players'] as List).whereType<Map>().map(
              (x) => Player.fromJson(Map<String, dynamic>.from(x)),
            ),
          ),
  );

  Map<String, dynamic> toJson() => {
    'teamName': teamName,
    'players': players == null
        ? []
        : List<dynamic>.from(players!.map((x) => x.toJson())),
  };
}

class Player {
  String? id;
  String? name;
  bool? isLeader;

  Player({this.id, this.name, this.isLeader});

  factory Player.fromJson(Map<String, dynamic> json) => Player(
    id: json['id']?.toString(),
    name: json['name']?.toString(),
    isLeader: json['isLeader'] as bool?,
  );

  Map<String, dynamic> toJson() => {
    'id': id,
    'name': name,
    'isLeader': isLeader,
  };
}
