import { Router } from "express";
import { assertBoard } from "./authz.js";
import { nexusSettingsService } from "../services/nexus-settings.js";

export function nexusSettingsRoutes(): Router {
  const router = Router();

  router.get("/nexus/settings", async (req, res) => {
    try {
      assertBoard(req);
      const settings = await nexusSettingsService().get();
      res.json(settings);
    } catch (err: unknown) {
      if (err && typeof err === "object" && "status" in err) {
        const e = err as { status: number; message: string };
        res.status(e.status).json({ error: e.message });
        return;
      }
      res.status(500).json({ error: "Unexpected error" });
    }
  });

  router.patch("/nexus/settings", async (req, res) => {
    try {
      assertBoard(req);
      const updated = await nexusSettingsService().set(req.body);
      res.json(updated);
    } catch (err: unknown) {
      if (err && typeof err === "object" && "status" in err) {
        const e = err as { status: number; message: string };
        res.status(e.status).json({ error: e.message });
        return;
      }
      res.status(500).json({ error: "Unexpected error" });
    }
  });

  return router;
}
