import { v } from "convex/values";
import { query } from "./_generated/server";

/**
 * Listar todas as roles
 */
export const listar = query({
  args: {},
  returns: v.array(
    v.object({
      _id: v.id("roles"),
      _creationTime: v.number(),
      nome: v.string(),
      descricao: v.string(),
      nivel: v.number(),
      setor: v.optional(v.string()),
      customizado: v.boolean(),
      editavel: v.optional(v.boolean()),
      criadoPor: v.optional(v.id("usuarios")),
    })
  ),
  handler: async (ctx) => {
    return await ctx.db.query("roles").collect();
  },
});

/**
 * Buscar role por ID
 */
export const buscarPorId = query({
  args: {
    roleId: v.id("roles"),
  },
  returns: v.union(
    v.object({
      _id: v.id("roles"),
      nome: v.string(),
      descricao: v.string(),
      nivel: v.number(),
      setor: v.optional(v.string()),
    }),
    v.null()
  ),
  handler: async (ctx, args) => {
    return await ctx.db.get(args.roleId);
  },
});

