import { httpDeleteInterceptor, httpGet, httpPost, httpPut } from "./http-config/axios-base-service";
import Cookies from "js-cookie";
import axiosBaseInstance from "./http-config/http-base-instance";

const token = Cookies.get("access_token");
const id = Cookies.get("uie");

export async function listMasterUsers(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpGet(`/users?page=${data.page}&limit=${data.limit}`, headers);
}

export async function createMasterUser(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  const pathUrl = `/users`;
  return await httpPost(pathUrl, headers, data);
}
export async function emailValidation(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  const pathUrl = `/users/email-validation`;
  return await httpPost(pathUrl, headers, data);
}
export async function setupEmail(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  const pathUrl = `/users/setup-email`;
  return await httpPost(pathUrl, headers, data);
}

export async function getDetailMasterUsers(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpGet(`/users/detail/${id}`, headers);
}

export async function editMasterUsers(data: any, id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPut(`/users/${id}`, headers, data);
}

export async function deleteMasterUser(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpDeleteInterceptor(`/users/${id}`, headers);
}

export async function postSignIn(data: any) {
  const headers = {
    accept: "application/json",
    "content-type": "application/json",
  };
  const pathUrl = `/users/login`;
  return await httpPost(pathUrl, headers, data);
}

export async function getProfile(code?: string) {
  const headers = {
    "content-type": "application/json",
    Authorization: `Bearer ${code || token}`,
  };
  return await httpGet(`/users/info`, headers);
}

export async function updateProfile(data: any) {
  const headers = {
    "content-type": "application/json",
    Authorization: `Bearer ${token}`,
  };
  return await httpPut(`/users/${id}`, headers, data);
}
export async function savePassword(data: any) {
  const headers = {
    "content-type": "application/json",
    Authorization: `Bearer ${token}`,
  };
  return await httpPost(`/users/save-password`, headers, data);
}

export async function resetPassword(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPost(`/users/reset-password`, headers, data);
}

export async function checkUsernames(username: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPost(`/users/forgot-password`, headers, { username });
}

export async function otpRequest(email: string, name: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPost(`/users/otp-request`, headers, { email, name });
}

export async function otpValidation(email: string, otpCode: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPost(`/users/otp-validation`, headers, { email, otpCode });
}
export async function otpValidationLogin(data: any) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpPost(`/users/otp-validation`, headers, data);
}

export async function postArticleComment(data: any) {
  const headers = token
    ? {
        "content-type": "application/json",
        Authorization: `Bearer ${token}`,
      }
    : {
        "content-type": "application/json",
      };
  return await httpPost(`/article-comments`, headers, data);
}

export async function editArticleComment(data: any, id: number) {
  const headers = {
    "content-type": "application/json",
    Authorization: `Bearer ${token}`,
  };
  return await httpPut(`/article-comments/${id}`, headers, data);
}

export async function getArticleComment(id: string) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpGet(`/article-comments?isPublic=true&articleId=${id}`, headers);
}

export async function deleteArticleComment(id: number) {
  const headers = {
    "content-type": "application/json",
  };
  return await httpDeleteInterceptor(`/article-comments/${id}`, headers);
}

export async function getCsrfToken() {
  const pathUrl = "csrf-token";
  const headers = {
    "content-type": "application/json",
  };
  return httpGetTemp(pathUrl, headers);
}

async function httpGetTemp(pathUrl: any, headers: any) {
  const response = await axiosBaseInstance.get(pathUrl, { headers }).catch(function (error: any) {
    console.log(error);
    return error.response;
  });
  console.log("Response base svc : ", response);
  if (response?.status == 200 || response?.status == 201) {
    return {
      error: false,
      message: "success",
      data: response?.data,
    };
  } else {
    return {
      error: true,
      message: response?.data?.message || response?.data || null,
      data: null,
    };
  }
}
