import { NextRequest, NextResponse } from "next/server";
import {
  assertBootstrapSetupAvailable,
  assertBootstrapToken,
  BootstrapSetupError,
} from "@/features/setup/server/setup-bootstrap.service";
import { getCurrentSetupRun } from "@/features/setup/server/setup-state.service";

export async function GET(request: NextRequest) {
  try {
    await assertBootstrapSetupAvailable();
    assertBootstrapToken(request.headers.get("x-bootstrap-setup-token") ?? "");

    return NextResponse.json(await getCurrentSetupRun());
  } catch (error) {
    if (error instanceof BootstrapSetupError) {
      return NextResponse.json(
        { message: error.message, details: error.details },
        { status: error.status },
      );
    }

    return NextResponse.json(
      { message: "Unable load bootstrap setup run" },
      { status: 500 },
    );
  }
}
