import { Module } from '@nestjs/common';
import { TypeOrmModule, getDataSourceToken } from '@nestjs/typeorm';
import type { DataSource } from 'typeorm';

import { AdminUserOrmEntity } from '@core/admin/infrastructure/typeorm/entities/AdminUserOrmEntity';
import { AdminUserOrmMapper } from '@core/admin/infrastructure/typeorm/mappers/AdminUserOrmMapper';
import { TypeOrmAdminUserRepository } from '@core/admin/infrastructure/typeorm/repositories/TypeOrmAdminUserRepository';

import { ADMIN_USER_REPOSITORY_TOKEN } from '../admin/AdminPersistenceTokens';

const typeOrmFeatureImports = [TypeOrmModule.forFeature([AdminUserOrmEntity])];

@Module({
  imports: [...typeOrmFeatureImports],
  providers: [
    { provide: AdminUserOrmMapper, useFactory: () => new AdminUserOrmMapper() },
    {
      provide: ADMIN_USER_REPOSITORY_TOKEN,
      useFactory: (dataSource: DataSource, mapper: AdminUserOrmMapper) => 
        new TypeOrmAdminUserRepository(dataSource, mapper),
      inject: [getDataSourceToken(), AdminUserOrmMapper],
    },
  ],
  exports: [ADMIN_USER_REPOSITORY_TOKEN],
})
export class TypeOrmAdminPersistenceModule {}