/// Widget de menu latéral (drawer) du dashboard
library dashboard_drawer;

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../shared/design_system/unionflow_design_system.dart';
import '../../../../shared/widgets/core_card.dart';
import '../../../../shared/widgets/mini_avatar.dart';

import '../../../authentication/presentation/bloc/auth_bloc.dart';
import '../../../profile/presentation/pages/profile_page_wrapper.dart';
import '../../../notifications/presentation/pages/notifications_page_wrapper.dart';
import '../../../help/presentation/pages/help_support_page.dart';
import '../../../about/presentation/pages/about_page.dart';
/// Drawer principal — Mon Espace
/// Profil · Notifications · Aide · À propos · Déconnexion
class DashboardDrawer extends StatelessWidget {
  // ignore: unused_field
  final Function(String route)? onNavigate;
  final VoidCallback? onLogout;

  const DashboardDrawer({super.key, this.onNavigate, this.onLogout});

  @override
  Widget build(BuildContext context) {
    final isDark = Theme.of(context).brightness == Brightness.dark;

    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, authState) {
        if (authState is! AuthAuthenticated) return const Drawer();

        return Drawer(
          backgroundColor:
              isDark ? AppColors.darkSurface : AppColors.lightBackground,
          child: SafeArea(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  _buildUserProfile(context, authState),
                  const SizedBox(height: SpacingTokens.md),
                  _buildSectionTitle(context, 'Mon Espace'),
                  _buildOptionTile(
                    context: context,
                    icon: Icons.person,
                    title: 'Mon Profil',
                    subtitle: 'Modifier mes informations',
                    onTap: () => Navigator.of(context).push(
                      MaterialPageRoute(builder: (_) => const ProfilePageWrapper()),
                    ),
                  ),
                  _buildOptionTile(
                    context: context,
                    icon: Icons.notifications,
                    title: 'Notifications',
                    subtitle: 'Gérer les notifications',
                    onTap: () => Navigator.of(context).push(
                      MaterialPageRoute(
                          builder: (_) => const NotificationsPageWrapper()),
                    ),
                  ),
                  _buildOptionTile(
                    context: context,
                    icon: Icons.help,
                    title: 'Aide & Support',
                    subtitle: 'Documentation et support',
                    onTap: () => Navigator.of(context).push(
                      MaterialPageRoute(builder: (_) => const HelpSupportPage()),
                    ),
                  ),
                  _buildOptionTile(
                    context: context,
                    icon: Icons.info,
                    title: 'À propos',
                    subtitle: 'Version et informations',
                    onTap: () => Navigator.of(context).push(
                      MaterialPageRoute(builder: (_) => const AboutPage()),
                    ),
                  ),
                  const SizedBox(height: SpacingTokens.md),
                  _buildOptionTile(
                    context: context,
                    icon: Icons.logout,
                    title: 'Déconnexion',
                    subtitle: 'Se déconnecter de l\'application',
                    accentColor: AppColors.error,
                    onTap: () {
                      Navigator.pop(context);
                      context.read<AuthBloc>().add(const AuthLogoutRequested());
                    },
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _buildUserProfile(BuildContext context, AuthAuthenticated state) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final nameColor =
        isDark ? AppColors.textPrimaryDark : AppColors.textPrimaryLight;
    final emailColor =
        isDark ? AppColors.textSecondaryDark : AppColors.textSecondaryLight;
    final roleColor =
        isDark ? AppColors.brandGreenLight : AppColors.primaryGreen;

    return CoreCard(
      child: Row(
        children: [
          MiniAvatar(
            fallbackText: state.user.firstName.isNotEmpty
                ? state.user.firstName[0].toUpperCase()
                : 'U',
            size: 32,
            imageUrl: state.user.avatar,
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${state.user.firstName} ${state.user.lastName}',
                  style: AppTypography.actionText.copyWith(color: nameColor),
                ),
                Text(
                  state.effectiveRole.displayName.toUpperCase(),
                  style: AppTypography.badgeText.copyWith(
                    color: roleColor,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  state.user.email,
                  style: AppTypography.subtitleSmall.copyWith(color: emailColor),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSectionTitle(BuildContext context, String title) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    return Padding(
      padding: const EdgeInsets.only(top: 10, bottom: 6, left: 4),
      child: Text(
        title.toUpperCase(),
        style: AppTypography.subtitleSmall.copyWith(
          fontWeight: FontWeight.bold,
          letterSpacing: 1.1,
          color: isDark ? AppColors.textSecondaryDark : AppColors.textSecondaryLight,
        ),
      ),
    );
  }

  Widget _buildOptionTile({
    required BuildContext context,
    required IconData icon,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
    Color? accentColor,
  }) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    final accent = accentColor ?? AppColors.primaryGreen;
    final titleColor = accentColor != null
        ? accentColor
        : (isDark ? AppColors.textPrimaryDark : AppColors.textPrimaryLight);
    final subtitleColor =
        isDark ? AppColors.textSecondaryDark : AppColors.textSecondaryLight;
    final chevronColor =
        isDark ? AppColors.textSecondaryDark : AppColors.textSecondaryLight;

    return CoreCard(
      margin: const EdgeInsets.only(bottom: 8),
      onTap: onTap,
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(6),
            decoration: BoxDecoration(
              color: accent.withOpacity(isDark ? 0.2 : 0.1),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(icon, color: accent, size: 16),
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: AppTypography.actionText.copyWith(color: titleColor),
                ),
                Text(
                  subtitle,
                  style: AppTypography.subtitleSmall.copyWith(color: subtitleColor),
                ),
              ],
            ),
          ),
          Icon(Icons.chevron_right, color: chevronColor, size: 16),
        ],
      ),
    );
  }
}
