import 'package:flutter/material.dart';
import '../../../../../shared/design_system/unionflow_design_system.dart';
import '../../../../../shared/widgets/core_card.dart';
import '../../../../members/presentation/pages/members_page_wrapper.dart';
import '../../../../events/presentation/pages/events_page_wrapper.dart';
import '../../../../contributions/presentation/pages/contributions_page_wrapper.dart';
import '../../../../reports/presentation/pages/reports_page_wrapper.dart';
import '../../../../settings/presentation/pages/system_settings_page.dart';

/// Widget de raccourcis rapides pour le dashboard
class DashboardShortcutsWidget extends StatelessWidget {
  final List<DashboardShortcut>? customShortcuts;
  final int maxShortcuts;

  const DashboardShortcutsWidget({
    super.key,
    this.customShortcuts,
    this.maxShortcuts = 6,
  });

  @override
  Widget build(BuildContext context) {
    final shortcuts = customShortcuts ?? _getDefaultShortcuts(context);
    final displayShortcuts = shortcuts.take(maxShortcuts).toList();

    return CoreCard(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildHeader(),
          const SizedBox(height: 16),
          _buildShortcutsGrid(displayShortcuts),
        ],
      ),
    );
  }

  Widget _buildHeader() {
    return Row(
      children: [
        const Icon(
          Icons.flash_on_outlined,
          color: AppColors.primary,
          size: 18,
        ),
        const SizedBox(width: 8),
        Expanded(
          child: Text(
            'ACTIONS RAPIDES',
            style: AppTypography.subtitleSmall.copyWith(
              fontWeight: FontWeight.bold,
              letterSpacing: 1.1,
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildShortcutsGrid(List<DashboardShortcut> shortcuts) {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
        childAspectRatio: 0.9,
      ),
      itemCount: shortcuts.length,
      itemBuilder: (context, index) {
        return _buildShortcutItem(shortcuts[index]);
      },
    );
  }

  Widget _buildShortcutItem(DashboardShortcut shortcut) {
    return GestureDetector(
      onTap: shortcut.onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            padding: const EdgeInsets.all(12),
            decoration: BoxDecoration(
              color: shortcut.color.withOpacity(0.1),
              borderRadius: BorderRadius.circular(12),
            ),
            child: Icon(
              shortcut.icon,
              color: shortcut.color,
              size: 20,
            ),
          ),
          const SizedBox(height: 8),
          Text(
            shortcut.title,
            style: AppTypography.subtitleSmall.copyWith(
              color: AppColors.textPrimary,
              fontSize: 9,
              fontWeight: FontWeight.w500,
            ),
            textAlign: TextAlign.center,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }

  List<DashboardShortcut> _getDefaultShortcuts(BuildContext context) {
    return [
      DashboardShortcut(
        title: 'Nouveau\nMembre',
        icon: Icons.person_add_outlined,
        color: AppColors.success,
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const MembersPageWrapper(),
            ),
          );
        },
      ),
      DashboardShortcut(
        title: 'Créer\nÉvénement',
        icon: Icons.event_available_outlined,
        color: AppColors.primary,
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const EventsPageWrapper(),
            ),
          );
        },
      ),
      DashboardShortcut(
        title: 'Ajouter\nContribution',
        icon: Icons.account_balance_wallet_outlined,
        color: AppColors.primaryDark,
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const CotisationsPageWrapper(),
            ),
          );
        },
      ),
      DashboardShortcut(
        title: 'Générer\nRapport',
        icon: Icons.assessment_outlined,
        color: AppColors.info,
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const ReportsPageWrapper(),
            ),
          );
        },
      ),
      DashboardShortcut(
        title: 'Paramètres',
        icon: Icons.settings_outlined,
        color: AppColors.textSecondary,
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(
              builder: (context) => const SystemSettingsPage(),
            ),
          );
        },
      ),
    ];
  }
}

class DashboardShortcut {
  final String title;
  final IconData icon;
  final Color color;
  final VoidCallback onTap;
  final String? badge;
  final Color? badgeColor;

  const DashboardShortcut({
    required this.title,
    required this.icon,
    required this.color,
    required this.onTap,
    this.badge,
    this.badgeColor,
  });
}
