// apps/web/src/server/queries/admin-assignments.ts
import { and, eq } from 'drizzle-orm';
import type { DrizzleClient } from '@/server/db/client.js';
import { adminAssignments } from '@/server/db/schema.js';
import { upsertAssignment } from '@/server/db/queries/admin-assignments.js';

export type AssignScope = 'support_ticket' | 'vendor_application' | 'dispute' | 'flagged_content';

export async function getAssignment(
  db: DrizzleClient,
  scope: AssignScope,
  entityId: string,
): Promise<string | null> {
  const [row] = await db
    .select({ adminUserId: adminAssignments.adminUserId })
    .from(adminAssignments)
    .where(and(eq(adminAssignments.scope, scope), eq(adminAssignments.entityId, entityId)))
    .limit(1);
  return row?.adminUserId ?? null;
}

export async function setAssignment(
  db: DrizzleClient,
  scope: AssignScope,
  entityId: string,
  adminUserId: string,
): Promise<void> {
  await upsertAssignment(db, { scope, entityId, adminUserId });
}
