import { NextResponse } from "next/server";
import { createMortgageCase, loadCase, serializeCase } from "@/lib/mortgage/caseService";

export async function POST() {
  try {
    const created = await createMortgageCase();
    return NextResponse.json(serializeCase(created));
  } catch (e) {
    const msg = e instanceof Error ? e.message : "Failed to create case";
    return NextResponse.json(
      { error: msg, hint: "Start XAMPP MySQL and run npx prisma migrate deploy in web/" },
      { status: 500 }
    );
  }
}

export async function GET(req: Request) {
  const url = new URL(req.url);
  const id = url.searchParams.get("id");
  if (!id) return NextResponse.json({ error: "id required" }, { status: 400 });
  const row = await loadCase(id);
  if (!row) return NextResponse.json({ error: "not found" }, { status: 404 });
  return NextResponse.json(serializeCase(row));
}
