/// Tests unitaires pour GetMessages use case
library get_messages_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_messages.dart';
import 'package:unionflow_mobile_apps/features/communication/domain/repositories/messaging_repository.dart';
import 'package:unionflow_mobile_apps/features/communication/domain/entities/message.dart';
import 'package:unionflow_mobile_apps/core/error/failures.dart';

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

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

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

  group('GetMessages Use Case', () {
    const tConversationId = 'conv-123';
    final tMessages = [
      Message(
        id: 'msg-1',
        conversationId: tConversationId,
        senderId: 'user-1',
        senderName: 'Amadou Diallo',
        content: 'Bonjour à tous!',
        type: MessageType.individual,
        status: MessageStatus.read,
        priority: MessagePriority.normal,
        recipientIds: ['user-2', 'user-3'],
        createdAt: DateTime(2024, 12, 15, 10, 0),
      ),
      Message(
        id: 'msg-2',
        conversationId: tConversationId,
        senderId: 'user-2',
        senderName: 'Fatou Ndiaye',
        content: 'Salut Amadou!',
        type: MessageType.individual,
        status: MessageStatus.delivered,
        priority: MessagePriority.normal,
        recipientIds: ['user-1'],
        createdAt: DateTime(2024, 12, 15, 10, 5),
      ),
    ];

    test('should return list of messages successfully', () async {
      // Arrange
      when(mockRepository.getMessages(
        conversationId: tConversationId,
        limit: anyNamed('limit'),
        beforeMessageId: anyNamed('beforeMessageId'),
      )).thenAnswer((_) async => Right(tMessages));

      // Act
      final result = await useCase(conversationId: tConversationId);

      // Assert
      expect(result, Right(tMessages));
      result.fold(
        (failure) => fail('Should not return failure'),
        (messages) {
          expect(messages.length, equals(2));
          expect(messages[0].content, equals('Bonjour à tous!'));
          expect(messages[0].status, equals(MessageStatus.read));
        },
      );
      verify(mockRepository.getMessages(
        conversationId: tConversationId,
        limit: null,
        beforeMessageId: null,
      ));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return paginated messages with limit', () async {
      // Arrange
      when(mockRepository.getMessages(
        conversationId: tConversationId,
        limit: 1,
        beforeMessageId: anyNamed('beforeMessageId'),
      )).thenAnswer((_) async => Right([tMessages[0]]));

      // Act
      final result = await useCase(conversationId: tConversationId, limit: 1);

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (messages) {
          expect(messages.length, equals(1));
        },
      );
    });

    test('should return empty list when no messages exist', () async {
      // Arrange
      when(mockRepository.getMessages(
        conversationId: anyNamed('conversationId'),
        limit: anyNamed('limit'),
        beforeMessageId: anyNamed('beforeMessageId'),
      )).thenAnswer((_) async => Right([]));

      // Act
      final result = await useCase(conversationId: 'empty-conv');

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

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

      // Act
      final result = await useCase(conversationId: tConversationId);

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