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

export const list = query({
  args: { tenantId: v.string(), viewerId: v.id("users") },
  handler: async (ctx, { tenantId, viewerId }) => {
    await requireStaff(ctx, viewerId, tenantId)
    const companies = await ctx.db
      .query("companies")
      .withIndex("by_tenant", (q) => q.eq("tenantId", tenantId))
      .collect()
    return companies.map((c) => ({ id: c._id, name: c.name }))
  },
})

