import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import '../../../../shared/design_system/unionflow_design_system.dart';
import '../../../../shared/widgets/core_card.dart';
import '../../../../shared/widgets/info_badge.dart';
import '../../../../shared/widgets/mini_avatar.dart';
import '../../bloc/adhesions_bloc.dart';
import '../../data/models/adhesion_model.dart';
import 'adhesion_detail_page.dart';
import '../widgets/create_adhesion_dialog.dart';
import '../../../authentication/presentation/bloc/auth_bloc.dart';

class AdhesionsPage extends StatefulWidget {
  const AdhesionsPage({super.key});

  @override
  State<AdhesionsPage> createState() => _AdhesionsPageState();
}

class _AdhesionsPageState extends State<AdhesionsPage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;
  final _currencyFormat = NumberFormat.currency(locale: 'fr_FR', symbol: 'FCFA');

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 4, vsync: this);
    _loadTab(0);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  void _loadTab(int index) {
    bool isGestionnaire = false;
    String? membreId;
    final authState = context.read<AuthBloc>().state;
    if (authState is AuthAuthenticated) {
      isGestionnaire = authState.effectiveRole.level >= 50;
      membreId = authState.user.id;
    }

    if (isGestionnaire) {
      switch (index) {
        case 0:
          context.read<AdhesionsBloc>().add(const LoadAdhesions());
          break;
        case 1:
          context.read<AdhesionsBloc>().add(const LoadAdhesionsEnAttente());
          break;
        case 2:
          context.read<AdhesionsBloc>().add(const LoadAdhesionsByStatut('APPROUVEE'));
          break;
        case 3:
          context.read<AdhesionsBloc>().add(const LoadAdhesionsByStatut('PAYEE'));
          break;
      }
    } else {
      // Normal member: always fetch their own records to ensure security
      if (membreId != null) {
        context.read<AdhesionsBloc>().add(LoadAdhesionsByMembre(membreId));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<AdhesionsBloc, AdhesionsState>(
      listener: (context, state) {
        if (state.status == AdhesionsStatus.error && state.message != null) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text(state.message!),
              backgroundColor: AppColors.error,
              action: SnackBarAction(
                label: 'Réessayer',
                textColor: Colors.white,
                onPressed: () {
                  if (mounted) _loadTab(_tabController.index);
                },
              ),
            ),
          );
        }
      },
      child: Scaffold(
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        appBar: UFAppBar(
          title: 'Adhésions',
          moduleGradient: ModuleColors.adhesionsGradient,
          actions: [
            IconButton(
              icon: const Icon(Icons.add, size: 20),
              onPressed: () => _showCreateDialog(),
              tooltip: 'Nouvelle demande',
            ),
          ],
          bottom: TabBar(
            controller: _tabController,
            onTap: _loadTab,
            isScrollable: true,
            labelColor: Colors.white,
            unselectedLabelColor: Colors.white70,
            indicatorColor: Colors.white,
            indicatorSize: TabBarIndicatorSize.label,
            labelStyle: AppTypography.actionText.copyWith(fontSize: 10, fontWeight: FontWeight.bold),
            tabs: const [
              Tab(child: Text('TOUTES')),
              Tab(child: Text('ATTENTE')),
              Tab(child: Text('APPROUVÉES')),
              Tab(child: Text('PAYÉES')),
            ],
          ),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [
            _buildList(null),
            _buildList('EN_ATTENTE'),
            _buildList('APPROUVEE'), // tab 2 charge déjà par statut
            _buildList('PAYEE'),     // tab 3 charge déjà par statut
          ],
        ),
      ),
    );
  }

  Widget _buildList(String? statutFilter) {
    return BlocBuilder<AdhesionsBloc, AdhesionsState>(
      buildWhen: (prev, curr) =>
          prev.status != curr.status || prev.adhesions != curr.adhesions,
      builder: (context, state) {
        if (state.status == AdhesionsStatus.loading && state.adhesions.isEmpty) {
          return const Center(child: CircularProgressIndicator());
        }
        var list = state.adhesions;
        if (statutFilter != null) {
          list = list.where((a) => a.statut == statutFilter).toList();
        }
        if (list.isEmpty) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(
                  Icons.assignment_outlined,
                  size: 40,
                  color: Theme.of(context).brightness == Brightness.dark
                      ? AppColors.textSecondaryDark
                      : AppColors.textSecondary,
                ),
                const SizedBox(height: 8),
                Text(
                  'Aucune demande d\'adhésion',
                  style: AppTypography.bodyTextSmall.copyWith(fontSize: 16, color: Theme.of(context).colorScheme.onSurfaceVariant),
                ),
                const SizedBox(height: 8),
                TextButton.icon(
                  onPressed: () => _showCreateDialog(),
                  icon: const Icon(Icons.add),
                  label: const Text('Créer une demande'),
                ),
              ],
            ),
          );
        }
        return RefreshIndicator(
          onRefresh: () async => _loadTab(_tabController.index),
          child: ListView.builder(
            padding: const EdgeInsets.all(8),
            itemCount: list.length,
            itemBuilder: (context, index) {
              final a = list[index];
              return _AdhesionCard(
                adhesion: a,
                currencyFormat: _currencyFormat,
                onTap: () => _openDetail(a),
              );
            },
          ),
        );
      },
    );
  }

  void _openDetail(AdhesionModel a) {
    if (a.id == null) return;
    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => BlocProvider.value(
          value: context.read<AdhesionsBloc>(),
          child: AdhesionDetailPage(adhesionId: a.id!),
        ),
      ),
    ).then((_) => _loadTab(_tabController.index));
  }

  void _showCreateDialog() {
    showDialog<void>(
      context: context,
      builder: (context) => CreateAdhesionDialog(
        onCreated: () {
          Navigator.of(context).pop();
          _loadTab(_tabController.index);
        },
      ),
    );
  }
}

