/**
 * Single Entry Status Check — User-triggered from UI.
 *
 * POST with session auth, body: { entryId: string }
 */

import { NextRequest, NextResponse } from "next/server";
import { getAuthSession } from "@/core/auth";
import { checkSingleEntry } from "@/modules/registratura/services/status-check-service";

export async function POST(req: NextRequest) {
  const session = await getAuthSession();
  if (!session?.user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const body = await req.json();
    const { entryId } = body as { entryId: string };

    if (!entryId) {
      return NextResponse.json(
        { error: "Missing entryId" },
        { status: 400 },
      );
    }

    const result = await checkSingleEntry(entryId);
    return NextResponse.json(result);
  } catch (err) {
    const message = err instanceof Error ? err.message : "Internal error";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}
