import axios from 'axios';

interface CSDAccountStatusBasic {
  /**
   * @deprecated
   */
  found?: boolean,
  readonly tier: number,
  readonly totalReferrals?: number,
  readonly createdAt?: Date,
  readonly warns?: Date[],
  readonly locks?: Date[],
  readonly deletes?: Date[],
}

export default class CloudServicesUtil {
  /**
   * This function calls an internal CSD API to upgrade the member's Tier to T2 from T1.
   * @param userID The member's ID
   * @param authorization The authorization key | Client#config.internalKey
   * @returns HTTP Status Code
   */
  static async upgradeT2(userID: string, authorization: string) {
    const response = await axios.get(`https://api.cloud.libraryofcode.org/wh/t2?userID=${userID}&auth=${authorization}`);
    return response.status;
  }

  /**
   * This function calls an internal CSD API to downgrade the member's Tier to T1 from T2.
   * Do not call this API to downgrade from T3 to T1, the function call will silently fail.
   * @param userID The member's ID
   * @param authorization The authorization key | Client#config.internalKey
   * @returns HTTP Status Code
   */
  static async downgradeT2(userID: string, authorization: string) {
    const response = await axios.get(`https://api.cloud.libraryofcode.org/wh/t2-rm?userID=${userID}&auth=${authorization}`);
    return response.status;
  }

  static async setTier(userID: string, tier: 1 | 2 | 3, authorization: string) {
    const response = await axios.get(`https://api.cloud.libraryofcode.org/wh/set-tier?userID=${userID}&t=${tier}&auth=${authorization}`);
    return response.status;
  }

  static async fetchAccountStatus(userID: string, authorization: string) {
    const response = (await axios.get<CSDAccountStatusBasic>(`https://api.cloud.libraryofcode.org/wh/score?id=${userID}&authorization=${authorization}`)).data;
    if (!response.found) return null;
    delete response.found;
    return response;
  }
}
