import { MiddlewareConsumer, Module, NestModule } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { AuthModule } from "./auth/auth.module";
import { AdminModule } from "./admin/admin.module";
import { ClientModule } from "./client/client.module";
import { UploadService } from "./upload/upload.service";
import { DocumentsService } from "./documents/documents.service";
import { S3Service } from "./s3/s3.service";
import { S3Module } from "./s3/s3.module";
import { PrismaService } from "./prisma/prisma.service";
import { PrismaModule } from "./prisma/prisma.module";
import { ConfigModule } from "@nestjs/config";
import { ConfigService } from "@nestjs/config";
import { AuthController } from "./auth/auth.controller";
import { DocumentsController } from "./documents/documents.controller";
import { JwtModule } from "@nestjs/jwt";
import { EmailModule } from "./email/email.module";
import { InitModule } from "./init/init.module";
import { HealthController } from "./health/health.controller";
import { corsConfig } from "./config/cors.config";

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    JwtModule.register({
      secret: process.env.JWT_SECRET,
      signOptions: { expiresIn: "1h" },
    }),
    AuthModule,
    AdminModule,
    ClientModule,
    S3Module,
    PrismaModule,
    EmailModule,
    InitModule,
  ],
  controllers: [
    AppController,
    AuthController,
    DocumentsController,
    HealthController,
  ],
  providers: [
    AppService,
    UploadService,
    DocumentsService,
    S3Service,
    PrismaService,
  ],
})
export class AppModule implements NestModule {
  constructor(private readonly configService: ConfigService) {}
  configure(consumer: MiddlewareConsumer) {
    const allowedOrigins = [
      "http://localhost:5173",
      "http://localhost:4173",
      "https://eu.i.posthog.com",
      "https://app.posthog.com",
      "https://eu.posthog.com",
      "https://www.placebo.mk",
      "https://placebo.mk",
      "https://imkapi.oblak.solutions",
    ];

    consumer
      .apply((req, res, next) => {
        const origin = req.headers.origin;
        if (req.method === "OPTIONS") {
          if (origin && corsConfig.origin.includes(origin)) {
            res.header("Access-Control-Allow-Origin", origin);
          }
          res.header("Access-Control-Allow-Credentials", "true");
          res.header(
            "Access-Control-Allow-Methods",
            corsConfig.methods.join(", "),
          );
          res.header(
            "Access-Control-Allow-Headers",
            corsConfig.allowedHeaders.join(", "),
          );
          res.header("Access-Control-Max-Age", "86400");
          res.status(204).end();

          return;
        }

        if (origin && corsConfig.origin.includes(origin)) {
          res.header("Access-Control-Allow-Origin", origin);
        }
        res.header("Access-Control-Allow-Credentials", "true");
        next();
      })
      .forRoutes("*");
  }
}
