import { NextResponse } from "next/server";
import { requireAdminSession } from "@/lib/admin/auth";
import { updateRepairStatus } from "@/lib/admin/store";
import type { InquiryStatus } from "@/lib/admin/types";

const statuses: InquiryStatus[] = ["new", "in_progress", "done"];

export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
  if (!await requireAdminSession()) return NextResponse.json({ message: "Nicht autorisiert." }, { status: 401 });

  const body = await request.json() as { status?: InquiryStatus };
  if (!body.status || !statuses.includes(body.status)) {
    return NextResponse.json({ message: "Ungültiger Status." }, { status: 400 });
  }

  const { id } = await params;
  await updateRepairStatus(id, body.status);
  return NextResponse.json({ message: "Status aktualisiert." });
}
