import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';

// RateRight brand colours
const BRAND_ORANGE = rgb(0.878, 0.42, 0.125); // #E06B20
const BRAND_ORANGE_LIGHT = rgb(1, 0.957, 0.929); // #FFF4ED
const BRAND_DARK = rgb(0.102, 0.102, 0.102); // #1a1a1a
const BRAND_GRAY = rgb(0.333, 0.333, 0.333); // #555
const BRAND_LIGHT_GRAY = rgb(0.953, 0.957, 0.965); // #f3f4f6
const WHITE = rgb(1, 1, 1);

export interface WorkerProfileData {
  name: string;
  phone: string;
  email: string;
  address?: string;
  suburb?: string;
  trade: string;
  experience: string;
  abn?: string;
  bankName: string;
  bsb: string;
  accountNumber: string;
  emergencyContactName: string;
  emergencyContactPhone: string;
  emergencyContactRelationship: string;
}

export async function generateOnboardingPackPDF(
  data: WorkerProfileData
): Promise<Uint8Array> {
  const pdfDoc = await PDFDocument.create();
  const page = pdfDoc.addPage([595, 842]); // A4

  const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica);
  const helveticaBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold);

  const margin = 50;
  const contentWidth = 595 - margin * 2;
  let y = 810;

  // === Orange accent bar at top ===
  page.drawRectangle({
    x: 0,
    y: 838,
    width: 595,
    height: 4,
    color: BRAND_ORANGE,
  });

  // === Header ===
  // Title
  page.drawText('WORKER ONBOARDING PACK', {
    x: margin,
    y,
    font: helveticaBold,
    size: 20,
    color: BRAND_DARK,
  });

  // Brand logo text (right side)
  page.drawText('RATERIGHT', {
    x: 595 - margin - helveticaBold.widthOfTextAtSize('RATERIGHT', 14),
    y: y + 2,
    font: helveticaBold,
    size: 14,
    color: BRAND_ORANGE,
  });

  y -= 18;

  page.drawText('Construction Hiring Made Simple', {
    x:
      595 -
      margin -
      helvetica.widthOfTextAtSize('Construction Hiring Made Simple', 7),
    y: y + 2,
    font: helvetica,
    size: 7,
    color: BRAND_GRAY,
  });

  // Worker name and date
  const today = new Date().toLocaleDateString('en-AU', {
    day: 'numeric',
    month: 'long',
    year: 'numeric',
  });

  page.drawText(`${data.name} — ${today}`, {
    x: margin,
    y,
    font: helveticaBold,
    size: 13,
    color: BRAND_GRAY,
  });

  y -= 30;

  // === Helper: Section Header (orange background) ===
  const drawSectionHeader = (title: string, yPos: number): number => {
    page.drawRectangle({
      x: margin - 4,
      y: yPos - 4,
      width: contentWidth + 8,
      height: 22,
      color: BRAND_ORANGE,
    });
    page.drawText(title, {
      x: margin + 8,
      y: yPos + 2,
      font: helveticaBold,
      size: 11,
      color: WHITE,
    });
    return yPos - 32;
  };

  // === Helper: Field Row ===
  const drawField = (label: string, value: string, yPos: number): number => {
    page.drawText(label, {
      x: margin + 12,
      y: yPos,
      font: helveticaBold,
      size: 9,
      color: BRAND_GRAY,
    });
    page.drawText(value || 'Not provided', {
      x: margin + 145,
      y: yPos,
      font: helvetica,
      size: 10,
      color: value ? BRAND_DARK : rgb(0.6, 0.6, 0.6),
    });
    return yPos - 18;
  };

  // === 1. Personal Details ===
  y = drawSectionHeader('1. PERSONAL DETAILS', y);
  y = drawField('Full Name', data.name, y);
  y = drawField('Phone', data.phone, y);
  y = drawField('Email', data.email, y);
  y = drawField(
    'Address',
    data.address || data.suburb || 'Not provided',
    y
  );
  y -= 8;

  // === 2. Trade Information ===
  y = drawSectionHeader('2. TRADE INFORMATION', y);
  y = drawField('Trade / Specialisation', data.trade, y);
  y = drawField('Years of Experience', data.experience, y);
  if (data.abn) {
    y = drawField('ABN', data.abn, y);
  }
  y -= 8;

  // === 3. Payment Details ===
  y = drawSectionHeader('3. PAYMENT DETAILS (ABN Path)', y);
  y = drawField('Bank Name', data.bankName, y);
  y = drawField('BSB', data.bsb, y);
  y = drawField('Account Number', data.accountNumber, y);
  y -= 8;

  // === 4. Emergency Contact ===
  y = drawSectionHeader('4. EMERGENCY CONTACT', y);
  y = drawField('Name', data.emergencyContactName, y);
  y = drawField('Phone', data.emergencyContactPhone, y);
  y = drawField(
    'Relationship',
    data.emergencyContactRelationship,
    y
  );
  y -= 12;

  // === Info box ===
  page.drawRectangle({
    x: margin - 4,
    y: y - 28,
    width: contentWidth + 8,
    height: 36,
    color: BRAND_ORANGE_LIGHT,
  });
  // Orange left border
  page.drawRectangle({
    x: margin - 4,
    y: y - 28,
    width: 3,
    height: 36,
    color: BRAND_ORANGE,
  });
  page.drawText(
    'This onboarding pack was generated through RateRight. Sensitive fields are entered locally and not stored.',
    {
      x: margin + 8,
      y: y - 14,
      font: helvetica,
      size: 8,
      color: BRAND_GRAY,
      maxWidth: contentWidth - 20,
    }
  );
  y -= 48;

  // === Disclaimer ===
  page.drawRectangle({
    x: margin - 4,
    y: y - 32,
    width: contentWidth + 8,
    height: 40,
    color: BRAND_LIGHT_GRAY,
  });
  page.drawText(
    'This document was prepared by the worker named above. RateRight Pty Ltd is a matching platform',
    {
      x: margin + 4,
      y: y - 10,
      font: helvetica,
      size: 7,
      color: rgb(0.6, 0.6, 0.6),
    }
  );
  page.drawText(
    'and is not party to any employment agreement. Information accuracy is the responsibility of the worker.',
    {
      x: margin + 4,
      y: y - 22,
      font: helvetica,
      size: 7,
      color: rgb(0.6, 0.6, 0.6),
    }
  );

  // === Footer ===
  const footerY = 40;
  // Orange line
  page.drawRectangle({
    x: margin,
    y: footerY + 12,
    width: contentWidth,
    height: 2,
    color: BRAND_ORANGE,
  });
  page.drawText('RATERIGHT', {
    x: margin,
    y: footerY,
    font: helveticaBold,
    size: 9,
    color: BRAND_ORANGE,
  });
  page.drawText("Australia's $50 Construction Hiring Platform", {
    x: margin + 60,
    y: footerY,
    font: helvetica,
    size: 8,
    color: BRAND_GRAY,
  });
  page.drawText('rateright.com.au', {
    x:
      595 -
      margin -
      helvetica.widthOfTextAtSize('rateright.com.au', 8),
    y: footerY,
    font: helvetica,
    size: 8,
    color: rgb(0.145, 0.435, 0.831),
  });

  return await pdfDoc.save();
}
