import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../features/authentication/presentation/bloc/auth_bloc.dart';
import '../../features/authentication/data/models/user_role.dart';
import '../../shared/design_system/unionflow_design_system.dart';
import '../../shared/widgets/core_card.dart';
import '../../shared/widgets/mini_avatar.dart';

import '../../features/admin/presentation/pages/user_management_page.dart';
import '../../features/settings/presentation/pages/system_settings_page.dart';
import '../../features/backup/presentation/pages/backup_page.dart';
import '../../features/logs/presentation/pages/logs_page.dart';
import '../../features/reports/presentation/pages/reports_page_wrapper.dart';
import '../../features/epargne/presentation/pages/epargne_page.dart';
import '../../features/contributions/presentation/pages/contributions_page_wrapper.dart';
import '../../features/adhesions/presentation/pages/adhesions_page_wrapper.dart';
import '../../features/solidarity/presentation/pages/demandes_aide_page_wrapper.dart';
import '../../features/organizations/presentation/pages/organizations_page_wrapper.dart';

/// Page "Plus" avec les fonctions avancées selon le rôle (Menu Principal Extensif)
class MorePage extends StatelessWidget {
  const MorePage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        if (state is! AuthAuthenticated) {
          return const Scaffold(
            body: Center(child: CircularProgressIndicator()),
          );
        }

