import { APIClient } from "../ApiClient";
import { Family } from "../FamilyApi";

export interface NewCalendarURL {
  accommodation_id?: number;
  name: string;
}

export interface AccommodationCalendarURL {
  id: number;
  family_id: number;
  accommodation_id: number;
  user_id: number;
  name: string;
  token: string;
  time_create: number;
  time_used: number;
}

export class AccommodationsCalendarURLApi {
  /**
   * Create a new accommodation calendar URL
   */
  static async Create(
    family: Family,
    calendar: NewCalendarURL
  ): Promise<AccommodationCalendarURL> {
    return (
      await APIClient.exec({
        method: "POST",
        uri: `/family/${family.family_id}/accommodations/reservations_calendars/create`,
        jsonData: calendar,
      })
    ).data;
  }

  /**
   * Get accommodation calendar URL route
   */
  static CalendarURL(c: AccommodationCalendarURL): string {
    return `${APIClient.backendURL()}/acccommodations_calendar/${c.token}`;
  }

  /**
   * Get accommodations calendars list
   */
  static async GetList(family: Family): Promise<AccommodationCalendarURL[]> {
    return (
      await APIClient.exec({
        method: "GET",
        uri: `/family/${family.family_id}/accommodations/reservations_calendars/list`,
      })
    ).data;
  }

  /**
   * Delete an accommodation calendar
   */
  static async Delete(
    calendar: AccommodationCalendarURL
  ): Promise<AccommodationCalendarURL> {
    return (
      await APIClient.exec({
        method: "DELETE",
        uri: `/family/${calendar.family_id}/accommodations/reservations_calendars/${calendar.id}`,
      })
    ).data;
  }
}
