import "dotenv/config";
import express, { json } from "express";
import mongoose, { connect } from "mongoose";
import cors from "cors";
import passport from "passport";
import "./config/passport.js";
import http from "http";
import { Server } from "socket.io";
import initSocket from "./config/socketio.js";
// import authRoutes from "./routes/auth.js";
// import uploadImagesRoutes from "./routes/upload-image.js";
// import userRoutes from "./routes/user-api.js";
// import patientVerificationRoutes from "./routes/verification-api.js";
// import fetchDataRoutes from "./routes/fetch-data.js";
// import adminRoutes from "./routes/admin.js";
// import predictRoutes from "./routes/predict.js";

const PORT = process.env.PORT;
const app = express();

app.use(json());
app.use(
  cors({
    // origin: ["http://10.0.2.2:5000/api", "http://localhost:3000/api"],
    origin: "*",
    methods: ["GET", "POST", "PUT", "DELETE", "PATCH"],
    allowedHeaders: ["Content-Type", "Authorization"],
  }),
);

app.use(passport.initialize());

const server = http.createServer(app);
initSocket(server);

connect(process.env.MONGO_URI)
  .then(() =>
    console.log("MongoDB Connected on database:", mongoose.connection.name),
  )
  .catch((err) => console.error("MongoDB Connection Error:", err));

server.listen(PORT, "0.0.0.0", () => {
  console.log(`Server running on port ${PORT}`);
});

app.get("/", (req, res) => {
  res.send("Halo, ini muncul berarti sudah terkoneksi ke backend");
});

// app.use("/api/auth", authRoutes);
// app.use("/api/upload-image", uploadImagesRoutes);
// app.use("/api/user-api", userRoutes);
// app.use("/api/verification-api", patientVerificationRoutes);
// app.use("/api/fetch-data", fetchDataRoutes);
// app.use("/api/admin", adminRoutes);
// app.use("/api/predict", predictRoutes);
// app.use("/api/chat", chatRoutes);

// import { networkInterfaces } from "os";
// app.get("/api/healthcheck", (req, res) => {
//   res.json({
//     status: "active",
//     timestamp: new Date().toISOString(),
//   });
// });
// app.get("/", (req, res) => {
//   res.send("API is working!");
// });
// const getIPAddress = () => {
//   const interfaces = networkInterfaces();
//   for (const name in interfaces) {
//     for (const face of interfaces[name]) {
//       if (face.family === "IPv4" && !face.internal) {
//         return face.address;
//       }
//     }
//   }
//   return "0.0.0.0";
// };
// app.listen(PORT, "0.0.0.0", () => {
//   console.log(`Server running on port ${PORT}`);
//   console.log(`Access via:
//   - Local: http://localhost:${PORT}
//   - Network: http://${getIPAddress()}:${PORT}`);
// });
