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

// --- Types ---
export interface UnitHeaders {
  delegatorPositionId: string;
  currentProjectId?: string;
}

export interface UnitPayload {
  name: {
    am: string;
    en: string;
  };
  key: string;
  organizationId?: string;
  parentUnitId?: string;
}

export interface UnitQueryParams {
  orderBy?: string;
  take?: number;
  skip?: number;
  order?: string;
}

// --- API Methods ---

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

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

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

export const getUnitList = async (
  id: string | number,
  queryParams: UnitQueryParams,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/units/list/${id}`, {
    params: queryParams,
    headers: withHeaders(),
  });
};

export const getMyAdminUnits = async (
  organizationId: string,
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/units/my-admin-units/organization/${organizationId}`, {
    headers: withHeaders(),
  });
};

export const createUnit = async (data: UnitPayload): Promise<AxiosResponse> => {
  return axiosInstance.post(`/units`, data, { headers: withHeaders() });
};

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

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

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

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

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

export interface RelateUnitToUnitPayload {
  unitId: string;
  newParentUnitId: string;
}

export const relateUnitToUnit = async (
  payload: RelateUnitToUnitPayload
): Promise<AxiosResponse> => {
  return axiosInstance.patch(`/units/relate-unit-to-unit`, payload, {
    headers: withHeaders(),
  });
};

export const getChildUnits = async (
  parentUnitId: string,
  params?: UnitQueryParams
): Promise<AxiosResponse> => {
  return axiosInstance.get(`/units/child-units/${parentUnitId}`, {
    params: {
      take: params?.take ?? 50,
      skip: params?.skip ?? 0,
      ...(params?.orderBy && { orderBy: params.orderBy }),
      ...(params?.order && { order: params.order }),
    },
    headers: withHeaders(),
  });
};
