import axiosInstance from "@/shared/services/axiosInstance";
import { withHeaders } from "./withHeaders";
import { SetTwoFAPayload } from "@/record-management/hooks/useAuthUser";

export interface SignupPayload {
  email: string;
  username: string;
  password: string;
  userType: string;
  name: {
    am: string;
    en: string;
  };
}
export interface VerifyUserPayload {
  otp: string;
  email?: string;
}

export interface LoginPayload {
  email: string;
  password: string;
}

export interface AuthHeaders {
  tenantKey: string;
  unitId?: string;
}

export const signup = async (payload: SignupPayload, headers: AuthHeaders) => {
  return axiosInstance.post("/auth/signup", payload, {
    headers: {
      "x-organization-tenant-key": headers.tenantKey,
      ...(headers.unitId && { "x-organization-unit-id": headers.unitId }),
    },
  });
};

export const login = async (payload: LoginPayload, headers: AuthHeaders) => {
  return axiosInstance.post("/auth/login", payload, {
    headers: {
      "x-organization-tenant-key": headers.tenantKey,
      ...(headers.unitId && { "x-organization-unit-id": headers.unitId }),
    },
  });
};

export const getProfile = async (headers: AuthHeaders) => {
  return axiosInstance.get("/auth/me", {
    headers: {
      "x-organization-tenant-key": headers.tenantKey,
      ...(headers.unitId && { "x-organization-unit-id": headers.unitId }),
    },
  });
};

export const logout = async () => {
  return axiosInstance.patch("/auth/logout");
};

export const EditTwoFA = async (payload: SetTwoFAPayload, id: string) => {
  return axiosInstance.put(`/account-configurations/my-config/${id}`, payload, {
    headers: withHeaders(),
  });
};
export const setTwoFA = async (payload: SetTwoFAPayload) => {
  return axiosInstance.post("/account-configurations/set-my-config", payload);
};
export const getTwoFactorStatus = async () => {
  return axiosInstance.get("/account-configurations/my-config");
};
