import { NextResponse } from "next/server";
import { listAssignments, createAssignment } from "@/lib/store";

export const dynamic = "force-dynamic";

export async function GET() {
  return NextResponse.json({ assignments: listAssignments() });
}

export async function POST(request) {
  try {
    const body = await request.json();
    if (!body || typeof body !== "object") throw new Error("Missing assignment body.");
    const record = createAssignment(body);
    return NextResponse.json(record, { status: 201 });
  } catch (e) {
    return NextResponse.json({ error: String(e.message || e) }, { status: 400 });
  }
}
