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

export interface PositionHeaders {
  tenantKey: string;
  unitId?: string;
}
export interface MovePostionPayload {
  positionId: string;
  newParentId: string;
}
export interface PositionPayload {
  name: {
    am: string;
    en: string;
  };
  key: string;
  unitId: string;
  organizationId: string;
  parentPositionId?: string;
  projectId?: string;
  positionTypeId?: string;
}
export interface PositionQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
}

export const getPositions = async (
  params?: PositionQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get("/positions", {
    ...{ headers: withHeaders() },
    params,
  });
};

export const getPositionById = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/${id}`, { headers: withHeaders() });
};

export const getPositionHierarchy = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/hierarchy/${id}`, {
    headers: withHeaders(),
  });
};

// Returns the direct children of a position: { count, items: PositionDto[] }
// where each item has parentPositionId === id.
export const getChildPositions = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/children/${id}`, {
    headers: withHeaders(),
  });
};

export const getPositionList = async (
  id: string | number,
  params: PositionQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/list/${id}`, {
    headers: withHeaders(),
    params,
  });
};
export const fetchPositionsByTypeAndUnit = async (
  positionTypeKey: string,
  unitId: string,
): Promise<OrganizationsPositions[]> => {
  const { data } = await axiosInstance.get(
    `/positions/list/position-type/${positionTypeKey}/unitId/${unitId}`,
    { headers: withHeaders() },
  );

  return Array.isArray(data) ? data : (data?.items ?? []);
};

export const fetchPositionsByRankAndUnit = async (
  unitId: string,
  rank: number,
): Promise<OrganizationsPositions[]> => {
  const { data } = await axiosInstance.get(
    `/positions/list/${unitId}/rank/${rank}`,
    {
      headers: withHeaders(),
    },
  );

  if (Array.isArray(data)) return data;
  if (Array.isArray(data?.items)) return data.items;
  if (Array.isArray(data?.data)) return data.data;
  return data?.data?.items ?? [];
};

export const getEmployeesUnderPosition = async (
  positionId: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/current/${positionId}/employees`, {
    headers: withHeaders(),
  });
};

export const createPosition = async (
  data: PositionPayload,
): Promise<AxiosResponse> => {
  return axiosInstance.post("/positions", data, { headers: withHeaders() });
};

export const updatePosition = async (
  id: string | number,
  data: PositionPayload,
): Promise<AxiosResponse> => {
  return axiosInstance.put(`/positions/${id}`, data, {
    headers: withHeaders(),
  });
};

export const movePosition = async (
  data: MovePostionPayload,
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/positions/change-parent`, data, {
    headers: withHeaders(),
  });
};

export const deletePosition = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/positions/${id}`, { headers: withHeaders() });
};

export const softDeletePosition = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.delete(`/positions/${id}/soft`, {
    headers: withHeaders(),
  });
};

export const getArchivedPositions = async (
  parentId: string | number,
  params?: PositionQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/positions/archived/${parentId}`, {
    headers: withHeaders(),
    params,
  });
};

export const restorePosition = async (
  id: string | number,
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/positions/${id}/restore`, null, {
    headers: withHeaders(),
  });
};
