/*
 * Copyright 2025 Daytona Platforms Inc.
 * SPDX-License-Identifier: AGPL-3.0
 */

import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { OrganizationController } from './controllers/organization.controller'
import { OrganizationRoleController } from './controllers/organization-role.controller'
import { OrganizationUserController } from './controllers/organization-user.controller'
import { OrganizationInvitationController } from './controllers/organization-invitation.controller'
import { Organization } from './entities/organization.entity'
import { OrganizationRole } from './entities/organization-role.entity'
import { OrganizationUser } from './entities/organization-user.entity'
import { OrganizationInvitation } from './entities/organization-invitation.entity'
import { OrganizationService } from './services/organization.service'
import { OrganizationRoleService } from './services/organization-role.service'
import { OrganizationUserService } from './services/organization-user.service'
import { OrganizationInvitationService } from './services/organization-invitation.service'
import { UserModule } from '../user/user.module'
import { Sandbox } from '../sandbox/entities/sandbox.entity'
import { SnapshotRepository } from '../sandbox/repositories/snapshot.repository'
import { Volume } from '../sandbox/entities/volume.entity'
import { RedisLockProvider } from '../sandbox/common/redis-lock.provider'
import { SnapshotRunner } from '../sandbox/entities/snapshot-runner.entity'
import { OrganizationUsageService } from './services/organization-usage.service'
import { OrgMetricsExporterService } from './services/org-metrics-exporter.service'
import { DataSource } from 'typeorm'
import { EventEmitter2 } from '@nestjs/event-emitter'
import { SandboxRepository } from '../sandbox/repositories/sandbox.repository'
import { SandboxLookupCacheInvalidationService } from '../sandbox/services/sandbox-lookup-cache-invalidation.service'
import { OrganizationAuthContextGuard } from './guards/organization-auth-context.guard'
import { RegionQuota } from './entities/region-quota.entity'
import { RegionModule } from '../region/region.module'
import { OrganizationRegionController } from './controllers/organization-region.controller'
import { Region } from '../region/entities/region.entity'
import { EncryptionModule } from '../encryption/encryption.module'

@Module({
  imports: [
    UserModule,
    RegionModule,
    TypeOrmModule.forFeature([
      Organization,
      OrganizationRole,
      OrganizationUser,
      OrganizationInvitation,
      Sandbox,
      Volume,
      SnapshotRunner,
      RegionQuota,
      Region,
    ]),
    EncryptionModule,
  ],
  controllers: [
    OrganizationController,
    OrganizationRoleController,
    OrganizationUserController,
    OrganizationInvitationController,
    OrganizationRegionController,
  ],
  providers: [
    OrganizationService,
    OrganizationRoleService,
    OrganizationUserService,
    OrganizationInvitationService,
    OrganizationUsageService,
    OrgMetricsExporterService,
    RedisLockProvider,
    SandboxLookupCacheInvalidationService,
    {
      provide: SandboxRepository,
      inject: [DataSource, EventEmitter2, SandboxLookupCacheInvalidationService],
      useFactory: (
        dataSource: DataSource,
        eventEmitter: EventEmitter2,
        sandboxLookupCacheInvalidationService: SandboxLookupCacheInvalidationService,
      ) => new SandboxRepository(dataSource, eventEmitter, sandboxLookupCacheInvalidationService),
    },
    {
      provide: SnapshotRepository,
      inject: [DataSource, EventEmitter2],
      useFactory: (dataSource: DataSource, eventEmitter: EventEmitter2) =>
        new SnapshotRepository(dataSource, eventEmitter),
    },
    OrganizationAuthContextGuard,
  ],
  exports: [
    OrganizationService,
    OrganizationRoleService,
    OrganizationUserService,
    OrganizationInvitationService,
    OrganizationUsageService,
    OrganizationAuthContextGuard,
  ],
})
export class OrganizationModule {}
