import type { RequestHandler } from "./$types";
import { api } from "$lib/optima-api/axios";
import { json, error } from "@sveltejs/kit";

export const GET: RequestHandler = async ({ locals }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) throw error(401, "Unauthorized");

  try {
    const res = await api.get("/v1/credential-type", {
      headers: { Authorization: `Bearer ${accessToken}` },
    });
    return json(res.data);
  } catch (err: unknown) {
    const status =
      (err as { response?: { status?: number } })?.response?.status ?? 500;
    const message =
      (err as { response?: { data?: { message?: string } } })?.response?.data
        ?.message ?? "Failed to load credential types";
    throw error(status, message);
  }
};
