import cors from "cors";
import express from "express";
import helmet from "helmet";
import responseTime from "response-time";

import { configurePassportAuth } from "@/contexts/auth";
import morgan from "morgan";
import passport from "passport";
import path from "path";
import { config } from "../../config";
import { v1Routes } from "./api/v1";

//const logger = initLogger(rTracer);

// Create Express server
const app = express();

//app.use(rTracer.expressMiddleware());
app.disable("x-powered-by");
app.use(express.json());
app.use(express.text());
app.use(express.urlencoded({ extended: true }));

// set up the response-time middleware
app.use(responseTime());

// enable CORS - Cross Origin Resource Sharing
app.use(
  cors({
    origin: config.server.cors_origin,
    methods: "GET,POST,PUT,DELETE",
    credentials: true,

    exposedHeaders: [
      "Access-Control-Allow-Headers",
      "Access-Control-Allow-Origin",
      "Content-Disposition",
      "Content-Type",
      "Content-Length",
      "X-Total-Count",
      "Pagination-Count",
      "Pagination-Page",
      "Pagination-Limit",
    ],
  })
);

// secure apps by setting various HTTP headers
app.use(helmet());

// request logging. dev: console | production: file
//app.use(morgan('common'));
app.use(morgan("dev"));

// Autentication
app.use(passport.initialize());
configurePassportAuth(passport);

// Express configuration
app.set("port", process.env.PORT ?? 3001);

// Public assets
app.use("/assets", express.static(path.join(__dirname, "/public")));

// API
app.use("/api/v1", v1Routes());

export { app };
