import 'package:flutter/material.dart';
import '../tokens/app_colors.dart';
import '../tokens/unionflow_colors.dart';

/// Type d'export disponible
enum ExportType {
  pdf('PDF', Icons.picture_as_pdf),
  excel('Excel', Icons.table_chart),
  csv('CSV', Icons.description);

  final String label;
  final IconData icon;
  const ExportType(this.label, this.icon);
}

/// Bouton d'export avec options — adaptatif dark/light via AppColors
class UnionExportButton extends StatelessWidget {
  final Function(ExportType) onExport;
  final bool isLoading;

  const UnionExportButton({
    super.key,
    required this.onExport,
    this.isLoading = false,
  });

  @override
  Widget build(BuildContext context) {
    final isDark   = Theme.of(context).brightness == Brightness.dark;
    final bgPopup  = isDark ? AppColors.surfaceDark     : AppColors.surface;
    final textColor= isDark ? AppColors.textPrimaryDark : AppColors.textPrimary;

    return PopupMenuButton<ExportType>(
      icon: Container(
        padding: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          gradient: UnionFlowColors.primaryGradient,
          borderRadius: BorderRadius.circular(12),
          boxShadow: UnionFlowColors.greenGlowShadow,
        ),
        child: isLoading
            ? const SizedBox(
                width: 20,
                height: 20,
                child: CircularProgressIndicator(
                  strokeWidth: 2,
                  valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                ),
              )
            : const Icon(Icons.download, color: Colors.white, size: 20),
      ),
      itemBuilder: (context) => ExportType.values.map((type) {
        return PopupMenuItem<ExportType>(
          value: type,
          child: Row(
            children: [
              Icon(type.icon, size: 20, color: UnionFlowColors.unionGreen),
              const SizedBox(width: 12),
              Text(
                'Exporter en ${type.label}',
                style: TextStyle(
                  fontSize: 13,
                  fontWeight: FontWeight.w600,
                  color: textColor,
                ),
              ),
            ],
          ),
        );
      }).toList(),
      onSelected: onExport,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      elevation: 8,
      color: bgPopup,
    );
  }
}

/// Dialog pour confirmer l'export — adaptatif dark/light via AppColors
class ExportConfirmDialog extends StatelessWidget {
  final ExportType exportType;
  final VoidCallback onConfirm;
  final String? message;

  const ExportConfirmDialog({
    super.key,
    required this.exportType,
    required this.onConfirm,
    this.message,
  });

  @override
  Widget build(BuildContext context) {
    final isDark        = Theme.of(context).brightness == Brightness.dark;
    final bgColor       = isDark ? AppColors.surfaceDark      : AppColors.surface;
    final textPrimary   = isDark ? AppColors.textPrimaryDark  : AppColors.textPrimary;
    final textSecondary = isDark ? AppColors.textSecondaryDark: AppColors.textSecondary;

    return AlertDialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
      backgroundColor: bgColor,
      title: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(10),
            decoration: BoxDecoration(
              color: UnionFlowColors.unionGreen.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Icon(exportType.icon, color: UnionFlowColors.unionGreen, size: 24),
          ),
          const SizedBox(width: 12),
          Expanded(
            child: Text(
              'Exporter en ${exportType.label}',
              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700, color: textPrimary),
            ),
          ),
        ],
      ),
      content: Text(
        message ?? 'Voulez-vous exporter le rapport au format ${exportType.label}?',
        style: TextStyle(fontSize: 14, color: textSecondary),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: Text(
            'Annuler',
            style: TextStyle(color: textSecondary, fontWeight: FontWeight.w600),
          ),
        ),
        ElevatedButton(
          onPressed: () {
            Navigator.pop(context);
            onConfirm();
          },
          style: ElevatedButton.styleFrom(
            backgroundColor: UnionFlowColors.unionGreen,
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
            elevation: 0,
          ),
          child: const Text('Confirmer', style: TextStyle(fontWeight: FontWeight.w700)),
        ),
      ],
    );
  }
}
