import { v } from "convex/values";

import { query } from "./_generated/server";
import { requireOwnerId } from "./authz";

export const list = query({
  args: { projectId: v.id("projects") },
  handler: async (ctx, args) => {
    const ownerId = await requireOwnerId(ctx);
    const project = await ctx.db.get("projects", args.projectId);
    if (!project || project.ownerId !== ownerId) {
      return [];
    }
    return await ctx.db
      .query("projectArtifacts")
      .withIndex("by_project", (q) => q.eq("projectId", args.projectId))
      .take(25);
  },
});
