"use server"

import { jsPDF } from "jspdf"

export interface QuoteData {
  id: string
  fullName: string
  company: string
  phone: string
  email: string
  projectType: string
  service: string
  location: string
  description: string
  duration: string
  notes: string
  submissionDate: string
  submissionTime: string
}

export async function generateQuotePDF(data: QuoteData): Promise<Buffer> {
  const doc = new jsPDF({
    orientation: "portrait",
    unit: "mm",
    format: "a4",
  })

  const pageWidth = doc.internal.pageSize.getWidth()
  const margin = 20
  const contentWidth = pageWidth - margin * 2
  let yPos = 20

  // Colors
  const primaryColor: [number, number, number] = [249, 115, 22] // Orange
  const darkColor: [number, number, number] = [23, 23, 23] // Dark
  const grayColor: [number, number, number] = [115, 115, 115] // Gray
  const lightGray: [number, number, number] = [245, 245, 245] // Light gray bg

  // Header Background
  doc.setFillColor(...darkColor)
  doc.rect(0, 0, pageWidth, 50, "F")

  // Company Name
  doc.setTextColor(255, 255, 255)
  doc.setFontSize(28)
  doc.setFont("helvetica", "bold")
  doc.text("SES", margin, 28)
  
  doc.setTextColor(...primaryColor)
  doc.text("ENDÜSTRİ", margin + 28, 28)

  // Tagline
  doc.setTextColor(200, 200, 200)
  doc.setFontSize(10)
  doc.setFont("helvetica", "normal")
  doc.text("Endüstriyel Mekanik Montaj ve Borulama Sistemleri", margin, 38)

  // Contact info on right
  doc.setTextColor(255, 255, 255)
  doc.setFontSize(9)
  doc.text("+90 530 130 56 46", pageWidth - margin, 22, { align: "right" })
  doc.text("+90 538 400 61 89", pageWidth - margin, 28, { align: "right" })
  doc.text("info@sesendustri.com", pageWidth - margin, 34, { align: "right" })
  doc.text("Kocaeli, Türkiye", pageWidth - margin, 40, { align: "right" })

  yPos = 60

  // Document Title
  doc.setTextColor(...darkColor)
  doc.setFontSize(20)
  doc.setFont("helvetica", "bold")
  doc.text("TEKLİF TALEP FORMU", pageWidth / 2, yPos, { align: "center" })

  yPos += 8
  
  // Quote ID and Date
  doc.setFontSize(10)
  doc.setTextColor(...grayColor)
  doc.setFont("helvetica", "normal")
  doc.text(`Talep No: ${data.id}`, pageWidth / 2, yPos, { align: "center" })
  
  yPos += 5
  doc.text(`Tarih: ${data.submissionDate} - ${data.submissionTime}`, pageWidth / 2, yPos, { align: "center" })

  yPos += 15

  // Section: Customer Information
  const drawSection = (title: string, startY: number): number => {
    doc.setFillColor(...primaryColor)
    doc.rect(margin, startY, 4, 8, "F")
    doc.setTextColor(...darkColor)
    doc.setFontSize(12)
    doc.setFont("helvetica", "bold")
    doc.text(title, margin + 8, startY + 6)
    return startY + 15
  }

  const drawField = (label: string, value: string, startY: number, isFullWidth = false): number => {
    doc.setFillColor(...lightGray)
    const fieldWidth = isFullWidth ? contentWidth : contentWidth / 2 - 5
    doc.roundedRect(margin, startY, fieldWidth, 16, 2, 2, "F")
    
    doc.setTextColor(...grayColor)
    doc.setFontSize(8)
    doc.setFont("helvetica", "normal")
    doc.text(label, margin + 4, startY + 5)
    
    doc.setTextColor(...darkColor)
    doc.setFontSize(10)
    doc.setFont("helvetica", "bold")
    const displayValue = value || "-"
    doc.text(displayValue.substring(0, 50), margin + 4, startY + 12)
    
    return startY + 20
  }

  const drawFieldPair = (label1: string, value1: string, label2: string, value2: string, startY: number): number => {
    const fieldWidth = contentWidth / 2 - 5
    
    // Left field
    doc.setFillColor(...lightGray)
    doc.roundedRect(margin, startY, fieldWidth, 16, 2, 2, "F")
    doc.setTextColor(...grayColor)
    doc.setFontSize(8)
    doc.setFont("helvetica", "normal")
    doc.text(label1, margin + 4, startY + 5)
    doc.setTextColor(...darkColor)
    doc.setFontSize(10)
    doc.setFont("helvetica", "bold")
    doc.text((value1 || "-").substring(0, 25), margin + 4, startY + 12)
    
    // Right field
    const rightX = margin + fieldWidth + 10
    doc.setFillColor(...lightGray)
    doc.roundedRect(rightX, startY, fieldWidth, 16, 2, 2, "F")
    doc.setTextColor(...grayColor)
    doc.setFontSize(8)
    doc.setFont("helvetica", "normal")
    doc.text(label2, rightX + 4, startY + 5)
    doc.setTextColor(...darkColor)
    doc.setFontSize(10)
    doc.setFont("helvetica", "bold")
    doc.text((value2 || "-").substring(0, 25), rightX + 4, startY + 12)
    
    return startY + 20
  }

  const drawTextArea = (label: string, value: string, startY: number, height: number): number => {
    doc.setFillColor(...lightGray)
    doc.roundedRect(margin, startY, contentWidth, height, 2, 2, "F")
    
    doc.setTextColor(...grayColor)
    doc.setFontSize(8)
    doc.setFont("helvetica", "normal")
    doc.text(label, margin + 4, startY + 5)
    
    doc.setTextColor(...darkColor)
    doc.setFontSize(10)
    doc.setFont("helvetica", "normal")
    
    const displayValue = value || "-"
    const lines = doc.splitTextToSize(displayValue, contentWidth - 8)
    doc.text(lines.slice(0, Math.floor((height - 10) / 5)), margin + 4, startY + 12)
    
    return startY + height + 4
  }

  // Customer Information Section
  yPos = drawSection("MÜŞTERİ BİLGİLERİ", yPos)
  yPos = drawFieldPair("Ad Soyad", data.fullName, "Firma", data.company, yPos)
  yPos = drawFieldPair("Telefon", data.phone, "E-Posta", data.email, yPos)

  // Project Details Section
  yPos = drawSection("PROJE DETAYLARI", yPos)
  yPos = drawFieldPair("Proje Türü", data.projectType, "Hizmet Türü", data.service, yPos)
  yPos = drawFieldPair("Lokasyon", data.location, "Tahmini Süre", data.duration, yPos)
  
  // Description
  yPos = drawTextArea("Proje Açıklaması", data.description, yPos, 35)
  
  // Notes
  yPos = drawTextArea("Ek Notlar", data.notes, yPos, 25)

  // Footer
  const footerY = doc.internal.pageSize.getHeight() - 25
  
  // Footer line
  doc.setDrawColor(...primaryColor)
  doc.setLineWidth(0.5)
  doc.line(margin, footerY, pageWidth - margin, footerY)
  
  // Footer text
  doc.setTextColor(...grayColor)
  doc.setFontSize(8)
  doc.setFont("helvetica", "normal")
  doc.text("Bu belge SES Endüstri web sitesi üzerinden otomatik olarak oluşturulmuştur.", pageWidth / 2, footerY + 6, { align: "center" })
  doc.text("www.sesendustri.com | info@sesendustri.com | +90 530 130 56 46", pageWidth / 2, footerY + 11, { align: "center" })
  
  // Confidentiality notice
  doc.setTextColor(...primaryColor)
  doc.setFontSize(7)
  doc.text("GİZLİ - Sadece yetkili personel içindir", pageWidth / 2, footerY + 18, { align: "center" })

  // Return as buffer
  const pdfOutput = doc.output("arraybuffer")
  return Buffer.from(pdfOutput)
}
