import { NextResponse } from "next/server";
import { adminCookieOptions, adminSessionCookie, createAdminSession, validateAdminCredentials } from "@/lib/admin/auth";

export async function POST(request: Request) {
  const form = await request.formData();
  const email = String(form.get("email") ?? "");
  const password = String(form.get("password") ?? "");

  if (!validateAdminCredentials(email, password)) {
    return NextResponse.json({ message: "Ungültige Zugangsdaten." }, { status: 401 });
  }

  const response = NextResponse.json({ message: "Anmeldung erfolgreich." });
  response.cookies.set(adminSessionCookie, createAdminSession(), adminCookieOptions());
  return response;
}