class _AdhesionCard extends StatelessWidget {
  final AdhesionModel adhesion;
  final NumberFormat currencyFormat;
  final VoidCallback onTap;

  const _AdhesionCard({
    required this.adhesion,
    required this.currencyFormat,
    required this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    return CoreCard(
      margin: const EdgeInsets.only(bottom: 10),
      onTap: onTap,
      padding: const EdgeInsets.all(12),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              const MiniAvatar(size: 24, fallbackText: '🏢'),
              const SizedBox(width: 10),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      adhesion.nomOrganisation ?? adhesion.organisationId ?? 'Organisation',
                      style: AppTypography.actionText.copyWith(fontSize: 12),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    Text(
                      adhesion.numeroReference ?? adhesion.id?.substring(0, 8) ?? '—',
                      style: AppTypography.subtitleSmall.copyWith(fontSize: 9),
                    ),
                  ],
                ),
              ),
              _buildStatutBadge(context, adhesion.statut),
            ],
          ),
          const SizedBox(height: 12),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text('FRAIS D\'ADHÉSION', style: AppTypography.subtitleSmall.copyWith(fontSize: 8, fontWeight: FontWeight.bold)),
                  Text(
                    adhesion.fraisAdhesion != null ? currencyFormat.format(adhesion.fraisAdhesion) : '—',
                    style: AppTypography.headerSmall.copyWith(fontSize: 13, color: AppColors.primary),
                  ),
                ],
              ),
              if (adhesion.dateDemande != null)
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text('DATE', style: AppTypography.subtitleSmall.copyWith(fontSize: 8, fontWeight: FontWeight.bold)),
                    Text(
                      DateFormat('dd/MM/yyyy').format(adhesion.dateDemande!),
                      style: AppTypography.bodyTextSmall.copyWith(fontSize: 10),
                    ),
                  ],
                ),
            ],
          ),
          if (adhesion.nomMembreComplet.isNotEmpty) ...[
            const SizedBox(height: 8),
            Text(
              'MEMBRE : ${adhesion.nomMembreComplet.toUpperCase()}',
              style: AppTypography.subtitleSmall.copyWith(fontSize: 8, color: Theme.of(context).colorScheme.onSurfaceVariant),
            ),
          ],
        ],
      ),
    );
  }

  Widget _buildStatutBadge(BuildContext context, String? statut) {
    Color color;
    switch (statut) {
      case 'APPROUVEE':
      case 'PAYEE':
        color = AppColors.success;
        break;
      case 'REJETEE':
      case 'ANNULEE':
        color = AppColors.error;
        break;
      case 'EN_ATTENTE':
        color = AppColors.primaryLight;
        break;
      case 'EN_PAIEMENT':
        color = AppColors.warning;
        break;
      default:
        color = Theme.of(context).brightness == Brightness.dark
            ? AppColors.textSecondaryDark
            : AppColors.textSecondary;
    }
    return InfoBadge(text: statut ?? 'INCONNU', backgroundColor: color);
  }
}
