import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CohortModule } from './modules/cohort/cohort.module';
import { UserModule } from './modules/user/user.module';
import { CompaniesModule } from './modules/companies/companies.module';
import { JobPostsModule } from './modules/job_posts/job_posts.module';
import { LocationsModule } from './modules/locations/locations.module';
import { StatsModule } from './modules/stats/stats.module';
import dbConfiguration from './config/db.config';
import readDbConfiguration from './config/read_db.config';
import { GlobalExceptionFilter } from './middleware/global-exception.filter';
import { AuthModule } from './modules/auth/auth.module';
import { PassportModule } from '@nestjs/passport';
import { SessionSerializer } from './modules/auth/session.serializer';
import { JwtAuthModule } from './jwt.module';
import { APP_GUARD, APP_FILTER } from '@nestjs/core';
import { JwtAuthGuard } from './modules/auth/jwt-auth.guard';
import { jwtConstants } from './config/common';
import { JwtModule } from '@nestjs/jwt';
import { UserController } from './modules/user/user.controller';
import { JobTypeModule } from './modules/job_type/job_type.module';
import { JobCategoriesModule } from './modules/job_categories/job_categories.module';
import { EducationLevelsModule } from './modules/education_levels/education_levels.module';
import { ExperienceModule } from './modules/experience/experience.module';
import { IndustriesModule } from './modules/industries/industries.module';
import { RoleLevelsModule } from './modules/role_levels/role_levels.module';
import { RolesModule } from './modules/roles/roles.module';
import { JobLevelModule } from './modules/job_level/job_level.module';
import { JobApplicationsModule } from './modules/job_applications/job_applications.module';
import { JobAlertsModule } from './modules/job_alerts/job_alerts.module';
import { MessagesModule } from './modules/messages/messages.module';
import { SavedCandidatesModule } from './modules/saved_candidates/saved_candidates.module';
import { DashboardModule } from './modules/dashboard/dashboard.module';
import { SeekerResumesModule } from './modules/seeker_resumes/seeker_resumes.module';
import { SkillsModule } from './modules/skills/skills.module';
import { LanguagesModule } from './modules/languages/languages.module';
import { SeekerEducationModule } from './modules/seeker_education/seeker_education.module';
import { SeekerExperienceModule } from './modules/seeker_experience/seeker_experience.module';
import { SocialPlatformModule } from './modules/social_platform/social_platform.module';
import { SeekerSocialLinksModule } from './modules/seeker_social_links/seeker_social_links.module';
import { CountryModule } from './modules/country/country.module';
import { CityModule } from './modules/city/city.module';
import { StateModule } from './modules/state/state.module';
import { SeekerAddressModule } from './modules/seeker_address/seeker_address.module';
import { JobSavedModule } from './modules/job_saved/job_saved.module';
import { SeekerPreferencesModule } from './modules/seeker_preferences/seeker_preferences.module';
import { JobViewsModule } from './modules/job_views/job_views.module';
import { FranchisesModule } from './modules/franchises/franchises.module';
import { AnalyticsModule } from './modules/analytics/analytics.module';
import { ReportsHistoryModule } from './modules/reports_history/reports_history.module';
import { TempFranchiseModule } from './modules/temp_franchise/temp_franchise.module';
import { RoleAccessModule } from './modules/role_access/role_access.module';
import { ModulesModule } from './modules/modules/modules.module';
import { TagsModule } from './modules/tags/tags.module';
import { JobsPostTagModule } from './modules/jobs_post_tag/jobs_post_tag.module';
import { TempTrainersModule } from './modules/temp_trainers/temp_trainers.module';
import { TrainersModule } from './modules/trainers/trainers.module';
import { PerksModule } from './modules/perks/perks.module';
import { JobPerksModule } from './modules/job_perks/job_perks.module';
import { CompanySuggestionsModule } from './modules/company_suggestions/company_suggestions.module';
import { AreaModule } from './modules/area/area.module';
import { ConfigSettingModule } from './modules/config_setting/config_setting.module';
import { RecruiterCvUsageModule } from './modules/recruiter_cv_usage/recruiter_cv_usage.module';
import { AssociateCvUsageModule } from './modules/associate_cv_usage/associate_cv_usage.module';
import { AssociatesModule } from './modules/associates/associates.module';
import { ActivityLogsModule } from './modules/activity_logs/activity_logs.module';
import { CompanySizesModule } from './modules/company_sizes/company_sizes.module';
import { OrganizationTypesModule } from './modules/organization_types/organization_types.module';
import { AiInterviewSchedulesModule } from './modules/ai_interview_schedules/ai_interview_schedules.module';
import { EmailTokensModule } from './modules/email_tokens/email_tokens.module';
import { EmailModule } from './modules/email/email.module';
import { ScheduleModule } from '@nestjs/schedule';
import { CacheModule } from '@nestjs/cache-manager';
import { AiScreeningModule } from './modules/ai_screening/ai_screening.module';
import { UserAiInterviewsModule } from './modules/user_ai_interviews/user_ai_interviews.module';
import { QuestionBankModule } from './modules/question_bank/question_bank.module';
import { MockTestAttemptsModule } from './modules/mock_test_attempts/mock_test_attempts.module';
import { SitemapModule } from './modules/sitemap/sitemap.module';
import { MobileAuthModule } from './mobile/auth/mobile-auth.module';
import { MobileHomeModule } from './mobile/home/mobile-home.module';
import { MobileOnboardingModule } from './mobile/onboarding/mobile-onboarding.module';
import { MobileSeekerProfileSettingsModule } from './mobile/seeker/profile-settings/mobile-seeker-profile-settings.module';
import { MobileAppliedJobsModule } from './mobile/seeker/applied-jobs/mobile-applied-jobs.module';
import { MobileSavedJobsModule } from './mobile/seeker/saved-jobs/mobile-saved-jobs.module';
import { MobileProfileModule } from './mobile/profile/mobile-profile.module';
import { MobileDashboardModule } from './mobile/dashboard/mobile-dashboard.module';
import { MobileDegreeModule } from './mobile/degree/mobile-degree.module';
import { MobileSpecializationModule } from './mobile/specialization/mobile-specialization.module';
import { MobileNotificationsModule } from './mobile/notifications/mobile-notifications.module';
import { MockTemplatesModule } from './modules/mock_templates/mock_templates.module';
import { PlansModule } from './modules/plans/plans.module';
import { PaymentsModule } from './modules/payments/payments.module';
import { WalletModule } from './modules/wallet/wallet.module';
import { FeatureUsageModule } from './modules/feature_usage/feature-usage.module';
import { CouponModule } from './modules/coupons/coupon.module';
import { JobPromotionModule } from './modules/job_promotions/job-promotion.module';
import { SubscriptionModule } from './modules/subscriptions/subscription.module';
import { CronModule } from './modules/cron/cron.module';
import { ProfileUnlockModule } from './modules/profile_unlock/profile_unlock.module';
import { NotificationModule } from './modules/notifications/notifications.module';
import { InvoiceIssuersModule } from './modules/invoice_issuers/invoice_issuers.module';
import { ReportsModule } from './modules/reports/reports.module';
import { PageSeoMetadataModule } from './modules/page_seo_metadata/page_seo_metadata.module';
import { SeoTemplatesModule } from './modules/seo_templates/seo_templates.module';
import { UserSessionsModule } from './modules/user_sessions/user_sessions.module';
import { IntakeModule } from './modules/intake/intake.module';
import { EmailTemplatesModule } from './modules/email_templates/email_templates.module';
import { UserNotificationsModule } from './modules/user_notifications/user_notifications.module';
import { JobInvitationsModule } from './modules/job_invitations/job_invitations.module';
import { CertificatesModule } from './modules/certificates/certificates.module';
import { SeekerCertificationsModule } from './modules/seeker_certifications/seeker_certifications.module';

