import { keysToSnakeCase } from "@bigbinary/neeto-cist";
import { BASE_URL } from "@neetoplaywright";

import { CommonUtils } from "@poms/index";
import { UpdateAvailabilityProps } from "@poms/types";

export class AvailabilityApis {
  private readonly availabilityBaseUrl: string;

  constructor(private commonUtils: CommonUtils) {
    this.availabilityBaseUrl = `${BASE_URL}/availabilities`;
  }

  create = (body: Record<string, unknown>) =>
    this.commonUtils.apiRequest({
      method: "post",
      url: this.availabilityBaseUrl,
      body: keysToSnakeCase({ availability: body }),
      failOnStatusCode: false,
    });

  fetch = () => this.commonUtils.apiRequest({ url: this.availabilityBaseUrl });

  fetchDefault = (baseURL = "") =>
    this.commonUtils.apiRequest({
      url: `${baseURL}${this.availabilityBaseUrl}/default`,
    });

  fetchDetails = (availabilityId: string) =>
    this.commonUtils.apiRequest({
      url: `${this.availabilityBaseUrl}/${availabilityId}`,
    });

  update = ({ availabilityId, body, baseURL = "" }: UpdateAvailabilityProps) =>
    this.commonUtils.apiRequest({
      url: `${baseURL}${this.availabilityBaseUrl}/${availabilityId}`,
      method: "put",
      data: { availability: body },
    });

  delete = (availabilityId: string) =>
    this.commonUtils.apiRequest({
      method: "delete",
      url: `${this.availabilityBaseUrl}/${availabilityId}`,
    });

  destroyDayAvailability = (periodableId: string) =>
    this.commonUtils.apiRequest({
      url: `${this.availabilityBaseUrl}/${periodableId}`,
      method: "put",
      data: {
        availability: { periods_attributes: [], update_with_destroy: true },
      },
    });
}
