// src/config/core.config.ts
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import HttpErrorResponse from '../exception/httpErrorResponse.config';
import { logger } from '../logger/logger.config';

const allowedOrigins = [
  'http://localhost:3000',
  'http://localhost:5173',
  'http://localhost:5174',
  'https://team-tracker.rinov8.studio',
  'https://tracker-management.netlify.app',
  'https://educator.faysal.design',
  'https://staff.tarbiyah.school',
];

const corsOptions: CorsOptions = {
  origin: (origin, callback) => {
    logger.info("Request's origin: " + origin);
    if (allowedOrigins.indexOf(origin) !== -1 || !origin) {
      logger.info('Request has been allowed by CORS: ' + origin);
      callback(null, true);
    } else {
      callback(
        new HttpErrorResponse('Request has not been allowed by CORS', 403),
      );
    }
  },
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  credentials: true,
  optionsSuccessStatus: 204,
};

export default corsOptions;
