import { jsPDF } from 'jspdf';

interface CertificateData {
  studentName: string;
  eventName: string;
  category: string;
  date: string;
  department: string;
  collegeName: string;
}

export function generateCertificatePdf(data: CertificateData) {
  // Create a landscape PDF (A4)
  const doc = new jsPDF({
    orientation: 'landscape',
    unit: 'mm',
    format: 'a4'
  });

  const width = doc.internal.pageSize.getWidth(); // ~297mm
  const height = doc.internal.pageSize.getHeight(); // ~210mm

  // 1. Sleek Dark Navy Background Border
  doc.setDrawColor(30, 41, 59); // Slate-800
  doc.setLineWidth(4);
  doc.rect(8, 8, width - 16, height - 16);

  // 2. Innermost Gold Accent Border
  doc.setDrawColor(249, 115, 22); // Orange-500/Amber gold
  doc.setLineWidth(1.5);
  doc.rect(12, 12, width - 24, height - 24);

  // 3. Corner Ornamental Accents
  const corners = [
    { x: 12, y: 12 },
    { x: width - 12, y: 12 },
    { x: 12, y: height - 12 },
    { x: width - 12, y: height - 12 }
  ];
  corners.forEach(c => {
    doc.setFillColor(249, 115, 22);
    doc.circle(c.x, c.y, 3, 'F');
  });

  // 4. Header Section: College Branding
  doc.setFont('Helvetica', 'bold');
  doc.setFontSize(22);
  doc.setTextColor(30, 41, 59); // Slate-800
  doc.text('RAJALAKSHMI INSTITUTE OF TECHNOLOGY', width / 2, 35, { align: 'center' });

  doc.setFont('Helvetica', 'normal');
  doc.setFontSize(9);
  doc.setTextColor(100, 116, 139); // Slate-500
  doc.text('Accredited by NBA & NAAC, Approved by AICTE, Affiliated to Anna University', width / 2, 41, { align: 'center' });
  doc.text('Kuthambakkam, Chennai - 600124', width / 2, 46, { align: 'center' });

  // 5. Divider Line
  doc.setDrawColor(226, 232, 240); // Slate-200
  doc.setLineWidth(0.5);
  doc.line(40, 52, width - 40, 52);

  // 6. Certificate Title
  doc.setFont('Helvetica', 'bold');
  doc.setFontSize(24);
  doc.setTextColor(249, 115, 22); // Brand Orange
  doc.text('CERTIFICATE OF PARTICIPATION', width / 2, 68, { align: 'center' });

  // 7. Statement Text
  doc.setFont('Helvetica', 'normal');
  doc.setFontSize(14);
  doc.setTextColor(71, 85, 105); // Slate-600
  doc.text('This is proudly presented to', width / 2, 85, { align: 'center' });

  // 8. Student Name
  doc.setFont('Helvetica', 'bold');
  doc.setFontSize(28);
  doc.setTextColor(15, 23, 42); // Slate-900
  doc.text(data.studentName.toUpperCase(), width / 2, 102, { align: 'center' });

  // Underline under the name
  doc.setDrawColor(249, 115, 22);
  doc.setLineWidth(1);
  doc.line(width / 2 - 60, 106, width / 2 + 60, 106);

  // 9. Event Details
  doc.setFont('Helvetica', 'normal');
  doc.setFontSize(13);
  doc.setTextColor(71, 85, 105);
  doc.text(
    `of ${data.collegeName} for active participation in the event`,
    width / 2,
    118,
    { align: 'center' }
  );

  doc.setFont('Helvetica', 'bold');
  doc.setFontSize(18);
  doc.setTextColor(30, 41, 59);
  doc.text(`"${data.eventName.toUpperCase()}"`, width / 2, 130, { align: 'center' });

  // 10. Metadata (Date & Category)
  doc.setFont('Helvetica', 'normal');
  doc.setFontSize(12);
  doc.setTextColor(100, 116, 139);
  
  const formattedDate = new Date(data.date).toLocaleDateString(undefined, {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  });
  
  doc.text(
    `held on ${formattedDate} as a part of our ${data.category.toUpperCase()} activities`,
    width / 2,
    140,
    { align: 'center' }
  );

  doc.text(
    `Organized by the Department / Club of ${data.department}`,
    width / 2,
    148,
    { align: 'center' }
  );

  // 11. Signatures
  // Left Signature: Coordinator
  doc.setFont('Helvetica', 'normal');
  doc.setFontSize(11);
  doc.setTextColor(30, 41, 59);
  doc.line(45, 180, 105, 180);
  // Fake signature graphic text
  doc.setFont('Courier', 'italic');
  doc.setTextColor(249, 115, 22);
  doc.text('Event Coordinator', 75, 175, { align: 'center' });
  doc.setFont('Helvetica', 'bold');
  doc.setTextColor(30, 41, 59);
  doc.text('CONVENOR', 75, 186, { align: 'center' });

  // Right Signature: Principal
  doc.setFont('Helvetica', 'normal');
  doc.line(width - 105, 180, width - 45, 180);
  doc.setFont('Courier', 'italic');
  doc.setTextColor(249, 115, 22);
  doc.text('Dr. College Principal', width - 75, 175, { align: 'center' });
  doc.setFont('Helvetica', 'bold');
  doc.setTextColor(30, 41, 59);
  doc.text('PRINCIPAL, RIT', width - 75, 186, { align: 'center' });

  // 12. Save PDF
  const safeTitle = data.eventName.replace(/[^a-z0-9]/gi, '_').toLowerCase();
  doc.save(`certificate_${safeTitle}.pdf`);
}
