const express = require("express");
const session = require("express-session");
const helmet = require("helmet");
const sessionStore = require("./config/session");
require("dotenv").config();

const adminRoutes = require("./routes/admin.routes");
const dashboardRoutes = require("./routes/dashboard.routes");
const patientRoutes = require("./routes/patient.routes");
const medicationRoutes = require("./routes/medications.routes");
const patientMedicationRoutes = require("./routes/patientMedication.routes");
const waitingRoomRoutes = require("./routes/waitingRoom.routes");
const serviceRoutes = require("./routes/service.routes");
const patientServiceRoutes = require("./routes/patientService.routes");
const invoiceRoutes = require("./routes/invoice.routes");
const patientFileRoutes = require("./routes/patientFile.routes");
const companySettingsRoutes = require("./routes/companySettings.routes");
const authRoutes = require("./routes/auth.routes");

const app = express();

/* ===============================
   MIDDLEWARE
================================ */
app.use(express.urlencoded({ extended: true }));
app.use(helmet());

app.use(
  session({
    name: "praxis.sid",
    secret: process.env.SESSION_SECRET,
    store: sessionStore,
    resave: false,
    saveUninitialized: false,
  })
);

const flashMiddleware = require("./middleware/flash.middleware");
app.use(flashMiddleware);

app.use(express.static("public"));
app.use("/uploads", express.static("uploads"));
app.set("view engine", "ejs");

/* ===============================
   COMPANY SETTINGS
================================ */
app.use(companySettingsRoutes);

/* ===============================
   AUTH / LOGIN
================================ */
app.use("/", authRoutes);

/* ===============================
   DASHBOARD
================================ */
app.use("/dashboard", dashboardRoutes);

/* ===============================
   ADMIN
================================ */
app.use("/admin", adminRoutes);

/* ===============================
   PATIENTEN
================================ */
app.use("/patients", patientRoutes);
app.use("/patients", patientMedicationRoutes);
app.use("/patients", patientServiceRoutes);

/* ===============================
   MEDIKAMENTE
================================ */
app.use("/medications", medicationRoutes);
console.log("🧪 /medications Router mounted");

/* ===============================
   LEISTUNGEN
================================ */
app.use("/services", serviceRoutes);

/* ===============================
   DATEIEN
================================ */
app.use("/", patientFileRoutes);

/* ===============================
   WARTEZIMMER
================================ */
app.use("/", waitingRoomRoutes);

/* ===============================
   RECHNUNGEN
================================ */
app.use("/", invoiceRoutes);

/* ===============================
   LOGOUT
================================ */
app.get("/logout", (req, res) => {
  req.session.destroy(() => res.redirect("/"));
});

/* ===============================
   ERROR HANDLING
================================ */
app.use((err, req, res, next) => {
  console.error(err);
  res.status(500).send("Interner Serverfehler");
});

/* ===============================
   SERVER
================================ */
const PORT = 51777;
const HOST = "127.0.0.1";

app.listen(PORT, HOST, () => {
  console.log(`Server läuft auf http://${HOST}:${PORT}`);
});
