import GenericError from "../../../Errors/GenericError";
import { activityCw } from "./activities";
import { CWActivity, CWCreateActivity } from "./activity.types";

/**
 * Create a new activity in ConnectWise.
 *
 * @param activity - The activity data to create
 * @returns The newly created CW activity object
 * @throws GenericError if the creation fails
 */
export const createActivity = async (
  activity: CWCreateActivity,
): Promise<CWActivity> => {
  try {
    return await activityCw.create(activity);
  } catch (error) {
    const errBody = (error as any).response?.data || error;
    console.error("Error creating activity:", errBody);
    throw new GenericError({
      name: "CreateActivityError",
      message: "Failed to create activity in ConnectWise",
      cause: typeof errBody === "string" ? errBody : JSON.stringify(errBody),
      status: 502,
    });
  }
};
