import { withHeaders } from "@/record-management/services/api/withHeaders";
import axiosInstance from "@/shared/services/axiosInstance";
import { AxiosResponse } from "axios";

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

export interface InviteEPPayload {
  positionId: string;
  username: string;
  email: string;
  name: {
    am: string;
    en: string;
  };
}
export interface InactiveEPPayload {
  employeeId: string;
  positionId: string;
}
export interface RemoveOrAssignFirstsForSecondPayload {
  secondId: string;
  firstIds: [string];
}
export interface RemoveOrAssignSecondsForFirstPayload {
  firstId: string;
  secondIds: [string];
}

export interface AssignEmployeePayload {
  positionId: string;
  employeeId: string;
}

export interface EmployeePositionQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
}
export interface UpdateProfilePayload {
  email?: string;
  username?: string;
  phoneNumber?: string;
  name: {
    am?: string;
    en?: string;
  };
}

// API endpoints

export const inviteEmployeePosition = async (
  payload: InviteEPPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/invite", payload, {
    headers: withHeaders(),
  });

export const setInactive = async (
  payload: InactiveEPPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/inactive", payload, {
    headers: withHeaders(),
  });

export const getGivenFirst = async (
  id: string,
  params?: EmployeePositionQueryParams
): Promise<AxiosResponse> =>
  axiosInstance.get(`/employee-positions/given-first/${id}`, {
    ...{ headers: withHeaders() },
    params,
  });

export const getGivenSecond = async (
  id: string,
  params?: EmployeePositionQueryParams
): Promise<AxiosResponse> =>
  axiosInstance.get(`/employee-positions/given-second/${id}`, {
    ...{ headers: withHeaders() },
    params,
  });
export const updateProfile = async (payload: UpdateProfilePayload,id:string) => {
  return axiosInstance.patch(`/users/update-users-profile/${id}`, payload);
};
export const updateUserProfile = async (
  payload: UpdateProfilePayload,
  id: string
) => {
  return axiosInstance.patch(`/users/update-users-profile/${id}`, payload);
};
export const assignEmployees = async (
  payload: AssignEmployeePayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/assign", payload, {
    headers: withHeaders(),
  });

export const assignFirstsForSecond = async (
  payload: RemoveOrAssignFirstsForSecondPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/assign-firsts-for-second", payload, {
    headers: withHeaders(),
  });

export const assignSecondsForFirst = async (
  payload: RemoveOrAssignSecondsForFirstPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/assign-seconds-for-first", payload, {
    headers: withHeaders(),
  });

export const deleteEmployeePosition = async (
  id: string
): Promise<AxiosResponse> =>
  axiosInstance.delete(`/employee-positions/${id}`, { headers: withHeaders() });

export const removeFirstsForSecond = async (
  payload: RemoveOrAssignFirstsForSecondPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/remove-firsts-for-second", payload, {
    headers: withHeaders(),
  });

export const removeSecondsForFirst = async (
  payload: RemoveOrAssignSecondsForFirstPayload
): Promise<AxiosResponse> =>
  axiosInstance.post("/employee-positions/remove-seconds-for-first", payload, {
    headers: withHeaders(),
  });

  export const deactivateUser = async (
    userId: string
  ): Promise<AxiosResponse> => {
    return axiosInstance.patch(`/employees/${userId}/deactivate`, {
      headers: withHeaders(),
    });
  };

    export const activateUser = async (
      userId: string
    ): Promise<AxiosResponse> => {
      return axiosInstance.patch(`/employees/${userId}/activate`, {
        headers: withHeaders(),
      });
    };

    