const express = require("express");
const session = require("express-session");
const bcrypt = require("bcrypt");
const db = require("./db");
const fs = require("fs");
const path = require("path");
const {  requireLogin,  requireAdmin} = require("./middleware/auth.middleware");
const adminRoutes = require("./routes/admin.routes");
const dashboardRoutes = require("./routes/dashboard.routes");
const helmet = require("helmet");
const sessionStore = require("./config/session");
const patientRoutes = require("./routes/patient.routes");
const medicationRoutes = require("./routes/medication.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");



require("dotenv").config();

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.set("view engine", "ejs");

app.use("/patients", require("./routes/patient.routes"));

app.use("/uploads", express.static("uploads"));


/* ===============================
   COMPANYDATA
================================ */
app.use(companySettingsRoutes);


/* ===============================
   LOGIN
================================ */
const authRoutes = require("./routes/auth.routes");

app.use("/", authRoutes);

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

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

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

/* ===============================
   MEDIKAMENTENÜBERSICHT
================================ */
app.use("/medications", medicationRoutes);
app.use("/patients", patientMedicationRoutes);


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

 // ===============================
// Leistungen
// ===============================
app.use("/services", serviceRoutes);
app.use("/patients", patientServiceRoutes);

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

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

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

/* ===============================
   SERVER
================================ */
const PORT = 51777;          // garantiert frei
const HOST = "127.0.0.1";   // kein HTTP.sys

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