import { NextRequest, NextResponse } from "next/server"
import nodemailer from "nodemailer"
import { formatPrice, type CalculationDetails } from "@/lib/calculations"
import { FLOORING_TYPES } from "@/lib/constants"
import { saveQuote } from "@/lib/db"
interface QuoteRequestBody {
customerInfo: {
name: string
email: string
phone: string
postalCode: string
address?: string
remarks?: string
}
calculationDetails: CalculationDetails
}
function createTransporter() {
return nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || "587"),
secure: process.env.SMTP_PORT === "465",
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
})
}
function getFlooringTypeName(type: string): string {
return FLOORING_TYPES[type as keyof typeof FLOORING_TYPES]?.name || type
}
function formatCustomerEmail(
customer: QuoteRequestBody["customerInfo"],
details: CalculationDetails,
trackingUrl: string
): string {
const components = []
if (details.includeInsulation) components.push(`Isolering (${details.insulationThickness} cm)`)
if (details.includeFloorHeating) components.push("Gulvvarme syntetisk net + Ø16 PEX (excl. tilslutning)")
if (details.includeCompound)
components.push(`Flydespartel (${getFlooringTypeName(details.flooringType)})`)
return `
Kære ${customer.name},
Tak for din interesse i Foam King Gulve. Her er dit prisoverslag baseret på de oplysninger du har indtastet:
${formatPrice(Math.round(details.totalInclVat))}
inkl. moms
Projektdetaljer
- Areal: ${details.area} m²
- Gulvhøjde: ${details.height} cm
- Placering: ${customer.postalCode}${customer.address ? `, ${customer.address}` : ""}
Inkluderet i prisen
${components.map((c) => `- ${c}
`).join("")}
- Transport
- Startgebyr (udstyr og sikkerhed)
Bemærk: Dette er et vejledende prisoverslag og kan variere med ±10.000 kr afhængigt af konkrete forhold på stedet.
Vi kontakter dig snarest for at aftale et uforpligtende besøg, hvor vi kan give dig et præcist og bindende tilbud.
Har du spørgsmål i mellemtiden, er du velkommen til at kontakte os.
Med venlig hilsen,
Foam King ApS
Tlf: 35 90 10 66
Email: info@foamking.dk
`
}
function formatFoamKingEmail(
customer: QuoteRequestBody["customerInfo"],
details: CalculationDetails,
quoteLink: string,
quoteId: number
): string {
const components = []
if (details.includeInsulation)
components.push(
`Isolering: ${details.insulationThickness} cm (${formatPrice(Math.round(details.insulation))})`
)
if (details.includeFloorHeating) {
components.push(`Gulvvarme: ${formatPrice(Math.round(details.floorHeating))}`)
components.push(`Syntetisk net: ${formatPrice(Math.round(details.syntheticNet))}`)
}
if (details.includeCompound) {
components.push(
`Flydespartel (${getFlooringTypeName(details.flooringType)}): ${formatPrice(Math.round(details.selfLevelingCompound))}`
)
components.push(
`Pumpebil (${details.compoundWeight} kg): ${formatPrice(Math.round(details.pumpTruckFee))}`
)
}
return `