import { type NextRequest, NextResponse } from "next/server"
import createMollieClient from "@mollie/api-client"

// Lazy initialization to avoid build-time errors
let mollieClient: ReturnType<typeof createMollieClient> | null = null

function getMollie() {
  if (!mollieClient) {
    mollieClient = createMollieClient({ apiKey: process.env.MOLLIE_API_KEY! })
  }
  return mollieClient
}

// Dynamic pricing configuration (in EUR)
const TICKET_PRICE = 80 // €80 early bird

// Accommodation prices per person for 7 nights
const ACCOMMODATION_PRICES: Record<string, { label: string; price: number }> = {
  "ch-multi": { label: "Bed in shared room (Commons Hub)", price: 274.40 },
  "ch-double": { label: "Bed in double room (Commons Hub)", price: 351.40 },
  "hh-single": { label: "Single — double bed (Herrnhof)", price: 95 },
  "hh-double-separate": { label: "Double — double + single bed (Herrnhof)", price: 60 },
  "hh-double-shared": { label: "Double — shared double bed (Herrnhof)", price: 50 },
  "hh-double-couch": { label: "Double — couch + single in living room (Herrnhof)", price: 40 },
  "hh-triple": { label: "Triple — shared double + single bed (Herrnhof)", price: 50 },
}

// Public base URL
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || "https://cryptocommonsgather.ing"

export async function POST(request: NextRequest) {
  try {
    const formData = await request.formData()
    const registrationDataStr = formData.get("registrationData") as string
    const includeAccommodation = formData.get("includeAccommodation") === "true"
    const accommodationType = (formData.get("accommodationType") as string) || "ch-multi"

    const registrationData = registrationDataStr ? JSON.parse(registrationDataStr) : null

    // Calculate total
    let total = TICKET_PRICE
    const descriptionParts = ["CCG 2026 Ticket (€80)"]

    if (includeAccommodation) {
      const accom = ACCOMMODATION_PRICES[accommodationType]
      if (accom) {
        total += accom.price
        descriptionParts.push(`${accom.label} (€${accom.price.toFixed(2)})`)
      }
    }

    // Build metadata for webhook
    const metadata: Record<string, string> = {}
    if (registrationData) {
      metadata.name = registrationData.name || ""
      metadata.contact = registrationData.contact || ""
      metadata.contributions = (registrationData.contributions || "").substring(0, 500)
      metadata.expectations = (registrationData.expectations || "").substring(0, 500)
      metadata.howHeard = registrationData.howHeard || ""
      metadata.dietary =
        (registrationData.dietary || []).join(", ") +
        (registrationData.dietaryOther ? `, ${registrationData.dietaryOther}` : "")
      metadata.crewConsent = registrationData.crewConsent || ""
      metadata.accommodation = includeAccommodation ? accommodationType : "none"
    }

    const payment = await getMollie().payments.create({
      amount: {
        value: total.toFixed(2),
        currency: "EUR",
      },
      description: `CCG 2026 Registration — ${descriptionParts.join(" + ")}`,
      redirectUrl: `${BASE_URL}/success`,
      webhookUrl: `${BASE_URL}/api/webhook`,
      metadata,
    })

    // Redirect to Mollie checkout
    return new Response(null, {
      status: 303,
      headers: { Location: payment.getCheckoutUrl()! },
    })
  } catch (err) {
    console.error("Error creating Mollie payment:", err)
    return NextResponse.json({ error: "Error creating payment" }, { status: 500 })
  }
}

export async function GET() {
  return NextResponse.json(
    { message: "This is an API endpoint. Use POST to create a checkout session." },
    { status: 405 },
  )
}
