import { Router } from "express";
import type { Request, Response } from "express";
import { db } from "../../../pkg/db/db.js";
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
import { DrizzleError, eq } from "drizzle-orm";

const router = Router();

router.get("/", async (req: Request, res: Response) => {
    const userID = req.user?.id || "";
    try {
        // get the roles the user has
        const roles = await db
            .select()
            .from(userRoles)
            .where(eq(userRoles.userId, userID));

        return res.status(200).json({ success: true, data: roles });
    } catch (err) {
        if (err instanceof DrizzleError && err.cause) {
            return res
                .status(400)
                .json({ message: "db error", error: err.cause });
        }
    }
});

export default router;
