import 'package:flutter/material.dart';
import 'package:playmaker/classe/theme.dart'; // 👇 IMPORT DO TEMA
import '../models/team_model.dart';
import '../models/person_model.dart';
import '../utils/size_extension.dart'; // 👇 O NOSSO SUPERPODER!

// --- CABEÇALHO ---
class StatsHeader extends StatelessWidget {
  final Team team;

  const StatsHeader({super.key, required this.team});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        top: 50 * context.sf, 
        left: 20 * context.sf, 
        right: 20 * context.sf, 
        bottom: 20 * context.sf
      ),
      decoration: BoxDecoration(
        color: AppTheme.primaryRed, // 👇 Usando a cor do teu tema!
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(30 * context.sf), 
          bottomRight: Radius.circular(30 * context.sf)
        ),
      ),
      child: Row(
        children: [
          IconButton(
            icon: Icon(Icons.arrow_back, color: Colors.white, size: 24 * context.sf),
            onPressed: () => Navigator.pop(context),
          ),
          SizedBox(width: 10 * context.sf),
          CircleAvatar(
            radius: 24 * context.sf,
            backgroundColor: Colors.white24,
            backgroundImage: (team.imageUrl.isNotEmpty && team.imageUrl.startsWith('http'))
                ? NetworkImage(team.imageUrl)
                : null,
            child: (team.imageUrl.isEmpty || !team.imageUrl.startsWith('http'))
                ? Text(
                    team.imageUrl.isEmpty ? "🛡️" : team.imageUrl,
                    style: TextStyle(fontSize: 20 * context.sf),
                  )
                : null,
          ),
          SizedBox(width: 15 * context.sf),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  team.name, 
                  style: TextStyle(color: Colors.white, fontSize: 20 * context.sf, fontWeight: FontWeight.bold),
                  overflow: TextOverflow.ellipsis,
                ),
                Text(
                  team.season, 
                  style: TextStyle(color: Colors.white70, fontSize: 14 * context.sf)
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

// --- CARD DE RESUMO ---
class StatsSummaryCard extends StatelessWidget {
  final int total;

  const StatsSummaryCard({super.key, required this.total});

  @override
  Widget build(BuildContext context) {
    // 👇 Adaptável ao Modo Escuro
    final cardColor = Theme.of(context).brightness == Brightness.dark 
        ? const Color(0xFF1E1E1E) 
        : Colors.white;

    return Card(
      elevation: 4,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20 * context.sf)),
      child: Container(
        padding: EdgeInsets.all(20 * context.sf),
        decoration: BoxDecoration(
          color: cardColor,
          borderRadius: BorderRadius.circular(20 * context.sf),
          border: Border.all(color: Colors.grey.withOpacity(0.15)),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                Icon(Icons.groups, color: AppTheme.primaryRed, size: 28 * context.sf), // 👇 Cor do tema
                SizedBox(width: 10 * context.sf),
                Text(
                  "Total de Membros", 
                  style: TextStyle(
                    color: Theme.of(context).colorScheme.onSurface, // 👇 Adaptável
                    fontSize: 16 * context.sf,
                    fontWeight: FontWeight.w600
                  )
                ),
              ],
            ),
            Text(
              "$total", 
              style: TextStyle(
                color: Theme.of(context).colorScheme.onSurface, // 👇 Adaptável
                fontSize: 28 * context.sf, 
                fontWeight: FontWeight.bold
              )
            ),
          ],
        ),
      ),
    );
  }
}

// --- TÍTULO DE SECÇÃO ---
class StatsSectionTitle extends StatelessWidget {
  final String title;

  const StatsSectionTitle({super.key, required this.title});

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          title, 
          style: TextStyle(
            fontSize: 18 * context.sf, 
            fontWeight: FontWeight.bold, 
            color: Theme.of(context).colorScheme.onSurface // 👇 Adaptável
          )
        ),
        Divider(color: Colors.grey.withOpacity(0.3)),
      ],
    );
  }
}

// --- CARD DA PESSOA (JOGADOR/TREINADOR) ---
class PersonCard extends StatelessWidget {
  final Person person;
  final bool isCoach;
  final VoidCallback onEdit;
  final VoidCallback onDelete;

  const PersonCard({
    super.key,
    required this.person,
    required this.isCoach,
    required this.onEdit,
    required this.onDelete,
  });

  @override
  Widget build(BuildContext context) {
    // 👇 Cores adaptáveis para o Card
    final defaultBg = Theme.of(context).brightness == Brightness.dark 
        ? const Color(0xFF1E1E1E) 
        : Colors.white;
        
    final coachBg = Theme.of(context).brightness == Brightness.dark 
        ? AppTheme.warningAmber.withOpacity(0.1) // Amarelo escuro se for modo noturno
        : const Color(0xFFFFF9C4); // Amarelo claro original

    return Card(
      margin: EdgeInsets.only(top: 12 * context.sf),
      elevation: 2,
      color: isCoach ? coachBg : defaultBg,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15 * context.sf)),
      child: ListTile(
        contentPadding: EdgeInsets.symmetric(horizontal: 16 * context.sf, vertical: 4 * context.sf),
        leading: isCoach
            ? CircleAvatar(
                radius: 22 * context.sf,
                backgroundColor: AppTheme.warningAmber, // 👇 Cor do tema
                child: Icon(Icons.person, color: Colors.white, size: 24 * context.sf)
              )  
            : Container(
                width: 45 * context.sf,
                height: 45 * context.sf,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  color: AppTheme.primaryRed.withOpacity(0.1), // 👇 Cor do tema
                  borderRadius: BorderRadius.circular(10 * context.sf)
                ),
                child: Text(
                  person.number ?? "J", 
                  style: TextStyle(
                    color: AppTheme.primaryRed, // 👇 Cor do tema
                    fontWeight: FontWeight.bold, 
                    fontSize: 16 * context.sf
                  )
                ),
              ),
        title: Text(
          person.name, 
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: 16 * context.sf,
            color: Theme.of(context).colorScheme.onSurface, // 👇 Adaptável
          )
        ),
        trailing: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            IconButton(
              icon: Icon(Icons.edit_outlined, color: Colors.blue, size: 22 * context.sf),
              onPressed: onEdit,
            ),
            IconButton(
              icon: Icon(Icons.delete_outline, color: AppTheme.primaryRed, size: 22 * context.sf), // 👇 Cor do tema
              onPressed: onDelete,
            ),
          ],
        ),
      ),
    );
  }
}