@Module({
  imports: [
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), 'uploads'),
      serveRoot: '/uploads',
      serveStaticOptions: {
        index: false,
      },
    }),
    ScheduleModule.forRoot(),
    CacheModule.register({
      isGlobal: true,
      ttl: 3600, // 1 hour default
    }),
    JwtModule.register({
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '24h' },
    }),
    ConfigModule.forRoot({
      isGlobal: true,
      load: [dbConfiguration, readDbConfiguration],
    }),
    TypeOrmModule.forRootAsync({
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const databaseConfig = await configService.get('database');
        return {
          ...databaseConfig,
        };
      },
    }),
    TypeOrmModule.forRootAsync({
      name: 'read_database',
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const databaseConfig = await configService.get('read_database');
        return {
          ...databaseConfig,
        };
      },
    }),
    CohortModule,
    UserModule,
    CompaniesModule,
    JobPostsModule,
    LocationsModule,
    StatsModule,
    JobTypeModule,
    JobCategoriesModule,
    EducationLevelsModule,
    ExperienceModule,
    IndustriesModule,
    RoleLevelsModule,
    RolesModule,
    JobLevelModule,
    JobApplicationsModule,
    JobAlertsModule,
    MessagesModule,
    SavedCandidatesModule,
    DashboardModule,
    SkillsModule,
    LanguagesModule,
    AuthModule,
    SeekerResumesModule,
    SeekerEducationModule,
    SeekerExperienceModule,
    SocialPlatformModule,
    SeekerSocialLinksModule,
    CountryModule,
    CityModule,
    StateModule,
    SeekerAddressModule,
    JobSavedModule,
    SeekerPreferencesModule,
    JobViewsModule,
    JobAlertsModule,
    FranchisesModule,
    AnalyticsModule,
    ReportsHistoryModule,
    TempFranchiseModule,
    RoleAccessModule,
    ModulesModule,
    TagsModule,
    JobsPostTagModule,
    PassportModule.register({ session: true }),
    JwtAuthModule,
    TempTrainersModule,
    TrainersModule,
    PerksModule,
    JobPerksModule,
    CompanySuggestionsModule,
    AreaModule,
    RecruiterCvUsageModule,
    AssociateCvUsageModule,
    ConfigSettingModule,
    AssociatesModule,
    AssociatesModule,
    ActivityLogsModule,
    CompanySizesModule,
    OrganizationTypesModule,
    AiInterviewSchedulesModule,
    EmailTokensModule,
    EmailModule,
    AiScreeningModule,
    UserAiInterviewsModule,
    QuestionBankModule,
    MockTestAttemptsModule,
    SitemapModule,
    MobileAuthModule,
    MobileHomeModule,
    MobileOnboardingModule,
    MobileSeekerProfileSettingsModule,
    MobileAppliedJobsModule,
    MobileProfileModule,
    MobileDashboardModule,
    MobileSavedJobsModule,
    MobileDegreeModule,
    MobileSpecializationModule,
    MobileNotificationsModule,
    MockTemplatesModule,
    // Billing & Monetization Modules
    PlansModule,
    PaymentsModule,
    WalletModule,
    FeatureUsageModule,
    CouponModule,
    JobPromotionModule,
    SubscriptionModule,
    CronModule,
    ProfileUnlockModule,
    NotificationModule,
    InvoiceIssuersModule,
    ReportsModule,
    PageSeoMetadataModule,
    SeoTemplatesModule,
    UserSessionsModule,
    IntakeModule,
    EmailTemplatesModule,
    UserNotificationsModule,
    JobInvitationsModule,
    CertificatesModule,
    SeekerCertificationsModule,
  ],
  controllers: [AppController, UserController],
  providers: [
    {
      provide: AppService,
      useClass: GlobalExceptionFilter,
    },
    {
      provide: APP_FILTER,
      useClass: GlobalExceptionFilter,
    },
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    SessionSerializer,
  ],
})
export class AppModule { }
