import { notifyUser } from './notification.service';

function docLabel(type: string) {
  return type.replace(/_/g, ' ').toLowerCase();
}

export async function notifyDocumentReview(opts: {
  userId: string;
  role: 'WORKER' | 'CUSTOMER';
  docType: string;
  status: 'APPROVED' | 'REJECTED' | 'ADDITIONAL_DOCS_REQUIRED';
  adminNote?: string | null;
  fullyVerified?: boolean;
  documentId: string;
}) {
  const portalPath =
    opts.role === 'WORKER' ? '/worker/verification' : '/customer/verification';
  const note = opts.adminNote?.trim()
    ? `\n\nAdmin note: ${opts.adminNote.trim()}`
    : '';

  if (opts.status === 'APPROVED' && opts.fullyVerified) {
    const title =
      opts.role === 'WORKER' ? 'Your worker account is verified' : 'Your customer account is verified';
    const message =
      opts.role === 'WORKER'
        ? `Your ${docLabel(opts.docType)} was approved and your identity verification is complete. Your public profile can now appear in search and on the map.`
        : `Your ${docLabel(opts.docType)} was approved and your identity verification is complete. You can now hire and message workers.`;
    return notifyUser({
      userId: opts.userId,
      type: 'VERIFICATION_UPDATE',
      title,
      message: `${message}${note}`,
      data: {
        documentId: opts.documentId,
        status: opts.status,
        fullyVerified: true,
        href: portalPath,
      },
      emailSubject: title,
      ctaLabel: 'Open verification',
      ctaPath: portalPath,
    });
  }

  if (opts.status === 'APPROVED') {
    const title = 'Document approved';
    const message = `Your ${docLabel(opts.docType)} has been approved. Complete any remaining verification steps (phone + required documents) to finish verification.${note}`;
    return notifyUser({
      userId: opts.userId,
      type: 'VERIFICATION_UPDATE',
      title,
      message,
      data: { documentId: opts.documentId, status: opts.status, href: portalPath },
      emailSubject: `eHandyHire: ${title}`,
      ctaLabel: 'Open verification',
      ctaPath: portalPath,
    });
  }

  if (opts.status === 'ADDITIONAL_DOCS_REQUIRED') {
    const title = 'More documents needed';
    const message = `We need additional documents for your ${docLabel(opts.docType)}. Please upload clearer or additional files from your verification page.${note}`;
    return notifyUser({
      userId: opts.userId,
      type: 'VERIFICATION_UPDATE',
      title,
      message,
      data: { documentId: opts.documentId, status: opts.status, href: portalPath },
      emailSubject: `eHandyHire: ${title}`,
      ctaLabel: 'Upload documents',
      ctaPath: portalPath,
    });
  }

  const title = 'Document rejected';
  const message = `Your ${docLabel(opts.docType)} was rejected. Please upload a new document from your verification page.${note}`;
  return notifyUser({
    userId: opts.userId,
    type: 'VERIFICATION_UPDATE',
    title,
    message,
    data: { documentId: opts.documentId, status: opts.status, href: portalPath },
    emailSubject: `eHandyHire: ${title}`,
    ctaLabel: 'Open verification',
    ctaPath: portalPath,
  });
}

export async function notifyManualVerification(opts: {
  userId: string;
  role: 'WORKER' | 'CUSTOMER';
  isVerified: boolean;
}) {
  const portalPath =
    opts.role === 'WORKER' ? '/worker/verification' : '/customer/verification';
  const title = opts.isVerified ? 'Account verified by admin' : 'Verification removed';
  const message = opts.isVerified
    ? opts.role === 'WORKER'
      ? 'An admin marked your worker account as verified. Your public profile can appear in search and on the map.'
      : 'An admin marked your customer account as verified. You can hire and message workers.'
    : 'An admin removed your verified status. Please check your verification page for next steps.';

  return notifyUser({
    userId: opts.userId,
    type: 'VERIFICATION_UPDATE',
    title,
    message,
    data: { isVerified: opts.isVerified, href: portalPath },
    emailSubject: `eHandyHire: ${title}`,
    ctaLabel: 'Open verification',
    ctaPath: portalPath,
  });
}

export async function notifyAvailabilityChange(opts: {
  userId: string;
  isAvailable: boolean;
}) {
  const title = opts.isAvailable ? 'You are marked available' : 'You are marked unavailable';
  const message = opts.isAvailable
    ? 'An admin marked your worker profile as available. Customers can see you as open for work.'
    : 'An admin marked your worker profile as unavailable. Update this anytime from your profile.';

  return notifyUser({
    userId: opts.userId,
    type: 'SYSTEM',
    title,
    message,
    data: { isAvailable: opts.isAvailable, href: '/worker/profile' },
    emailSubject: `eHandyHire: ${title}`,
    ctaLabel: 'Open profile',
    ctaPath: '/worker/profile',
  });
}

export async function notifyAccountStatusChange(opts: {
  userId: string;
  status: 'ACTIVE' | 'SUSPENDED';
  role: string;
}) {
  const title = opts.status === 'ACTIVE' ? 'Account reactivated' : 'Account suspended';
  const message =
    opts.status === 'ACTIVE'
      ? 'Your eHandyHire account has been reactivated. You can sign in and use the platform again.'
      : 'Your eHandyHire account has been suspended by an admin. Contact support if you believe this was a mistake.';
  const href =
    opts.role === 'WORKER'
      ? '/worker/dashboard'
      : opts.role === 'CUSTOMER'
        ? '/customer/dashboard'
        : '/';

  return notifyUser({
    userId: opts.userId,
    type: 'SYSTEM',
    title,
    message,
    data: { status: opts.status, href },
    emailSubject: `eHandyHire: ${title}`,
    ctaLabel: opts.status === 'ACTIVE' ? 'Open dashboard' : undefined,
    ctaPath: opts.status === 'ACTIVE' ? href : undefined,
  });
}
