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

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

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

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

  group('CreateOrganization Use Case', () {
    final tOrganization = OrganizationModel(
      nom: 'Nouvelle Organisation',
      nomCourt: 'NO',
      email: 'contact@nouvelle.org',
      typeOrganisation: TypeOrganization.association,
      statut: StatutOrganization.enCreation,
    );

    final tCreatedOrganization = OrganizationModel(
      id: 'org123',
      nom: 'Nouvelle Organisation',
      nomCourt: 'NO',
      email: 'contact@nouvelle.org',
      typeOrganisation: TypeOrganization.association,
      statut: StatutOrganization.active,
    );

    test('should create organization successfully', () async {
      // Arrange
      when(mockRepository.createOrganization(tOrganization))
          .thenAnswer((_) async => tCreatedOrganization);

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

      // Assert
      expect(result, equals(tCreatedOrganization));
      expect(result.id, isNotNull);
      expect(result.nom, equals('Nouvelle Organisation'));
      verify(mockRepository.createOrganization(tOrganization));
      verifyNoMoreInteractions(mockRepository);
    });

    test('should create organization with minimal required fields', () async {
      // Arrange
      final minimalOrg = OrganizationModel(
        nom: 'Minimal Org',
        nomCourt: 'MO',
        email: 'minimal@org.com',
        typeOrganisation: TypeOrganization.cooperative,
        statut: StatutOrganization.enCreation,
      );
      final createdMinimal = OrganizationModel(
        id: 'org456',
        nom: 'Minimal Org',
        nomCourt: 'MO',
        email: 'minimal@org.com',
        typeOrganisation: TypeOrganization.cooperative,
        statut: StatutOrganization.active,
      );
      when(mockRepository.createOrganization(minimalOrg))
          .thenAnswer((_) async => createdMinimal);

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

      // Assert
      expect(result.id, isNotNull);
      expect(result.nom, equals('Minimal Org'));
    });

    test('should throw exception when email already exists', () async {
      // Arrange
      when(mockRepository.createOrganization(any))
          .thenThrow(Exception('Email déjà utilisé'));

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

    test('should throw exception when validation fails', () async {
      // Arrange
      when(mockRepository.createOrganization(any))
          .thenThrow(Exception('Données invalides'));

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