/// Tests unitaires pour SendMessage use case
library send_message_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/send_message.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 'send_message_test.mocks.dart';

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

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

  group('SendMessage Use Case', () {
    const tConversationId = 'conv-123';
    const tContent = 'Bonjour, comment allez-vous?';
    final tSentMessage = Message(
      id: 'msg-new',
      conversationId: tConversationId,
      senderId: 'user-1',
      senderName: 'Amadou Diallo',
      content: tContent,
      type: MessageType.individual,
      status: MessageStatus.sent,
      priority: MessagePriority.normal,
      recipientIds: ['user-2'],
      createdAt: DateTime.now(),
    );

    test('should send message successfully', () async {
      // Arrange
      when(mockRepository.sendMessage(
        conversationId: tConversationId,
        content: tContent,
        attachments: anyNamed('attachments'),
        priority: anyNamed('priority'),
      )).thenAnswer((_) async => Right(tSentMessage));

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

      // Assert
      expect(result, Right(tSentMessage));
      result.fold(
        (failure) => fail('Should not return failure'),
        (message) {
          expect(message.id, equals('msg-new'));
          expect(message.content, equals(tContent));
          expect(message.status, equals(MessageStatus.sent));
        },
      );
      verify(mockRepository.sendMessage(
        conversationId: tConversationId,
        content: tContent,
        attachments: null,
        priority: MessagePriority.normal,
      ));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should send urgent message with attachments', () async {
      // Arrange
      final attachments = ['file1.pdf', 'file2.jpg'];
      final urgentMessage = Message(
        id: 'msg-urgent',
        conversationId: tConversationId,
        senderId: 'user-1',
        senderName: 'Amadou Diallo',
        content: 'URGENT: Document important',
        type: MessageType.individual,
        status: MessageStatus.sent,
        priority: MessagePriority.urgent,
        recipientIds: ['user-2'],
        attachments: attachments,
        createdAt: DateTime.now(),
      );
      when(mockRepository.sendMessage(
        conversationId: tConversationId,
        content: 'URGENT: Document important',
        attachments: attachments,
        priority: MessagePriority.urgent,
      )).thenAnswer((_) async => Right(urgentMessage));

      // Act
      final result = await useCase(
        conversationId: tConversationId,
        content: 'URGENT: Document important',
        attachments: attachments,
        priority: MessagePriority.urgent,
      );

      // Assert
      result.fold(
        (failure) => fail('Should not return failure'),
        (message) {
          expect(message.priority, equals(MessagePriority.urgent));
          expect(message.attachments, equals(attachments));
        },
      );
    });

    test('should return ValidationFailure when content is empty', () async {
      // Act
      final result = await useCase(
        conversationId: tConversationId,
        content: '   ',
      );

      // Assert
      result.fold(
        (failure) {
          expect(failure, isA<ValidationFailure>());
          expect((failure as ValidationFailure).message, contains('ne peut pas être vide'));
        },
        (message) => fail('Should not return message'),
      );
      verifyZeroInteractions(mockRepository);
    });

    test('should return ServerFailure when repository fails', () async {
      // Arrange
      final tFailure = ServerFailure('Erreur d\'envoi');
      when(mockRepository.sendMessage(
        conversationId: anyNamed('conversationId'),
        content: anyNamed('content'),
        attachments: anyNamed('attachments'),
        priority: anyNamed('priority'),
      )).thenAnswer((_) async => Left(tFailure));

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

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