"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const auth_1 = require("../modules/auth");
const api = __importStar(require("../api/users"));
const router = (0, express_1.Router)();
// ── User CRUD ─────────────────────────────────────────────────────────────────
/**
 * GET /api/users
 * List users with optional filters.
 * Query: ?role=reviewer&roleType=admin&status=active&limit=50&offset=0
 */
router.get('/', async (req, res) => {
    const result = await api.listUsers({
        role: req.query.role,
        roleType: req.query.roleType,
        status: req.query.status,
        limit: req.query.limit ? parseInt(req.query.limit, 10) : undefined,
        offset: req.query.offset ? parseInt(req.query.offset, 10) : undefined,
    });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/users/:id
 * Get a single user by ID.
 */
router.get('/:id', async (req, res) => {
    const result = await api.getUser({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/users
 * Create a new user.
 * Body: { external_id, email?, display_name?, roles?: [{ role, type }], metadata? }
 */
router.post('/', auth_1.requireAdmin, async (req, res) => {
    const result = await api.createUser(req.body || {});
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/users/:id
 * Update a user.
 * Body: { email?, display_name?, status?, metadata? }
 */
router.put('/:id', auth_1.requireAdmin, async (req, res) => {
    const result = await api.updateUser({ id: req.params.id, ...(req.body || {}) });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/users/:id
 * Delete a user.
 */
router.delete('/:id', auth_1.requireAdmin, async (req, res) => {
    const result = await api.deleteUser({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
// ── Role management ─────────────────────────────────────────────────────────
/**
 * GET /api/users/:id/roles
 * Get all roles for a user.
 */
router.get('/:id/roles', async (req, res) => {
    const result = await api.getUserRoles({ id: req.params.id });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/users/:id/roles
 * Add a role to a user.
 * Body: { role, type } — type must be superadmin, admin, or member
 */
router.post('/:id/roles', auth_1.requireAdmin, async (req, res) => {
    const { role, type } = req.body || {};
    const result = await api.addUserRole({ id: req.params.id, role, type });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/users/:id/roles/:role
 * Remove a role from a user.
 */
router.delete('/:id/roles/:role', auth_1.requireAdmin, async (req, res) => {
    const result = await api.removeUserRole({ id: req.params.id, role: req.params.role });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
