import { NextResponse } from 'next/server'; import { prisma } from '@/lib/prisma'; export async function GET(req: Request, { params }: { params: { id: string } }) { try { const { id } = params; const order = await prisma.order.findUnique({ where: { id }, include: { items: true }, }); if (!order) { return NextResponse.json({ error: 'Order not found' }, { status: 404 }); } const item = order.items[0]; if (!item) { return NextResponse.json({ error: 'No items in order' }, { status: 400 }); } const design = JSON.parse(item.designJson) as { front: any[]; back: any[] }; const product = await prisma.product.findUnique({ where: { id: item.productId }, }); if (!product) { return NextResponse.json({ error: 'Product not found' }, { status: 404 }); } // Generate HTML/CSS for a printable spec sheet const printAreaWidthMm = product.printAreaWidthMm || 200; const printAreaHeightMm = product.printAreaHeightMm || 280; // Calculate scale factor: pixels to millimeters const DISPLAY = 560; // Same as in DesignCanvas const printArea = JSON.parse(product.printArea) as { xPct: number; yPct: number; wPct: number; hPct: number; }; const printAreaWidthPx = DISPLAY * (printArea.wPct / 100); const printAreaHeightPx = DISPLAY * (printArea.hPct / 100); // Scale: how many pixels per millimeter const pxPerMm = printAreaWidthPx / printAreaWidthMm; // Convert pixel measurements to CM (divide by pxPerMm to get mm, then divide by 10 for cm) const convertToCm = (px: number) => { const mm = px / pxPerMm; return (mm / 10).toFixed(1); }; const printAreaStartX = DISPLAY * (printArea.xPct / 100); const printAreaStartY = DISPLAY * (printArea.yPct / 100); const generateDesignSection = (designElements: any[], side: 'Front' | 'Back', placement: string | null, designPreview: string | null) => { if (!designElements || designElements.length === 0) return ''; return `

${side} Design

${placement ? `

Design on product (reference only) — with measurement guides

${side} design placement ${designElements.map((el, idx) => ` E${idx + 1} `).join('')}

━━ Red dashed: Print area boundary

━ Blue: X-axis (0cm at left)

━ Green: Y-axis (0cm at top)

● Orange circles: Element positions with labels (E1, E2, etc.)

` : ''} ${designElements.map((el, idx) => ` `).join('')}
Element Type Position (CM) Size (CM) Details
E${idx + 1} ${el.type === 'text' ? '📝 Text' : '🖼️ Image'} X: ${convertToCm(el.x)} | Y: ${convertToCm(el.y)} ${el.type === 'image' ? `${convertToCm(el.width)} × ${convertToCm(el.height)}` : 'N/A'} ${el.type === 'text' ? ` Font: ${el.fontFamily}, ${Math.round(el.fontSize / pxPerMm * 10 / 10)}pt
Color: ${el.fill}
Text: "${el.text}"
Rotation: ${Math.round(el.rotation)}° ` : ` Rotation: ${Math.round(el.rotation)}° `}
`; }; const html = ` Design Specification - Order ${order.id}

Design Specification Sheet

Order ID: ${order.id}

Product: ${item.productName}

Color: ${item.color} ${item.size ? `| Size: ${item.size}` : ''}

Order Date: ${new Date(order.createdAt).toLocaleDateString()}

${generateDesignSection(design.front, 'Front', item.placementPreviewUrl, item.designPreviewUrl)} ${generateDesignSection(design.back, 'Back', item.placementPreviewUrlBack, item.designPreviewUrlBack)}

Notes:

`; return new NextResponse(html, { headers: { 'Content-Type': 'text/html; charset=utf-8', 'Content-Disposition': `attachment; filename="design-spec-${order.id}.html"`, }, }); } catch (err) { console.error('Error generating design spec:', err); return NextResponse.json({ error: 'Failed to generate specification' }, { status: 500 }); } }