import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { ScheduleModule } from '@nestjs/schedule'
import { PassportModule } from '@nestjs/passport'
import { JwtModule } from '@nestjs/jwt'

// Config
import { appConfig, databaseConfig, redisConfig, kafkaConfig, jwtConfig } from '@/config'

// Infrastructure
import { PrismaService } from '@/infrastructure/persistence/prisma/prisma.service'
import {
  AuthorizationRoleRepositoryImpl,
  AUTHORIZATION_ROLE_REPOSITORY,
} from '@/infrastructure/persistence/repositories/authorization-role.repository.impl'
import {
  MonthlyAssessmentRepositoryImpl,
  MONTHLY_ASSESSMENT_REPOSITORY,
} from '@/infrastructure/persistence/repositories/monthly-assessment.repository.impl'
// [2026-01-04] 新增：系统账户仓储，用于系统账户报表统计
// 回滚方式：删除此 import 及下方 providers 中的 SYSTEM_ACCOUNT_REPOSITORY 和 SystemAccountApplicationService
import {
  SystemAccountRepositoryImpl,
  SYSTEM_ACCOUNT_REPOSITORY,
} from '@/infrastructure/persistence/repositories/system-account.repository.impl'
// [2026-01-08] 新增：权益考核记录仓储，用于保存权益有效性考核历史
import {
  BenefitAssessmentRecordRepositoryImpl,
  BENEFIT_ASSESSMENT_RECORD_REPOSITORY,
} from '@/infrastructure/persistence/repositories/benefit-assessment-record.repository.impl'
import { RedisModule } from '@/infrastructure/redis/redis.module'
import { KafkaModule } from '@/infrastructure/kafka/kafka.module'
import { EventConsumerController } from '@/infrastructure/kafka/event-consumer.controller'
import { ReferralServiceClient, IdentityServiceClient, RewardServiceClient } from '@/infrastructure/external'

// Application
import {
  AuthorizationApplicationService,
  SystemAccountApplicationService,
  REFERRAL_REPOSITORY,
  TEAM_STATISTICS_REPOSITORY,
} from '@/application/services'
import { MonthlyAssessmentScheduler } from '@/application/schedulers'

// API
import {
  AuthorizationController,
  AdminAuthorizationController,
  HealthController,
  InternalAuthorizationController,
  InternalSelfApplyPhotosController,
} from '@/api/controllers'

// Shared
import { JwtStrategy } from '@/shared/strategies'
// [2026-02-17] 新增：预种计划授权限制
import { PrePlantingGuardModule } from './pre-planting/pre-planting-guard.module'

// Mock repositories for external services (should be replaced with actual implementations)
const MockReferralRepository = {
  provide: REFERRAL_REPOSITORY,
  useValue: {
    findByUserId: async () => null,
    getAllAncestors: async () => [],
    getAllDescendants: async () => [],
  },
}

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [appConfig, databaseConfig, redisConfig, kafkaConfig, jwtConfig],
    }),
    ScheduleModule.forRoot(),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      useFactory: () => ({
        secret: process.env.JWT_SECRET,
        signOptions: { expiresIn: process.env.JWT_EXPIRES_IN || '7d' },
      }),
    }),
    RedisModule,
    KafkaModule,
    // [2026-02-17] 新增：预种计划授权限制
    PrePlantingGuardModule,
  ],
  controllers: [
    AuthorizationController,
    AdminAuthorizationController,
    HealthController,
    InternalAuthorizationController,
    // [2026-03-02] 纯新增：自助申请照片内部 API
    InternalSelfApplyPhotosController,
    EventConsumerController,
  ],
  providers: [
    // Prisma
    PrismaService,

    // Repositories
    {
      provide: AUTHORIZATION_ROLE_REPOSITORY,
      useClass: AuthorizationRoleRepositoryImpl,
    },
    {
      provide: MONTHLY_ASSESSMENT_REPOSITORY,
      useClass: MonthlyAssessmentRepositoryImpl,
    },
    // [2026-01-04] 新增：系统账户仓储
    {
      provide: SYSTEM_ACCOUNT_REPOSITORY,
      useClass: SystemAccountRepositoryImpl,
    },
    // [2026-01-08] 新增：权益考核记录仓储
    {
      provide: BENEFIT_ASSESSMENT_RECORD_REPOSITORY,
      useClass: BenefitAssessmentRecordRepositoryImpl,
    },
    MockReferralRepository,

    // External Service Clients (replaces mock)
    ReferralServiceClient,
    IdentityServiceClient,
    RewardServiceClient,
    {
      provide: TEAM_STATISTICS_REPOSITORY,
      useExisting: ReferralServiceClient,
    },

    // Application Services
    AuthorizationApplicationService,
    // [2026-01-04] 新增：系统账户应用服务，用于系统账户报表统计
    SystemAccountApplicationService,
    MonthlyAssessmentScheduler,

    // Strategies
    JwtStrategy,
  ],
})
export class AppModule {}
