import prisma from './prisma';
import { AppError } from './utils';

const VERIFY_BEFORE_HIRE =
  'Verify your identity before hiring a worker. Upload your ID documents in Account → Verification.';
const VERIFY_BEFORE_MESSAGE =
  'Verify your identity before messaging workers. Upload your ID documents in Account → Verification.';

export async function assertCustomerVerified(
  userId: string,
  action: 'hire' | 'message' = 'hire'
) {
  const customerProfile = await prisma.customerProfile.findUnique({
    where: { userId },
    select: { id: true, isVerified: true },
  });
  if (!customerProfile) throw new AppError('Customer profile not found', 404);
  if (!customerProfile.isVerified) {
    throw new AppError(action === 'message' ? VERIFY_BEFORE_MESSAGE : VERIFY_BEFORE_HIRE, 403);
  }
  return customerProfile;
}
