/// Page liste des conversations
library conversations_page;

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../core/di/injection_container.dart';
import '../../../../shared/design_system/unionflow_design_system.dart';
import '../bloc/messaging_bloc.dart';
import '../bloc/messaging_event.dart';
import '../bloc/messaging_state.dart';
import '../widgets/conversation_tile.dart';

class ConversationsPage extends StatelessWidget {
  final String? organizationId;

  const ConversationsPage({
    super.key,
    this.organizationId,
  });

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => sl<MessagingBloc>()
        ..add(LoadConversations(organizationId: organizationId)),
      child: Scaffold(
        backgroundColor: ColorTokens.background,
        appBar: const UFAppBar(
          title: 'MESSAGES',
          automaticallyImplyLeading: true,
        ),
        body: BlocBuilder<MessagingBloc, MessagingState>(
          builder: (context, state) {
            if (state is MessagingLoading) {
              return const Center(child: CircularProgressIndicator());
            }

            if (state is MessagingError) {
              return Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    const Icon(
                      Icons.error_outline,
                      size: 64,
                      color: AppColors.error,
                    ),
                    const SizedBox(height: SpacingTokens.md),
                    Text(
                      'Erreur',
                      style: AppTypography.headerSmall,
                    ),
                    const SizedBox(height: SpacingTokens.sm),
                    Text(
                      state.message,
                      style: AppTypography.bodyTextSmall,
                      textAlign: TextAlign.center,
                    ),
                    const SizedBox(height: SpacingTokens.lg),
                    UFPrimaryButton(
                      label: 'Réessayer',
                      onPressed: () {
                        context.read<MessagingBloc>().add(
                          LoadConversations(organizationId: organizationId),
                        );
                      },
                    ),
                  ],
                ),
              );
            }

            if (state is ConversationsLoaded) {
              final conversations = state.conversations;

              if (conversations.isEmpty) {
                return Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(
                        Icons.chat_bubble_outline,
                        size: 64,
                        color: AppColors.textSecondaryLight,
                      ),
                      const SizedBox(height: SpacingTokens.md),
                      Text(
                        'Aucune conversation',
                        style: AppTypography.headerSmall.copyWith(
                          color: AppColors.textSecondaryLight,
                        ),
                      ),
                      const SizedBox(height: SpacingTokens.sm),
                      Text(
                        'Commencez une nouvelle conversation',
                        style: AppTypography.bodyTextSmall.copyWith(
                          color: AppColors.textSecondaryLight,
                        ),
                      ),
                    ],
                  ),
                );
              }

              return RefreshIndicator(
                onRefresh: () async {
                  context.read<MessagingBloc>().add(
                    LoadConversations(organizationId: organizationId),
                  );
                },
                child: ListView.separated(
                  padding: const EdgeInsets.all(SpacingTokens.md),
                  itemCount: conversations.length,
                  separatorBuilder: (_, __) => const SizedBox(height: SpacingTokens.sm),
                  itemBuilder: (context, index) {
                    final conversation = conversations[index];
                    return ConversationTile(
                      conversation: conversation,
                      onTap: () {
                        // Navigation vers la page de chat
                        // TODO: Implémenter navigation
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                            content: Text('Ouvrir conversation: ${conversation.name}'),
                          ),
                        );
                      },
                    );
                  },
                ),
              );
            }

            return const SizedBox.shrink();
          },
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: AppColors.primaryGreen,
          onPressed: () {
            // TODO: Ouvrir dialogue nouvelle conversation
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text('Nouvelle conversation (à implémenter)')),
            );
          },
          child: const Icon(Icons.add, color: Colors.white),
        ),
      ),
    );
  }
}
