import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ScheduleModule } from '@nestjs/schedule';
import databaseConfig from './config/database.config';
import jwtConfig from './config/jwt.config';
import redisConfig from './config/redis.config';
import { ScheduleModule as TaskScheduleModule } from './schedule/schedule.module';

// 新的模块结构 - 按端分类
import { AppModule as AppClientModule } from './modules/app/app.module';
import { MerchantModule } from './modules/merchant/merchant.module';
import { AdminModule } from './modules/admin/admin.module';
import { SharedModule } from './modules/shared/shared.module';
import { WebsiteModule } from './modules/website/website.module';

@Module({
  imports: [
    ScheduleModule.forRoot(),
    TaskScheduleModule,
    ConfigModule.forRoot({
      isGlobal: true,
      load: [databaseConfig, jwtConfig, redisConfig],
      envFilePath: ['.env.local', '.env'],
    }),
    TypeOrmModule.forRootAsync({
      useFactory: () => ({
        type: 'mysql',
        host: process.env.DB_HOST || 'localhost',
        port: parseInt(process.env.DB_PORT || '3306', 10),
        username: process.env.DB_USERNAME || 'root',
        password: process.env.DB_PASSWORD || '',
        database: process.env.DB_DATABASE || 'rent_platform',
        entities: [__dirname + '/entities/**/*.entity{.ts,.js}'],
        synchronize: false,
        logging: process.env.NODE_ENV === 'development',
        charset: 'utf8mb4',
        timezone: '+08:00',
      }),
    }),
    // 新的模块结构：按端分组
    AppClientModule, // 应用端（小程序C端）
    MerchantModule,  // 商家端（小程序商家端+商家管理后台）
    AdminModule,     // 平台管理端
    SharedModule,    // 共享模块（上传、配置等）
    WebsiteModule,   // 官网
  ],
})
export class AppModule {}
