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

/** GET /api/companies/[id]/sites — fetch CW sites for a company */
export const GET: RequestHandler = async ({ params, locals }) => {
  const accessToken = locals.session?.accessToken;
  if (!accessToken) {
    return json({ data: [] }, { status: 401 });
  }

  try {
    const result = await optima.company.fetchSites(accessToken, params.id);
    return json({ data: result?.data ?? [] });
  } catch (err) {
    console.error("[api/companies/sites] Failed:", err);
    return json({ data: [] }, { status: 500 });
  }
};
