import express from "express";
import authRoutes from "./routes/auth.routes.js";
import userRoutes from "./routes/user.routes.js";
import friendRequestRoutes from "./routes/friendRequest.routes.js";
import messageRoutes from "./routes/message.routes.js";
import dotenv from "dotenv";
import connectMongoDB from "./db/connectMongoDB.js";
import cookieParser from "cookie-parser";
import { v2 as cloudinary } from "cloudinary";
import cors from "cors";
import { app, server } from "./socket/index.js";
import path from "path";

const __dirname = path.resolve()


dotenv.config();
cloudinary.config({
  cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

const PORT = process.env.PORT;
const corsOptions = {
  origin: ["https://chathub.up.railway.app", "http://localhost:5173", 'https://chat-hub-aoza.onrender.com'],
  credentials: true,
};

app.use(express.json({ limit: "5mb" }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(cors(corsOptions));

app.use("/api/auth", authRoutes); 
app.use("/api/user", userRoutes);
app.use("/api/friend-request", friendRequestRoutes);
app.use("/api/message", messageRoutes);

if (process.env.NODE_ENV === "production") {
	app.use(express.static(path.join(__dirname, "/client/dist")));

	app.get("*", (req, res) => {
		res.sendFile(path.resolve(__dirname, "client", "dist", "index.html"));
	});
}

server.listen(PORT, () => {
  console.log(`server is listening on ${PORT}`);
  connectMongoDB();
});
