class Game {
  final String id;
  final String userId;
  final String myTeam;
  final String opponentTeam;
  final String myScore; 
  final String opponentScore;
  final String season;
  final String status;
  final DateTime gameDate;
  
  // Novos campos que estão na tua base de dados
  final int remainingSeconds;
  final int myTimeouts;
  final int oppTimeouts;
  final int currentQuarter;
  final String topPtsName;
  final String topAstName;
  final String topRbsName;
  final String topDefName;
  final String mvpName;

  Game({
    required this.id,
    required this.userId,
    required this.myTeam,
    required this.opponentTeam,
    required this.myScore,
    required this.opponentScore,
    required this.season,
    required this.status,
    required this.gameDate,
    required this.remainingSeconds,
    required this.myTimeouts,
    required this.oppTimeouts,
    required this.currentQuarter,
    required this.topPtsName,
    required this.topAstName,
    required this.topRbsName,
    required this.topDefName,
    required this.mvpName,
  });

  // 👇 A MÁGICA ACONTECE AQUI: Lemos os dados e protegemos os NULLs
  factory Game.fromMap(Map<String, dynamic> json) {
    return Game(
      id: json['id']?.toString() ?? '',
      userId: json['user_id']?.toString() ?? '',
      myTeam: json['my_team']?.toString() ?? 'Minha Equipa',
      opponentTeam: json['opponent_team']?.toString() ?? 'Adversário',
      myScore: (json['my_score'] ?? 0).toString(), // Protege NULL e converte Int4 para String
      opponentScore: (json['opponent_score'] ?? 0).toString(),
      season: json['season']?.toString() ?? '---',
      status: json['status']?.toString() ?? 'Decorrer',
      gameDate: json['game_date'] != null ? DateTime.tryParse(json['game_date']) ?? DateTime.now() : DateTime.now(),
      
      // Proteção para os Inteiros (se for NULL, assume 0)
      remainingSeconds: json['remaining_seconds'] as int? ?? 600, // 600s = 10 minutos
      myTimeouts: json['my_timeouts'] as int? ?? 0,
      oppTimeouts: json['opp_timeouts'] as int? ?? 0,
      currentQuarter: json['current_quarter'] as int? ?? 1,
      
      // Proteção para os Nomes (se for NULL, assume '---')
      topPtsName: json['top_pts_name']?.toString() ?? '---',
      topAstName: json['top_ast_name']?.toString() ?? '---',
      topRbsName: json['top_rbs_name']?.toString() ?? '---',
      topDefName: json['top_def_name']?.toString() ?? '---',
      mvpName: json['mvp_name']?.toString() ?? '---',
    );
  }
}