/// Tests unitaires pour UpdateOrganizationConfig use case
library update_organization_config_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_config.dart';
import 'package:unionflow_mobile_apps/features/organizations/data/models/organization_model.dart';

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

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

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

  group('UpdateOrganizationConfig Use Case', () {
    const tOrganizationId = 'org1';
    final tConfig = {
      'theme': 'dark',
      'language': 'fr',
      'notifications': true,
      'cotisationMensuelle': 5000.0,
    };

    final tUpdatedOrganization = OrganizationModel(
      id: tOrganizationId,
      nom: 'Organisation Alpha',
      nomCourt: 'OA',
      email: 'contact@alpha.org',
      typeOrganisation: TypeOrganization.association,
      statut: StatutOrganization.active,
    );

    test('should update organization configuration successfully', () async {
      // Arrange
      when(mockRepository.updateOrganizationConfig(tOrganizationId, tConfig))
          .thenAnswer((_) async => tUpdatedOrganization);

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

      // Assert
      expect(result, equals(tUpdatedOrganization));
      verify(mockRepository.updateOrganizationConfig(tOrganizationId, tConfig));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should update partial configuration', () async {
      // Arrange
      final partialConfig = {'theme': 'light'};
      when(mockRepository.updateOrganizationConfig(tOrganizationId, partialConfig))
          .thenAnswer((_) async => tUpdatedOrganization);

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

      // Assert
      expect(result, isNotNull);
      verify(mockRepository.updateOrganizationConfig(tOrganizationId, partialConfig));
    });

    test('should handle empty configuration map', () async {
      // Arrange
      final emptyConfig = <String, dynamic>{};
      when(mockRepository.updateOrganizationConfig(tOrganizationId, emptyConfig))
          .thenAnswer((_) async => tUpdatedOrganization);

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

      // Assert
      expect(result, isNotNull);
    });

    test('should throw exception when update fails', () async {
      // Arrange
      when(mockRepository.updateOrganizationConfig(any, any))
          .thenThrow(Exception('Configuration update failed'));

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