import axiosInstance from "@/shared/services/axiosInstance";
import Cookies from "js-cookie";
import { withHeaders } from "./withHeaders";

export interface OrganizationsPositionsName {
  am: string;
  en: string;
}

export interface OrganizationsPositions {
  id: string;
  createdAt: string;
  updatedAt: string;
  name: OrganizationsPositionsName;
  key: string;
  parentPositionId: string | null;
  unitId: string;
  organizationId: string;
  projectId: string | null;
  positionTypeId: string;
}

export interface InternalCCItemDTO {
  id: string; // unique ID of the position/unit
  label: string; // display name
  unitId: string; // unit to which this position belongs
}
export interface OrganizationsPositionsResponse {
  count: number;
  items: OrganizationsPositions[];
}

export const fetchCurrentDepartment = async (
  organizationUnitId: string,
): Promise<any> => {
  try {
    const { data } = await axiosInstance.get(
      `units/current/${organizationUnitId}/employees`,
      {
        headers: withHeaders(),
      },
    );

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message || "Failed to fetch current department";
    throw new Error(message);
  }
};

export const fetchInternalDepartments =
  async (): Promise<OrganizationsPositionsResponse> => {
    try {
      const { data } = await axiosInstance.get(
        `positions/parallel-and-below/`,
        {
          headers: withHeaders(),
        },
      );

      return data;
    } catch (error) {
      throw error;
    }
  };

export const fetchImmediateChildDepartments =
  async (): Promise<OrganizationsPositionsResponse> => {
    try {
      const { data } = await axiosInstance.get(`positions/immediate-child`, {
        headers: withHeaders(),
      });

      return data;
    } catch (error: any) {
      const message =
        error?.response?.data?.message ||
        "Failed to fetch immedaitely departments";
      throw new Error(message);
    }
  };

export const fetchScopedDepartments = async (
  params: {
    take?: number;
    skip?: number;
  } = {},
): Promise<OrganizationsPositionsResponse> => {
  try {
    const { data } = await axiosInstance.get(`positions/scoped`, {
      params: {
        ...params,
      },
      headers: withHeaders(),
    });

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message || "Failed to fetch scoped departments";
    throw new Error(message);
  }
};

export const fetchAssignedScopeDepartments = async (
  params: {
    take?: number;
    skip?: number;
  } = {},
): Promise<OrganizationsPositionsResponse> => {
  try {
    const { data } = await axiosInstance.get(
      `/positions/internal-record-assigning`,
      {
        params: {
          ...params,
        },
        headers: withHeaders(),
      },
    );

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message ||
      "Failed to fetch assigned scope departments";
    throw new Error(message);
  }
};

export const fetchScopedEmployees = async (
  params: {
    take?: number;
    skip?: number;
  } = {},
): Promise<OrganizationsPositionsResponse> => {
  try {
    const { data } = await axiosInstance.get(`positions/scoped`, {
      params: {
        ...params,
      },
      headers: withHeaders(),
    });

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message || "Failed to fetch scoped employees";
    throw new Error(message);
  }
};

export const fetchParallelAndBelowDepartments =
  async (): Promise<OrganizationsPositionsResponse> => {
    try {
      const { data } = await axiosInstance.get(
        `positions/parallel-and-below/`,
        {
          headers: withHeaders(),
        },
      );

      return data;
    } catch (error) {
      throw error;
    }
  };

export const fetchAllInternalDepartments = async (
  unitId: string | null = null,
  params: {
    take?: number;
    skip?: number;
  } = {},
): Promise<OrganizationsPositionsResponse> => {
  try {
    const { data } = await axiosInstance.get(`positions/list/${unitId}`, {
      params: {
        ...params,
      },
      headers: withHeaders(),
    });

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message ||
      "Failed to fetch all internal departments";
    throw new Error(message);
  }
};

export const fetchInternalReceivingDepartments = async (
  params: {
    take?: number;
    skip?: number;
  } = {},
): Promise<OrganizationsPositionsResponse> => {
  try {
    const { data } = await axiosInstance.get(
      `positions/internal-record-receiving`,
      {
        params: {
          ...params,
          take: params.take || 3000,
        },
        headers: withHeaders(),
      },
    );

    return data;
  } catch (error: any) {
    const message =
      error?.response?.data?.message ||
      "Failed to fetch internal receiving departments";
    throw new Error(message);
  }
};
