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

/** GET /api/companies/[id]/details — fetch company with contacts and address */
export const GET: RequestHandler = async ({ params, locals }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) {
    return json({ data: null }, { status: 401 });
  }

  console.log("Here")

  try {
    const result = await optima.company.fetch(accessToken, params.id, {
      includeAllContacts: true,
      includeAddress: true,
      includeAllAddresses: true,
    });
    return json({ data: result?.data ?? null });
  } catch (err) {
    console.error("[api/companies/details] Failed:", err);
    return json({ data: null }, { status: 500 });
  }
};
