import 'package:flutter/material.dart';
import 'package:geosector_app/core/data/models/membre_model.dart';
import 'package:geosector_app/presentation/widgets/membre_row_widget.dart';

class MembreTableWidget extends StatelessWidget {
  final List<MembreModel> membres;
  final Function(MembreModel)? onEdit;
  final Function(MembreModel)? onDelete;
  final bool showHeader;
  final double? height;
  final EdgeInsetsGeometry? padding;

  const MembreTableWidget({
    Key? key,
    required this.membres,
    this.onEdit,
    this.onDelete,
    this.showHeader = true,
    this.height,
    this.padding,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);

    return Container(
      height: height,
      padding: padding ?? const EdgeInsets.all(16.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.0),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 4,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // En-tête du tableau
          if (showHeader)
            Padding(
              padding:
                  const EdgeInsets.only(bottom: 16.0, left: 16.0, right: 16.0),
              child: Row(
                children: [
                  // ID
                  Expanded(
                    flex: 1,
                    child: Text(
                      'ID',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),

                  // Prénom (firstName)
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Prénom',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),

                  // Nom (name)
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Nom',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),

                  // Secteur (sectName)
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Secteur',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),

                  // Rôle (fkRole)
                  Expanded(
                    flex: 1,
                    child: Text(
                      'Rôle',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ),

                  // Actions
                  Expanded(
                    flex: 2,
                    child: Text(
                      'Actions',
                      style: theme.textTheme.titleSmall?.copyWith(
                        fontWeight: FontWeight.bold,
                      ),
                      textAlign: TextAlign.end,
                    ),
                  ),
                ],
              ),
            ),

          // Liste des membres
          Expanded(
            child: membres.isEmpty
                ? Center(
                    child: Text(
                      'Aucun membre disponible',
                      style: theme.textTheme.bodyMedium,
                    ),
                  )
                : ListView.separated(
                    itemCount: membres.length,
                    separatorBuilder: (context, index) =>
                        const SizedBox(height: 8.0),
                    itemBuilder: (context, index) {
                      final membre = membres[index];
                      return MembreRowWidget(
                        membre: membre,
                        onEdit: onEdit != null ? () => onEdit!(membre) : null,
                        onDelete:
                            onDelete != null ? () => onDelete!(membre) : null,
                      );
                    },
                  ),
          ),
        ],
      ),
    );
  }
}
