import { AliyunModule } from './aliyun/aliyun.module'
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { TypeOrmModule } from '@nestjs/typeorm'
import { ThrottlerModule } from '@nestjs/throttler'
import * as Yup from 'yup'
import { DevtoolsModule } from '@nestjs/devtools-integration'
import { APP_FILTER } from '@nestjs/core'
import { UsersModule } from './users/users.module'
import { ApiUserModule } from './api-users/api-user.module'
import { SmsModule } from './sms/sms.module'
import { AuthModule } from './auth/auth.module'
import { FileModule } from './file/file.module'
import { ChatModule } from './chat/chat.module'
import { MembershipModule } from './membership/membership.module'
import { WeixinModule } from './weixin/weixin.module'
import { NotifyModule } from './notify/notify.module'
import { CommissionModule } from './commission/commission.module'
import { UserBalanceModule } from './user-balance/user-balance.module'
import { MaskModule } from './mask/mask.module'
import { WithdrawModule } from './withdraw/withdraw.module'
import { SysConfigModule } from './sys-config/sys-config.module'
import { AllExceptionsFilter } from './filters/all-exceptions-filter.filter'
import { ChatRoleModule } from './chat-role/chat-role.module'
import { LabelModule } from './label/label.module'
import { MomentsModule } from './moments/moments.module'
import { CommentModule } from './comment/comment.module'
import { ChatPdfModule } from './chat-pdf/chat-pdf.module'
import { LikesModule } from './likes/likes.module'
import { ConditionModule } from './condition/condition.module'
import { OrgModule } from './org/org.module';
import { KnowledgeBaseModule } from './knowledge-base/knowledge-base.module';
import { FormModule } from './form/form.module'
import { WebModule } from './web/web.module'
import { PaperModule } from './paper/paper.module';
@Module({
    imports: [
        DevtoolsModule.register({
            http: process.env.NODE_ENV !== 'production',
            port: 8000
        }),
        ConfigModule.forRoot({
            isGlobal: true,
            validationSchema: Yup.object({
                TYPEORM_HOST: Yup.string().required(),
                TYPEORM_PORT: Yup.number().default(3306),
                TYPEORM_USERNAME: Yup.string().required(),
                TYPEORM_PASSWORD: Yup.string().required(),
                TYPEORM_DATABASE: Yup.string().required()
            })
        }),
        ThrottlerModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (config: ConfigService) => ({
                ttl: config.get<number>('THROTTLE_TTL'),
                limit: config.get<number>('THROTTLE_LIMIT')
            })
        }),
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            useFactory: (config: ConfigService) => ({
                type: 'mysql',
                host: config.get<string>('TYPEORM_HOST'),
                port: config.get<number>('TYPEORM_PORT'),
                username: config.get<string>('TYPEORM_USERNAME'),
                password: config.get<string>('TYPEORM_PASSWORD'),
                database: config.get<string>('TYPEORM_DATABASE'),
                synchronize: true,
                entities: [__dirname + '/**/*.entity.{ts,js}'],
                migrations: ['dist/migrations/**/*.js'],
                subscribers: ['dist/subscriber/**/*.js'],
                cli: {
                    migrationsDir: config.get<string>('TYPEORM_MIGRATIONS_DIR'),
                    subscribersDir: config.get<string>('TYPEORM_SUBSCRIBERS_DIR')
                }
            })
        }),
        TypeOrmModule.forRootAsync({
            imports: [ConfigModule],
            inject: [ConfigService],
            name: 'db1',
            useFactory: (config: ConfigService) => ({
                name: 'db1',
                type: 'postgres',
                host: config.get<string>('PG_HOST'),
                port: config.get<number>('PG_PORT'),
                username: config.get<string>('PG_USERNAME'),
                password: config.get<string>('PG_PASSWORD'),
                database: config.get<string>('PG_DATABASE'),
                synchronize: true
            })
        }),
        AliyunModule,
        SmsModule,
        UsersModule,
        AuthModule,
        FileModule,
        ChatModule,
        MembershipModule,
        WeixinModule,
        NotifyModule,
        CommissionModule,
        UserBalanceModule,
        MaskModule,
        WithdrawModule,
        SysConfigModule,
        ChatRoleModule,
        LabelModule,
        MomentsModule,
        CommentModule,
        ChatPdfModule,
        LikesModule,
        ApiUserModule,
        ConditionModule,
        OrgModule,
        KnowledgeBaseModule,
        FormModule,
        WebModule,
        PaperModule
    ],
    providers: [
        {
            provide: APP_FILTER,
            useClass: AllExceptionsFilter
        },
    ]
})
export class AppModule { }
