import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { DatabaseModule, TenantProvisioningService } from '@it0/database';
import { AuthController } from './interfaces/rest/controllers/auth.controller';
import { TenantController } from './interfaces/rest/controllers/tenant.controller';
import { UserController } from './interfaces/rest/controllers/user.controller';
import { SettingsController } from './interfaces/rest/controllers/settings.controller';
import { RoleController } from './interfaces/rest/controllers/role.controller';
import { PermissionController } from './interfaces/rest/controllers/permission.controller';
import { SmsController } from './interfaces/rest/controllers/sms.controller';
import { TenantTagController } from './interfaces/rest/controllers/tenant-tag.controller';
import { JwtStrategy } from './infrastructure/strategies/jwt.strategy';
import { RbacGuard } from './infrastructure/guards/rbac.guard';
import { AuthService } from './application/services/auth.service';
import { UserRepository } from './infrastructure/repositories/user.repository';
import { ApiKeyRepository } from './infrastructure/repositories/api-key.repository';
import { SmsService } from './infrastructure/sms/sms.service';
import { RedisProvider } from './infrastructure/redis/redis.provider';
import { User } from './domain/entities/user.entity';
import { Role } from './domain/entities/role.entity';
import { ApiKey } from './domain/entities/api-key.entity';
import { Tenant } from './domain/entities/tenant.entity';
import { TenantInvite } from './domain/entities/tenant-invite.entity';
import { TenantTag } from './domain/entities/tenant-tag.entity';
import { TenantTagAssignment } from './domain/entities/tenant-tag-assignment.entity';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),
    DatabaseModule.forRoot(),
    TypeOrmModule.forFeature([User, Role, ApiKey, Tenant, TenantInvite, TenantTag, TenantTagAssignment]),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      useFactory: () => ({
        secret: process.env.JWT_SECRET || 'dev-secret',
        signOptions: { expiresIn: process.env.JWT_EXPIRES_IN || '24h' },
      }),
    }),
  ],
  controllers: [AuthController, TenantController, UserController, SettingsController, RoleController, PermissionController, SmsController, TenantTagController],
  providers: [
    JwtStrategy,
    RbacGuard,
    AuthService,
    UserRepository,
    ApiKeyRepository,
    TenantProvisioningService,
    SmsService,
    RedisProvider,
  ],
  exports: [AuthService],
})
export class AuthModule {}
