import { CalendarEvent, CaldavConfig } from "@calchat/shared";
import { ApiClient } from "./ApiClient";

export const CaldavConfigService = {
  saveConfig: async (
    serverUrl: string,
    username: string,
    password: string,
  ): Promise<CaldavConfig> => {
    return ApiClient.put<CaldavConfig>("/caldav/config", {
      serverUrl,
      username,
      password,
    });
  },
  getConfig: async (): Promise<CaldavConfig> => {
    return ApiClient.get<CaldavConfig>("/caldav/config");
  },
  deleteConfig: async (): Promise<void> => {
    return ApiClient.delete<void>("/caldav/config");
  },
  pull: async (): Promise<CalendarEvent[]> => {
    return ApiClient.post<CalendarEvent[]>("/caldav/pull");
  },
  pushAll: async (): Promise<void> => {
    return ApiClient.post<void>("/caldav/pushAll");
  },
  sync: async (): Promise<void> => {
    await ApiClient.post<void>("/caldav/pushAll");
    await ApiClient.post<CalendarEvent[]>("/caldav/pull");
  },
};
