import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { ScheduleModule } from '@nestjs/schedule';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { APP_GUARD, APP_FILTER } from '@nestjs/core';
import { join } from 'path';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DatabaseModule } from './database/database.module';
import { CommonModule } from './common/common.module';
import { AuthModule } from './modules/auth/auth.module';
import { ChildrenModule } from './modules/children/children.module';
import { FamiliesModule } from './modules/families/families.module';
import { TrackingModule } from './modules/tracking/tracking.module';
import { VoiceModule } from './modules/voice/voice.module';
import { AIModule } from './modules/ai/ai.module';
import { NotificationsModule } from './modules/notifications/notifications.module';
import { AnalyticsModule } from './modules/analytics/analytics.module';
import { FeedbackModule } from './modules/feedback/feedback.module';
import { PhotosModule } from './modules/photos/photos.module';
import { ComplianceModule } from './modules/compliance/compliance.module';
import { GraphQLCustomModule } from './graphql/graphql.module';
import { JwtAuthGuard } from './modules/auth/guards/jwt-auth.guard';
import { ErrorTrackingService } from './common/services/error-tracking.service';
import { GlobalExceptionFilter } from './common/filters/global-exception.filter';
import { HealthCheckService } from './common/services/health-check.service';
import { HealthController } from './common/controllers/health.controller';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: '.env',
    }),
    GraphQLModule.forRootAsync<ApolloDriverConfig>({
      driver: ApolloDriver,
      inject: [ConfigService],
      imports: [GraphQLCustomModule],
      useFactory: (configService: ConfigService) => ({
        autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
        sortSchema: true,
        playground: configService.get('NODE_ENV') !== 'production',
        introspection: true,
        context: ({ req, res }, ...args) => {
          // DataLoaders will be provided via REQUEST scope
          return { req, res };
        },
        formatError: (error) => {
          return {
            message: error.message,
            code: error.extensions?.code || 'INTERNAL_SERVER_ERROR',
            path: error.path,
          };
        },
      }),
    }),
    ScheduleModule.forRoot(),
    DatabaseModule,
    CommonModule,
    AuthModule,
    ChildrenModule,
    FamiliesModule,
    TrackingModule,
    VoiceModule,
    AIModule,
    NotificationsModule,
    AnalyticsModule,
    FeedbackModule,
    PhotosModule,
    ComplianceModule,
    GraphQLCustomModule,
  ],
  controllers: [AppController, HealthController],
  providers: [
    AppService,
    ErrorTrackingService,
    HealthCheckService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    {
      provide: APP_FILTER,
      useClass: GlobalExceptionFilter,
    },
  ],
})
export class AppModule {}
