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

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

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

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

  group('GetOrganizationById Use Case', () {
    const tOrganizationId = 'org1';
    final tOrganization = OrganizationModel(
      id: tOrganizationId,
      nom: 'Organisation Alpha',
      nomCourt: 'OA',
      email: 'contact@alpha.org',
      telephone: '+33123456789',
      adresse: '123 Rue de Paris',
      typeOrganisation: TypeOrganization.association,
      statut: StatutOrganization.active,
    );

    test('should return organization by id', () async {
      // Arrange
      when(mockRepository.getOrganizationById(tOrganizationId))
          .thenAnswer((_) async => tOrganization);

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

      // Assert
      expect(result, equals(tOrganization));
      expect(result?.id, equals(tOrganizationId));
      verify(mockRepository.getOrganizationById(tOrganizationId));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should return null when organization not found', () async {
      // Arrange
      when(mockRepository.getOrganizationById(any))
          .thenAnswer((_) async => null);

      // Act
      final result = await useCase('nonexistent');

      // Assert
      expect(result, isNull);
      verify(mockRepository.getOrganizationById('nonexistent'));
    });

    test('should return organization with all fields populated', () async {
      // Arrange
      when(mockRepository.getOrganizationById(tOrganizationId))
          .thenAnswer((_) async => tOrganization);

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

      // Assert
      expect(result?.nom, isNotNull);
      expect(result?.email, isNotNull);
      expect(result?.statut, equals(StatutOrganization.active));
    });

    test('should throw exception when repository fails', () async {
      // Arrange
      when(mockRepository.getOrganizationById(any))
          .thenThrow(Exception('Database error'));

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