import express from "express";
import multer from "multer";
import { createCrudRouter } from "./crudRouter.js";
import {
  listUsers,
  getUser,
  serveUserPhoto,
  createUser,
  updateUser,
  deleteUser
} from "../controllers/usersController.js";

const router = express.Router();

// In-memory upload so the profile photo can be stored straight into the LONGBLOB.
const upload = multer({ storage: multer.memoryStorage() });

// Users: dedicated controller (photo blob handling + role mapping + audit logs).
router.get("/users", listUsers);
router.get("/users/:user_id", getUser);
router.get("/users/:user_id/photo", serveUserPhoto);
router.post("/users", upload.single("photo"), createUser);
router.put("/users/:user_id", upload.single("photo"), updateUser);
router.delete("/users/:user_id", deleteUser);

// Role assignment + access maps via generic CRUD.
router.use("/user_role", createCrudRouter("user_role", ["user_role_id"]));
router.use("/users_logs", createCrudRouter("users_logs", ["log_id"]));
router.use(
  "/admin_level_user_multi_city_map",
  createCrudRouter("admin_level_user_multi_city_map", ["user_id", "city_id"])
);

export default router;