        return Scaffold(
          backgroundColor: ColorTokens.background,
          appBar: const UFAppBar(
            title: 'PLUS',
            automaticallyImplyLeading: false,
          ),
          body: SingleChildScrollView(
            padding: const EdgeInsets.all(16),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Profil utilisateur
                _buildUserProfile(state),
                const SizedBox(height: SpacingTokens.md),

                // Options selon le rôle
                ..._buildRoleBasedOptions(context, state),

                const SizedBox(height: SpacingTokens.md),

                // Options communes
                ..._buildCommonOptions(context),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildUserProfile(AuthAuthenticated state) {
    return CoreCard(
      child: Row(
        children: [
          MiniAvatar(
            fallbackText: state.user.firstName.isNotEmpty ? state.user.firstName[0].toUpperCase() : 'U',
            size: 40,
            imageUrl: state.user.avatar,
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${state.user.firstName} ${state.user.lastName}',
                  style: AppTypography.actionText,
                ),
                Text(
                  state.effectiveRole.displayName.toUpperCase(),
                  style: AppTypography.badgeText.copyWith(
                    color: AppColors.primaryGreen,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  List<Widget> _buildRoleBasedOptions(BuildContext context, AuthAuthenticated state) {
    final options = <Widget>[];

    // Options Super Admin uniquement
    if (state.effectiveRole == UserRole.superAdmin) {
      options.addAll([
        _buildSectionTitle('Administration Système'),
        _buildOptionTile(
          icon: Icons.people,
          title: 'Gestion des utilisateurs',
          subtitle: 'Utilisateurs Keycloak et rôles',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const UserManagementPage()),
            );
          },
        ),
        _buildOptionTile(
          icon: Icons.settings,
          title: 'Paramètres Système',
          subtitle: 'Configuration globale',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const SystemSettingsPage()),
            );
          },
        ),
        _buildOptionTile(
          icon: Icons.backup,
          title: 'Sauvegarde & Restauration',
          subtitle: 'Gestion des sauvegardes',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const BackupPage()),
            );
          },
        ),
        _buildOptionTile(
          icon: Icons.article,
          title: 'Logs & Monitoring',
          subtitle: 'Surveillance et journaux',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const LogsPage()),
            );
          },
        ),
      ]);
    }

    // Options Admin+ (Admin Organisation et Super Admin)
    if (state.effectiveRole == UserRole.orgAdmin || state.effectiveRole == UserRole.superAdmin) {
      options.addAll([
        _buildSectionTitle('Administration'),
        _buildOptionTile(
          icon: Icons.business,
          title: 'Gestion des Organisations',
          subtitle: 'Créer et gérer les organisations',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const OrganizationsPageWrapper()),
            );
          },
        ),
        _buildSectionTitle('Workflow Financier'),
        _buildOptionTile(
          icon: Icons.pending_actions,
          title: 'Approbations en attente',
          subtitle: 'Valider les transactions financières',
          onTap: () {
            Navigator.pushNamed(context, '/approvals');
          },
        ),
        _buildOptionTile(
          icon: Icons.account_balance_wallet,
          title: 'Gestion des Budgets',
          subtitle: 'Créer et suivre les budgets',
          onTap: () {
            Navigator.pushNamed(context, '/budgets');
          },
        ),
        _buildSectionTitle('Communication'),
        _buildOptionTile(
          icon: Icons.message,
          title: 'Messages & Broadcast',
          subtitle: 'Communiquer avec les membres',
          onTap: () {
            Navigator.pushNamed(context, '/messages');
          },
        ),
        _buildSectionTitle('Rapports & Analytics'),
        _buildOptionTile(
          icon: Icons.assessment,
          title: 'Rapports & Analytics',
          subtitle: 'Statistiques détaillées',
          onTap: () {
            Navigator.of(context).push(
              MaterialPageRoute(builder: (context) => const ReportsPageWrapper()),
            );
          },
        ),
      ]);
    }

    // Options Modérateur (Communication limitée)
    if (state.effectiveRole == UserRole.moderator) {
      options.addAll([
        _buildSectionTitle('Communication'),
        _buildOptionTile(
          icon: Icons.message,
          title: 'Messages aux membres',
          subtitle: 'Communiquer avec les membres',
          onTap: () {
            Navigator.pushNamed(context, '/messages');
          },
        ),
      ]);
    }

    return options;
  }

  List<Widget> _buildCommonOptions(BuildContext context) {
    return [
      _buildSectionTitle('Général'),
      _buildOptionTile(
        icon: Icons.payment,
        title: 'Cotisations',
        subtitle: 'Gérer les cotisations',
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => const CotisationsPageWrapper()),
          );
        },
      ),
      _buildOptionTile(
        icon: Icons.how_to_reg,
        title: 'Demandes d\'adhésion',
        subtitle: 'Demandes d\'adhésion à une organisation',
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => const AdhesionsPageWrapper()),
          );
        },
      ),
      _buildOptionTile(
        icon: Icons.volunteer_activism,
        title: 'Demandes d\'aide',
        subtitle: 'Solidarité – demandes d\'aide',
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => const DemandesAidePageWrapper()),
          );
        },
      ),
      _buildOptionTile(
        icon: Icons.savings_outlined,
        title: 'Comptes épargne',
        subtitle: 'Mutuelle épargne – dépôts (LCB-FT)',
        onTap: () {
          Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => const EpargnePage()),
          );
        },
      ),
    ];
  }

  Widget _buildSectionTitle(String title) {
    return Padding(
      padding: const EdgeInsets.only(top: 24, bottom: 8, left: 4),
      child: Text(
        title.toUpperCase(),
        style: AppTypography.subtitleSmall.copyWith(
          fontWeight: FontWeight.bold,
          letterSpacing: 1.1,
          color: AppColors.textSecondaryLight,
        ),
      ),
    );
  }

  Widget _buildOptionTile({
    required IconData icon,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
    Color? color,
  }) {
    final effectiveColor = color ?? AppColors.primaryGreen;
    
    return CoreCard(
      margin: const EdgeInsets.only(bottom: 8),
      onTap: onTap,
      child: Row(
        children: [
          Container(
            padding: const EdgeInsets.all(8),
            decoration: BoxDecoration(
              color: effectiveColor.withOpacity(0.1),
              borderRadius: BorderRadius.circular(8),
            ),
            child: Icon(
              icon,
              color: effectiveColor,
              size: 20,
            ),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: AppTypography.actionText.copyWith(
                    color: color ?? AppColors.textPrimaryLight,
                  ),
                ),
                Text(
                  subtitle,
                  style: AppTypography.subtitleSmall,
                ),
              ],
            ),
          ),
          const Icon(
            Icons.chevron_right,
            color: AppColors.textSecondaryLight,
            size: 16,
          ),
        ],
      ),
    );
  }
}
