import axiosInstance from "@/shared/services/axiosInstance";
import qs from "qs";
import { withHeaders } from "./withHeaders";
import { UnitDto } from "@/user-management/dto/unit/unitDto";
import { TreeViewItem } from "@/shared/common/ui/treeSelect";
import { EUserStatus } from "@/user-management/userManagement/hooks/usePendingUsersHook";

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

export interface OrgUnit {
  createdAt: string;
  updatedAt: string;
  id: string;
  name: OrgName;
  key: string;
  organizationId: string;
  parentUnitId: string | null;
}

export interface ValidOrganizationDto {
  id: string;
  createdAt: string;
  updatedAt: string;
  name: OrgName;
  key: string;
  status: "Active" | "Inactive";
  parentId: string | null;
  organizationTypeId: string;
  unit: OrgUnit | null;
  units: OrgUnit[];
}

export interface ValidOrganizationsDto {
  count: number;
  items: ValidOrganizationDto[];
}

export interface UpdatedValidOrganizationDto {
  firstUnitId: string;
  hasChildren: boolean;
  name: OrgName;
  id: string;
  type: string;
}

export interface Params {
  take?: number;
  skip?: number;
  orderBy?: string;
  userStatus?:EUserStatus;

}

export const fetchValidOrganization = async ({
  queryKey,
}: {
  queryKey: [string, Params?];
}): Promise<ValidOrganizationsDto> => {
  const [, params = { take: 300, skip: 0 }] = queryKey;

  try {
    const { data } = await axiosInstance.get("organizations/record-receiving", {
      headers: withHeaders(),
      params,
      paramsSerializer: (params) => qs.stringify(params),
    });

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

export const fetchModifiedValidOrganization = async ({
  queryKey,
}: {
  queryKey: [string, Params?];
}): Promise<TreeViewItem[]> => {
  const [, params = { take: 300, skip: 0 }] = queryKey;

  try {
    const { data } = await axiosInstance.get("organizations/children", {
      headers: withHeaders(),
      params,
      paramsSerializer: (params) => qs.stringify(params),
    });

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

export const fetchChildrensById = async ({
  queryKey,
  id,
}: {
  queryKey: [string, Params?];
  id: string;
}): Promise<UpdatedValidOrganizationDto[]> => {
  const [, params = { take: 300, skip: 0 }] = queryKey;

  try {
    const { data } = await axiosInstance.get(
      `
organizations/children/${id}`,
      {
        headers: withHeaders(),
        params,
        paramsSerializer: (params) => qs.stringify(params),
      }
    );

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

export const fetchOrganizationById = async (
  organizationId: string
): Promise<any> => {
  try {
    const { data } = await axiosInstance.get(
      `organizations/${organizationId}`,
      {
        headers: withHeaders(),
      }
    );

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

export const fetchUnitById = async (unitId: string): Promise<UnitDto> => {
  try {
    const { data } = await axiosInstance.get(`units/${unitId}`, {
      headers: withHeaders(),
    });

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