import 'package:flutter/material.dart';
import 'package:playmaker/pages/PlacarPage.dart'; 
import 'package:playmaker/classe/theme.dart'; // 👇 IMPORT DO TEMA!
import '../controllers/team_controller.dart';
import '../controllers/game_controller.dart'; 

class GameResultCard extends StatelessWidget {
  final String gameId, myTeam, opponentTeam, myScore, opponentScore, status, season;
  final String? myTeamLogo, opponentTeamLogo; 
  final double sf; 

  const GameResultCard({
    super.key, required this.gameId, required this.myTeam, required this.opponentTeam,
    required this.myScore, required this.opponentScore, required this.status, required this.season,
    this.myTeamLogo, this.opponentTeamLogo, required this.sf, 
  });

  @override
  Widget build(BuildContext context) {
    // 👇 Puxa as cores de fundo dependendo do Modo (Claro/Escuro)
    final bgColor = Theme.of(context).colorScheme.surface;
    final textColor = Theme.of(context).colorScheme.onSurface;

    return Container(
      margin: EdgeInsets.only(bottom: 16 * sf),
      padding: EdgeInsets.all(16 * sf),
      decoration: BoxDecoration(
        color: bgColor, // Usa a cor do tema
        borderRadius: BorderRadius.circular(20 * sf),
        boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 10 * sf)],
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(child: _buildTeamInfo(myTeam, AppTheme.primaryRed, myTeamLogo, sf, textColor)), // Usa o primaryRed
          _buildScoreCenter(context, gameId, sf),
          Expanded(child: _buildTeamInfo(opponentTeam, textColor, opponentTeamLogo, sf, textColor)),
        ],
      ),
    );
  }

  Widget _buildTeamInfo(String name, Color color, String? logoUrl, double sf, Color textColor) {
    return Column(
      children: [
        CircleAvatar(
          radius: 24 * sf, 
          backgroundColor: color,
          backgroundImage: (logoUrl != null && logoUrl.isNotEmpty) ? NetworkImage(logoUrl) : null,
          child: (logoUrl == null || logoUrl.isEmpty) ? Icon(Icons.shield, color: Colors.white, size: 24 * sf) : null,
        ),
        SizedBox(height: 6 * sf),
        Text(name, 
          style: TextStyle(fontWeight: FontWeight.bold, fontSize: 13 * sf, color: textColor), // Adapta à noite/dia
          textAlign: TextAlign.center, overflow: TextOverflow.ellipsis, maxLines: 2, 
        ),
      ],
    );
  }

  Widget _buildScoreCenter(BuildContext context, String id, double sf) {
    final textColor = Theme.of(context).colorScheme.onSurface;

    return Column(
      children: [
        Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            _scoreBox(myScore, AppTheme.successGreen, sf), // Verde do tema
            Text(" : ", style: TextStyle(fontWeight: FontWeight.bold, fontSize: 22 * sf, color: textColor)),
            _scoreBox(opponentScore, Colors.grey, sf),
          ],
        ),
        SizedBox(height: 10 * sf),
        TextButton.icon(
          onPressed: () => Navigator.push(context, MaterialPageRoute(builder: (context) => PlacarPage(gameId: id, myTeam: myTeam, opponentTeam: opponentTeam))),
          icon: Icon(Icons.play_circle_fill, size: 18 * sf, color: AppTheme.primaryRed),
          label: Text("RETORNAR", style: TextStyle(fontSize: 11 * sf, color: AppTheme.primaryRed, fontWeight: FontWeight.bold)),
          style: TextButton.styleFrom(
            backgroundColor: AppTheme.primaryRed.withOpacity(0.1),
            padding: EdgeInsets.symmetric(horizontal: 14 * sf, vertical: 8 * sf),
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20 * sf)),
            visualDensity: VisualDensity.compact,
          ),
        ),
        SizedBox(height: 6 * sf),
        Text(status, style: TextStyle(fontSize: 12 * sf, color: Colors.blue, fontWeight: FontWeight.bold)),
      ],
    );
  }

  Widget _scoreBox(String pts, Color c, double sf) => Container(
    padding: EdgeInsets.symmetric(horizontal: 12 * sf, vertical: 6 * sf),
    decoration: BoxDecoration(color: c, borderRadius: BorderRadius.circular(8 * sf)),
    child: Text(pts, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 16 * sf)),
  );
}