// apps/web/src/server/notifications/admin-routing.ts
import type { DrizzleClient } from '@/server/db/client.js';
import {
  getAssignment,
  setAssignment,
  type AssignScope,
} from '@/server/queries/admin-assignments.js';

export type { AssignScope };

/**
 * Resolve which admin user is assigned to handle a given scoped entity.
 * Returns null if no assignment exists.
 */
export async function resolveAdminForEntity(
  db: DrizzleClient,
  scope: AssignScope,
  entityId: string,
): Promise<string | null> {
  return getAssignment(db, scope, entityId);
}

/**
 * Assign (or reassign) an admin user to a scoped entity.
 * Upserts: updates existing assignment or creates a new one.
 */
export async function assignAdmin(
  db: DrizzleClient,
  scope: AssignScope,
  entityId: string,
  adminUserId: string,
): Promise<void> {
  return setAssignment(db, scope, entityId, adminUserId);
}
