import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { APP_GUARD } from '@nestjs/core';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ArticlesModule } from './modules/articles.module';
import { StrapiModule } from './modules/strapi.module';
import { LiveBlogModule } from './modules/live-blog.module';
import { UserModule } from './modules/users/user.module';
import { AuthModule } from './modules/auth/auth.module';
import { CommentModule } from './modules/comment/comment.module';
import { AnalyticsModule } from './modules/analytics/analytics.module';
import { PushModule } from './modules/push/push.module';
import {
  Article,
  Author,
  Category,
  LiveBlog,
  LiveBlogUpdate,
  User,
  Comment,
  Reaction,
} from './modules/entities';
import { ShareEvent } from './modules/analytics/analytics.entity';
import { PushSubscriptionEntity } from './modules/push/push-subscription.entity';
import { JwtAuthPublicGuard } from './modules/auth/jwt-auth-public.guard';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.DATABASE_HOST || 'localhost',
      port: parseInt(process.env.DATABASE_PORT || '5432', 10),
      username: process.env.DATABASE_USERNAME || 'placebo_user',
      password: process.env.DATABASE_PASSWORD || 'placebo_password',
      database: process.env.DATABASE_NAME || 'placebo_backend_db',
      entities: [
        Article,
        Author,
        Category,
        LiveBlog,
        LiveBlogUpdate,
        User,
        Comment,
        Reaction,
        ShareEvent,
        PushSubscriptionEntity,
      ],
      synchronize: process.env.DATABASE_SYNCHRONIZE === 'true',
      logging: process.env.DATABASE_LOGGING === 'true',
    }),
    ArticlesModule,
    StrapiModule,
    LiveBlogModule,
    UserModule,
    AuthModule,
    CommentModule,
    AnalyticsModule,
    PushModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthPublicGuard,
    },
  ],
})
export class AppModule {}
