import { Module, MiddlewareConsumer, NestModule } from "@nestjs/common";
import { ConfigModule } from "@nestjs/config";
import { SecureErrorMapperService } from "./services/secure-error-mapper.service";
import { CsrfService } from "./services/csrf.service";
import { CsrfMiddleware } from "./middleware/csrf.middleware";
import { CsrfController } from "./controllers/csrf.controller";

@Module({
  imports: [ConfigModule],
  controllers: [CsrfController],
  providers: [SecureErrorMapperService, CsrfService, CsrfMiddleware],
  exports: [SecureErrorMapperService, CsrfService],
})
export class SecurityModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // Apply CSRF middleware to all routes except those handled by the middleware itself
    consumer.apply(CsrfMiddleware).forRoutes("*");
  }
}
