import {
  API_ADMIN_DISABLE_2FA,
  API_CHANGE_PASSWORD,
  API_DISABLE_2FA,
  API_ENABLE_2FA,
  API_ENABLE_2FA_VERIFY,
  API_FORGOT_PASSWORD,
  API_PATH_GET_PROFILE,
  API_PATH_LOGIN,
  API_PATH_LOGIN_2FA,
  API_PATH_REFRESH_TOKEN,
  API_USERS,
} from '@/constants/api';
import { request } from '@umijs/max';

export async function apiLogin(body: MasterModel.LoginRequestBody) {
  // console.log('Login request body:', body);

  return request<MasterModel.LoginResponse>(API_PATH_LOGIN, {
    method: 'POST',
    data: body,
  });
}

export async function apiRefreshToken(
  body: MasterModel.RefreshTokenRequestBody,
) {
  return request<MasterModel.RefreshTokenReponse>(API_PATH_REFRESH_TOKEN, {
    method: 'POST',
    data: body,
  });
}

export async function apiQueryProfile() {
  return request<MasterModel.UserResponse>(API_PATH_GET_PROFILE);
}

export async function apiUpdateProfile(
  body: Partial<MasterModel.UserMetadata>,
) {
  return request<MasterModel.UserResponse>(API_USERS, {
    method: 'PUT',
    data: {
      metadata: body,
    },
    getResponse: true,
  });
}

export async function apiChangePassword(
  body: MasterModel.ChangePasswordRequestBody,
) {
  return request<void>(API_CHANGE_PASSWORD, {
    method: 'PATCH',
    data: body,
    getResponse: true,
  });
}
export async function apiForgotPassword(
  body: MasterModel.ForgotPasswordRequestBody,
) {
  return request<MasterModel.ForgotPasswordResponse>(API_FORGOT_PASSWORD, {
    method: 'POST',
    data: body,
  });
}

// 2FA
export async function apiLogin2FA(
  token: string,
  body: MasterModel.Verify2FARequestBody,
) {
  return request<MasterModel.LoginResponse>(API_PATH_LOGIN_2FA, {
    method: 'POST',
    data: body,
    headers: {
      Authorization: token,
    },
  });
}

export async function apiEnable2FA() {
  return request<MasterModel.Enable2FAResponse>(API_ENABLE_2FA, {
    method: 'GET',
    getResponse: true,
  });
}

export async function apiVerify2FA(body: MasterModel.Verify2FARequestBody) {
  return request(API_ENABLE_2FA_VERIFY, {
    method: 'POST',
    data: body,
    getResponse: true,
  });
}

export async function apiDisable2FA() {
  return request(API_DISABLE_2FA, {
    method: 'PUT',
    getResponse: true,
  });
}

export async function apiAdminDisable2FA(userId: string) {
  return request(API_ADMIN_DISABLE_2FA + userId, {
    method: 'PUT',
    getResponse: true,
  });
}
