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

/** DELETE /sales/opportunity/[id] — delete an opportunity */
export const DELETE: RequestHandler = async ({ params, locals }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) throw error(401, "Unauthorized");

  try {
    const result = await optima.sales.deleteOpportunity(accessToken, params.id);
    return json(result);
  } catch (err: unknown) {
    console.error("Failed to delete opportunity:", err);
    const status =
      err && typeof err === "object" && "status" in err
        ? (err as { status: number }).status
        : 500;
    throw error(status, "Failed to delete opportunity");
  }
};
