import api from "./api";

import {
  getAdminHeaders
} from "./adminAuthService";

/* =========================================================
   GET ADMINISTRATOR USERS
   ========================================================= */

export const getAdminUsers =
  async ({
    page = 1,
    limit = 10,
    search = "",
    role = "",
    status = ""
  } = {}) => {
    const response =
      await api.get(
        "/admin/users",
        {
          headers:
            getAdminHeaders(),

          params: {
            page,
            limit,

            search:
              search ||
              undefined,

            role:
              role ||
              undefined,

            status:
              status ||
              undefined
          }
        }
      );

    return response.data;
  };

/* =========================================================
   CREATE ADMINISTRATOR USER
   ========================================================= */

/* =========================================================
   CREATE ADMINISTRATOR USER
   ========================================================= */

export const createAdminUser =
  async (
    payload
  ) => {
    if (
      !payload ||
      typeof payload !==
        "object"
    ) {
      throw new Error(
        "Administrator details are required."
      );
    }

    /*
     * The user form calls the field
     * temporary_password, while the backend
     * password validator uses password.
     *
     * Send both names for compatibility.
     */

    const password =
      String(
        payload.password ||
        payload.temporary_password ||
        ""
      );

    const response =
      await api.post(
        "/admin/users",
        {
          ...payload,

          password,

          temporary_password:
            password
        },
        {
          headers:
            getAdminHeaders()
        }
      );

    return response.data;
  };

/* =========================================================
   UPDATE ADMINISTRATOR USER
   ========================================================= */

export const updateAdminUser =
  async (
    adminUserId,
    payload
  ) => {
    if (!adminUserId) {
      throw new Error(
        "Administrator user ID is required."
      );
    }

    if (
      !payload ||
      typeof payload !==
        "object"
    ) {
      throw new Error(
        "Administrator details are required."
      );
    }

    const response =
      await api.patch(
        `/admin/users/${adminUserId}`,
        payload,
        {
          headers:
            getAdminHeaders()
        }
      );

    return response.data;
  };

/* =========================================================
   UPDATE ADMINISTRATOR STATUS
   ========================================================= */

export const updateAdminUserStatus =
  async (
    adminUserId,
    status
  ) => {
    if (!adminUserId) {
      throw new Error(
        "Administrator user ID is required."
      );
    }

    if (
      ![
        "active",
        "inactive"
      ].includes(
        status
      )
    ) {
      throw new Error(
        "A valid administrator status is required."
      );
    }

    const response =
      await api.patch(
        `/admin/users/${adminUserId}/status`,
        {
          status
        },
        {
          headers:
            getAdminHeaders()
        }
      );

    return response.data;
  };

/* =========================================================
   RESET ADMINISTRATOR PASSWORD
   ========================================================= */

/* =========================================================
   RESET ADMINISTRATOR PASSWORD
   ========================================================= */

export const resetAdminUserPassword =
  async (
    adminUserId,
    temporaryPassword
  ) => {
    if (!adminUserId) {
      throw new Error(
        "Administrator user ID is required."
      );
    }

    const password =
      String(
        temporaryPassword ||
        ""
      );

    if (
      password.trim().length <
      12
    ) {
      throw new Error(
        "The temporary password must contain at least 12 characters."
      );
    }

    const response =
      await api.patch(
        `/admin/users/${adminUserId}/reset-password`,
        {
          password,

          temporary_password:
            password
        },
        {
          headers:
            getAdminHeaders()
        }
      );

    return response.data;
  };