import { Module } from '@nestjs/common'
import { MongooseModule } from '@nestjs/mongoose'
import { AuditModule } from '../audit/audit.module.js'
import { AuthModule } from '../auth/auth.module.js'
import { IntegrationsModule } from '../integrations/integrations.module.js'
import { Domain, DomainSchema } from '../schemas/domain.schema.js'
import { Partner, PartnerSchema } from '../schemas/partner.schema.js'
import { Price, PriceSchema } from '../schemas/price.schema.js'
import { Subscription, SubscriptionSchema } from '../schemas/subscription.schema.js'
import { Tenant, TenantSchema } from '../schemas/tenant.schema.js'
import { User, UserSchema } from '../schemas/user.schema.js'
import { TenantsModule } from '../tenants/tenants.module.js'
import { PlatformReportsController } from './platform-reports.controller.js'
import { TenantMembersController } from './tenant-members.controller.js'
import { UsersController } from './users.controller.js'
import { UsersService } from './users.service.js'

@Module({
  imports: [
    MongooseModule.forFeature([
      { name: User.name, schema: UserSchema },
      { name: Tenant.name, schema: TenantSchema },
      // Partner model registered directly (instead of importing PartnersModule)
      // to avoid the circular import — PartnersModule already imports
      // UsersModule for the invitePartnerUser delegation.
      { name: Partner.name, schema: PartnerSchema },
      // Subscription + Price are read by partnerMrr() — MRR aggregation
      // runs entirely in this service rather than chaining into
      // SubscriptionsModule, since we want one direct query path that's
      // easy to extend (prorating, multi-currency) later.
      { name: Subscription.name, schema: SubscriptionSchema },
      { name: Price.name, schema: PriceSchema },
      // Domain — read by createTenantMember to resolve the tenant's primary
      // mail domain + its Stalwart id for mailbox provisioning.
      { name: Domain.name, schema: DomainSchema },
    ]),
    AuthModule,
    AuditModule,
    IntegrationsModule,
    TenantsModule,
  ],
  controllers: [UsersController, PlatformReportsController, TenantMembersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}
