import { PDFDocument, rgb, degrees } from 'pdf-lib';
import { readFileSync } from 'fs';
import { join } from 'path';

export interface CertificateData {
  adminName: string;
  certificationName: string;
  certificationNumber: string;
  issuedDate: Date;
  expiresDate?: Date;
  badgeUrl?: string;
  organizationName?: string;
  verificationUrl?: string;
}

/**
 * Generate a professional PDF certificate
 */
export async function generateCertificate(data: CertificateData): Promise<Buffer> {
  const pdfDoc = await PDFDocument.create();
  const page = pdfDoc.addPage([850, 1100]); // 11x8.5 inches

  const { width, height } = page.getSize();

  // Background color
  page.drawRectangle({
    x: 0,
    y: 0,
    width,
    height,
    color: rgb(1, 0.95, 0.9), // Light cream
  });

  // Decorative border
  page.drawRectangle({
    x: 30,
    y: 30,
    width: width - 60,
    height: height - 60,
    borderColor: rgb(0.8, 0.6, 0.2), // Gold
    borderWidth: 3,
  });

  // Inner decorative border
  page.drawRectangle({
    x: 50,
    y: 50,
    width: width - 100,
    height: height - 100,
    borderColor: rgb(0.9, 0.8, 0.6), // Light gold
    borderWidth: 1,
  });

  // Title
  page.drawText('CERTIFICATE OF ACHIEVEMENT', {
    x: width / 2,
    y: height - 150,
    size: 48,
    color: rgb(0.2, 0.3, 0.6), // Dark blue
    align: 'center',
    maxWidth: width - 100,
  });

  // Subtitle
  page.drawText('Professional Certification', {
    x: width / 2,
    y: height - 200,
    size: 20,
    color: rgb(0.5, 0.5, 0.5), // Gray
    align: 'center',
    maxWidth: width - 100,
  });

  // Recipient name
  page.drawText('This certifies that', {
    x: width / 2,
    y: height - 280,
    size: 16,
    color: rgb(0.3, 0.3, 0.3),
    align: 'center',
  });

  page.drawText(data.adminName, {
    x: width / 2,
    y: height - 340,
    size: 36,
    color: rgb(0.2, 0.3, 0.6), // Dark blue
    align: 'center',
    maxWidth: width - 100,
  });

  // Achievement text
  page.drawText('has successfully completed the', {
    x: width / 2,
    y: height - 400,
    size: 16,
    color: rgb(0.3, 0.3, 0.3),
    align: 'center',
  });

  page.drawText(data.certificationName, {
    x: width / 2,
    y: height - 460,
    size: 28,
    color: rgb(0.8, 0.6, 0.2), // Gold
    align: 'center',
    maxWidth: width - 100,
  });

  // Certification details
  const detailsY = height - 540;
  const leftX = 150;
  const rightX = width / 2 + 50;

  page.drawText('Certification Number:', {
    x: leftX,
    y: detailsY,
    size: 12,
    color: rgb(0.5, 0.5, 0.5),
  });

  page.drawText(data.certificationNumber, {
    x: leftX,
    y: detailsY - 25,
    size: 14,
    color: rgb(0.2, 0.3, 0.6),
  });

  page.drawText('Issued Date:', {
    x: rightX,
    y: detailsY,
    size: 12,
    color: rgb(0.5, 0.5, 0.5),
  });

  page.drawText(data.issuedDate.toLocaleDateString(), {
    x: rightX,
    y: detailsY - 25,
    size: 14,
    color: rgb(0.2, 0.3, 0.6),
  });

  // Expiration date if applicable
  if (data.expiresDate) {
    page.drawText('Valid Until:', {
      x: leftX,
      y: detailsY - 80,
      size: 12,
      color: rgb(0.5, 0.5, 0.5),
    });

    page.drawText(data.expiresDate.toLocaleDateString(), {
      x: leftX,
      y: detailsY - 105,
      size: 14,
      color: rgb(0.2, 0.3, 0.6),
    });
  }

  // Organization name
  if (data.organizationName) {
    page.drawText(data.organizationName, {
      x: width / 2,
      y: 200,
      size: 16,
      color: rgb(0.3, 0.3, 0.3),
      align: 'center',
    });
  }

  // Verification URL
  if (data.verificationUrl) {
    page.drawText('Verify this certificate:', {
      x: width / 2,
      y: 140,
      size: 10,
      color: rgb(0.5, 0.5, 0.5),
      align: 'center',
    });

    page.drawText(data.verificationUrl, {
      x: width / 2,
      y: 120,
      size: 9,
      color: rgb(0, 0, 1), // Blue for link
      align: 'center',
      maxWidth: width - 100,
    });
  }

  // Signature lines
  const signatureY = 300;
  const signatureLineLength = 120;

  page.drawLine({
    start: { x: leftX, y: signatureY },
    end: { x: leftX + signatureLineLength, y: signatureY },
    color: rgb(0.2, 0.2, 0.2),
    thickness: 1,
  });

  page.drawText('Authorized Signature', {
    x: leftX,
    y: signatureY - 30,
    size: 10,
    color: rgb(0.5, 0.5, 0.5),
  });

  page.drawLine({
    start: { x: rightX, y: signatureY },
    end: { x: rightX + signatureLineLength, y: signatureY },
    color: rgb(0.2, 0.2, 0.2),
    thickness: 1,
  });

  page.drawText('Date', {
    x: rightX,
    y: signatureY - 30,
    size: 10,
    color: rgb(0.5, 0.5, 0.5),
  });

  // Generate PDF
  const pdfBytes = await pdfDoc.save();
  return Buffer.from(pdfBytes);
}

/**
 * Generate certificate filename
 */
export function generateCertificateFilename(certNumber: string): string {
  return `certificate-${certNumber}-${Date.now()}.pdf`;
}

/**
 * Verify certificate authenticity (placeholder)
 */
export async function verifyCertificate(certNumber: string): Promise<boolean> {
  // TODO: Implement certificate verification logic
  // This would check the certificate number against the database
  return true;
}
