import express from "express";
import cookieParser from "cookie-parser";
import { commonSecurity } from "./middleware/security.js";
import { errorHandler } from "./middleware/errorHandler.js";
import { env } from "./config/env.js";
import authRoutes from "./modules/auth/routes.js";
import dashboardRoutes from "./modules/dashboard/routes.js";
import auditRoutes from "./modules/audit/routes.js";
import committeeRoutes from "./modules/committees/routes.js";
import userRoutes from "./modules/users/routes.js";
import caseRoutes from "./modules/cases/routes.js";
import deadlineRoutes from "./modules/deadlines/routes.js";
import meetingRoutes from "./modules/meetings/routes.js";
import agendaRoutes from "./modules/agenda-items/routes.js";
import decisionRoutes from "./modules/decisions/routes.js";
import taskRoutes from "./modules/tasks/routes.js";
import documentRoutes, { publicDocumentsRouter } from "./modules/documents/routes.js";
import commentRoutes from "./modules/comments/routes.js";
import templateRoutes from "./modules/templates/routes.js";
import notificationRoutes from "./modules/notifications/routes.js";
import calendarRoutes from "./modules/calendar/routes.js";
import { runBootstrapMigrations } from "./db/bootstrap.js";
import reportRoutes from "./modules/reports/routes.js";
import permissionRoutes from "./modules/permissions/routes.js";
import financeRoutes from "./modules/finance/routes.js";
import kummerkastenRoutes, { publicKummerkastenRouter } from "./modules/kummerkasten/routes.js";
import departmentRoutes from "./modules/departments/routes.js";
import documentCategoryRoutes from "./modules/document-categories/routes.js";

const app = express();

app.use(commonSecurity);
app.use(express.json({ limit: "10mb" }));
app.use(cookieParser());

app.get("/health", (req, res) => res.json({ ok: true, env: env.nodeEnv }));
app.use("/api/auth", authRoutes);
app.use("/api/dashboard", dashboardRoutes);
app.use("/api/committees", committeeRoutes);
app.use("/api/users", userRoutes);
app.use("/api/cases", caseRoutes);
app.use("/api/deadlines", deadlineRoutes);
app.use("/api/meetings", meetingRoutes);
app.use("/api/agenda-items", agendaRoutes);
app.use("/api/decisions", decisionRoutes);
app.use("/api/tasks", taskRoutes);
app.use("/api/documents", documentRoutes);
app.use("/api/comments", commentRoutes);
app.use("/api/templates", templateRoutes);
app.use("/api/notifications", notificationRoutes);
app.use("/api/calendar", calendarRoutes);
app.use("/api/audit", auditRoutes);
app.use("/api/reports", reportRoutes);
app.use("/api/permissions", permissionRoutes);
app.use("/api/finance", financeRoutes);
app.use("/api/kummerkasten", kummerkastenRoutes);
app.use("/api/departments", departmentRoutes);
app.use("/api/document-categories", documentCategoryRoutes);
app.use("/api/public/kummerkasten", publicKummerkastenRouter);
app.use("/api/public/documents", publicDocumentsRouter);
app.use(errorHandler);

async function start() {
  try {
    console.log("[startup] starting backend");
    await runBootstrapMigrations();
    console.log("[startup] migrations done");

    app.listen(env.port, "0.0.0.0", () => {
      console.log(`BR-Office backend listening on :${env.port}`);
    });
  } catch (error) {
    console.error("[startup] fatal error");
    console.error(error);
    process.exit(1);
  }
}

start();
