/// Tests unitaires pour UpdateOrganization use case
library update_organization_test;

import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:unionflow_mobile_apps/features/organizations/domain/repositories/organization_repository.dart';
import 'package:unionflow_mobile_apps/features/organizations/domain/usecases/update_organization.dart';
import 'package:unionflow_mobile_apps/features/organizations/data/models/organization_model.dart';

@GenerateMocks([IOrganizationRepository])
import 'update_organization_test.mocks.dart';

void main() {
  late UpdateOrganization useCase;
  late MockIOrganizationRepository mockRepository;

  setUp(() {
    mockRepository = MockIOrganizationRepository();
    useCase = UpdateOrganization(mockRepository);
  });

  group('UpdateOrganization Use Case', () {
    const tOrganizationId = 'org1';
    final tOrganization = OrganizationModel(
      id: tOrganizationId,
      nom: 'Organisation Mise à Jour',
      nomCourt: 'OMA',
      email: 'updated@org.com',
      telephone: '+33987654321',
      typeOrganisation: TypeOrganization.association,
      statut: StatutOrganization.active,
    );

    test('should update organization successfully', () async {
      // Arrange
      when(mockRepository.updateOrganization(tOrganizationId, tOrganization))
          .thenAnswer((_) async => tOrganization);

      // Act
      final result = await useCase(tOrganizationId, tOrganization);

      // Assert
      expect(result, equals(tOrganization));
      expect(result.nom, equals('Organisation Mise à Jour'));
      verify(mockRepository.updateOrganization(tOrganizationId, tOrganization));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update partial organization fields', () async {
      // Arrange
      final partialUpdate = OrganizationModel(
        id: tOrganizationId,
        nom: 'Nom Modifié',
        nomCourt: 'OA',
        email: 'contact@alpha.org',
        typeOrganisation: TypeOrganization.association,
        statut: StatutOrganization.active,
      );
      when(mockRepository.updateOrganization(tOrganizationId, partialUpdate))
          .thenAnswer((_) async => partialUpdate);

      // Act
      final result = await useCase(tOrganizationId, partialUpdate);

      // Assert
      expect(result.nom, equals('Nom Modifié'));
    });

    test('should throw exception when organization not found', () async {
      // Arrange
      when(mockRepository.updateOrganization(any, any))
          .thenThrow(Exception('Organisation non trouvée'));

      // Act & Assert
      expect(
        () => useCase(tOrganizationId, tOrganization),
        throwsA(isA<Exception>()),
      );
    });

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updateOrganization(any, any))
          .thenThrow(Exception('Mise à jour échouée'));

      // Act & Assert
      expect(() => useCase(tOrganizationId, tOrganization), throwsException);
    });
  });
}
