<script>
import { ref } from "vue";
import { authenticatedRequest } from "@/components/session/authenticatedRequest.vue";
import { parseError } from "@/components/request/HandleGlobalError.vue";
import { DatabaseSystemObject } from "@/components/session/token/superUser/systemDatabase.vue";
import { Cron } from "@/components/session/token/superUser/cron.vue";
import { Economic } from "@/components/session/token/superUser/modules/Economic/Economic.vue";
import { reCAPTCHA } from "@/components/session/token/superUser/modules/reCAPTCHA/reCAPTCHA.vue";
import { Email } from "@/components/session/token/superUser/modules/Email/Email.vue";
import { Backups } from "@/components/session/token/superUser/modules/Backups/Backups.vue";
import { MotorAPI } from "@/components/session/token/superUser/modules/motorapi/MotorAPI.vue";
import { Stripe } from "@/components/session/token/superUser/modules/stripe/Stripe.vue";
import { FXRatesAPI } from "@/components/session/token/superUser/modules/fxratesapi/FXRatesAPI.vue";
import { GatewayAPI } from "@/components/session/token/superUser/modules/gatewayapi/GatewayAPI.vue";
import { XLVask } from "@/components/session/token/superUser/modules/xlvask/XLVask.vue";
import { Entra } from "@/components/session/token/superUser/modules/entra/Entra.vue";

/**
 * The superuser object
 */
export const SuperUserObject = {
  system: {
    database: DatabaseSystemObject,
  },
  modules: {
    economic: Economic,
    reCAPTCHA: reCAPTCHA,
    email: Email,
    backups: Backups,
    motorapi: MotorAPI,
    stripe: Stripe,
    fxratesapi: FXRatesAPI,
    gatewayapi: GatewayAPI,
    xlvask: XLVask,
    entra: Entra,
  },
  /** Intimidate a user */
  intimidate: {
    /**
     * Intimidate a user
     * @param userId
     * @returns {Promise<void>}
     */
    intimidateUser: async (userId) => {
      return await authenticatedRequest("/su/intimidate", "POST", {user_id: userId})
          .then(
              (response) => {
                // Save the current token, so we can restore it after the user has been intimidated
                const currentToken = localStorage.getItem("token");
                localStorage.setItem("superuser_token", currentToken);
                // Set the token in the local storage
                localStorage.setItem("token", response.data.data.token);
                // Redirect the user to the dashboard
                window.location = "/";
              }
          )
          .catch(
              (error) => {
                parseError(error, 'auth');
              }
          );
    },
    /**
     * Go back from intimidating a user
     */
    goBack: () => {
      // Get the superuser token
      const superuserToken = localStorage.getItem("superuser_token");
      // Set the token in the local storage
      localStorage.setItem("token", superuserToken);
      // Clear the superuser token
      localStorage.removeItem("superuser_token");
      // Redirect the user to the dashboard
      window.location = "/";
    },
    /**
     * Check if the superuser is currently intimidating a user
     * @returns {boolean}
     */
    isIntimidated: () => {
      return localStorage.getItem("superuser_token") !== null;
    }
  },
  cron: Cron
};
</script>