export function extractDomain(fromAddr) {
  const m = String(fromAddr || '').match(/@([A-Za-z0-9.-]+)/);
  return m ? m[1].toLowerCase() : '';
}

export function extractFirstJobNumber(text) {
  const t = String(text || '');
  const m = t.match(/\b\d{8,}\b/);
  return m ? m[0] : '';
}

export function validatePassword(pw) {
  const p = String(pw || '');
  if (p.length < 12) return 'Password must be at least 12 characters.';
  if (!/[A-Z]/.test(p)) return 'Password must include an uppercase letter.';
  if (!/[a-z]/.test(p)) return 'Password must include a lowercase letter.';
  if (!/\d/.test(p)) return 'Password must include a number.';
  return null;
}

export function buildPCOBody(email, project) {
  return [
    '# Potential Change Order (PCO) - Draft',
    '',
    `**Project:** ${project?.name || ''}`,
    `**Job #:** ${project?.job_number || ''}`,
    `**Source email:** ${email?.subject || ''} (${email?.id || ''})`,
    `**From:** ${email?.from_addr || ''}`,
    `**Date:** ${email?.date || ''}`,
    '',
    '## Description / Change event',
    '- (Describe what changed and why)',
    '',
    '## Contract / drawing references',
    '- (Sheet/spec refs)',
    '',
    '## Cost / schedule impact',
    '- Labor:',
    '- Material:',
    '- Equipment:',
    '- Schedule impact:',
    '',
    '## Supporting info',
    '- Email excerpt:',
    '',
    `> ${String(email?.body_text || '').slice(0, 1200).replace(/\n/g, '\n> ')}`
  ].join('\n');
}

export function buildRFIBody(email, project) {
  return [
    '# RFI - Draft',
    '',
    `**Project:** ${project?.name || ''}`,
    `**Job #:** ${project?.job_number || ''}`,
    `**Source email:** ${email?.subject || ''} (${email?.id || ''})`,
    `**From:** ${email?.from_addr || ''}`,
    `**Date:** ${email?.date || ''}`,
    '',
    '## Question',
    '- (Write the question clearly)',
    '',
    '## Background',
    '- (Why this is needed / conflict)',
    '',
    '## Drawing/spec references',
    '- (Sheet/detail/spec section)',
    '',
    '## Proposed resolution (optional)',
    '-',
    '',
    '## Supporting excerpt',
    `> ${String(email?.body_text || '').slice(0, 1200).replace(/\n/g, '\n> ')}`
  ].join('\n');
}
