/// Tests unitaires pour GetConversations use case
library get_conversations_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:dartz/dartz.dart';
import 'package:unionflow_mobile_apps/features/communication/domain/usecases/get_conversations.dart';
import 'package:unionflow_mobile_apps/features/communication/domain/repositories/messaging_repository.dart';
import 'package:unionflow_mobile_apps/features/communication/domain/entities/conversation.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';

@GenerateMocks([MessagingRepository])
import 'get_conversations_test.mocks.dart';

void main() {
  late GetConversations useCase;
  late MockMessagingRepository mockRepository;

  setUp(() {
    mockRepository = MockMessagingRepository();
    useCase = GetConversations(mockRepository);
  });

  group('GetConversations Use Case', () {
    final tConversations = [
      Conversation(
        id: 'conv-1',
        name: 'Discussion Projet Alpha',
        type: ConversationType.group,
        participantIds: ['user-1', 'user-2', 'user-3'],
        organizationId: 'org-123',
        unreadCount: 5,
        isPinned: true,
        createdAt: DateTime(2024, 12, 1),
      ),
      Conversation(
        id: 'conv-2',
        name: 'Fatou Ndiaye',
        type: ConversationType.individual,
        participantIds: ['user-1', 'user-4'],
        unreadCount: 0,
        createdAt: DateTime(2024, 12, 10),
      ),
    ];

    test('should return list of conversations successfully', () async {
      // Arrange
      when(mockRepository.getConversations(
        organizationId: anyNamed('organizationId'),
        includeArchived: anyNamed('includeArchived'),
      )).thenAnswer((_) async => Right(tConversations));

      // Act
      final result = await useCase(organizationId: 'org-123');

      // Assert
      expect(result, Right(tConversations));
      result.fold(
        (failure) => fail('Should not return failure'),
        (conversations) {
          expect(conversations.length, equals(2));
          expect(conversations[0].name, equals('Discussion Projet Alpha'));
          expect(conversations[0].unreadCount, equals(5));
          expect(conversations[0].isPinned, isTrue);
        },
      );
      verify(mockRepository.getConversations(
        organizationId: 'org-123',
        includeArchived: false,
      ));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return conversations with archived included', () async {
      // Arrange
      final archivedConv = Conversation(
        id: 'conv-3',
        name: 'Ancienne Discussion',
        type: ConversationType.group,
        participantIds: ['user-1', 'user-2'],
        isArchived: true,
        createdAt: DateTime(2024, 11, 1),
      );
      when(mockRepository.getConversations(
        organizationId: anyNamed('organizationId'),
        includeArchived: true,
      )).thenAnswer((_) async => Right([...tConversations, archivedConv]));

      // Act
      final result = await useCase(includeArchived: true);

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (conversations) {
          expect(conversations.length, equals(3));
          expect(conversations.any((c) => c.isArchived), isTrue);
        },
      );
    });

    test('should return empty list when no conversations exist', () async {
      // Arrange
      when(mockRepository.getConversations(
        organizationId: anyNamed('organizationId'),
        includeArchived: anyNamed('includeArchived'),
      )).thenAnswer((_) async => Right([]));

      // Act
      final result = await useCase();

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (conversations) => expect(conversations, isEmpty),
      );
    });

    test('should return ServerFailure when repository fails', () async {
      // Arrange
      final tFailure = ServerFailure('Erreur serveur');
      when(mockRepository.getConversations(
        organizationId: anyNamed('organizationId'),
        includeArchived: anyNamed('includeArchived'),
      )).thenAnswer((_) async => Left(tFailure));

      // Act
      final result = await useCase();

      // Assert
      expect(result, Left(tFailure));
      result.fold(
        (failure) => expect(failure, isA<ServerFailure>()),
        (conversations) => fail('Should not return conversations'),
      );
    });
  });
}
