import { dispatchEmail } from '../delivery/dispatcher.js';
import type { ReviewItemDoc } from './types.js';

const noopLog = {
  info: (..._args: unknown[]) => {},
  error: (..._args: unknown[]) => {},
};

export async function notifyReviewAssigned(
  review: ReviewItemDoc,
  log: { info: (...args: unknown[]) => void; error: (...args: unknown[]) => void } = noopLog
): Promise<void> {
  if (!review.assignedTo) return;

  log.info(
    { reviewId: review.id, assignedTo: review.assignedTo },
    '[reviews] Review assignment notification queued'
  );

  const to = `${review.assignedTo}@bytelyst.local`;
  await dispatchEmail(
    {
      to,
      templateId: 'review-assigned',
      variables: {
        title: review.title,
        description: review.description ?? '',
        runId: review.runId ?? '',
      },
      productId: review.productId,
      userId: review.assignedTo,
    },
    log
  );
}
