import axios from "axios";

const userid = localStorage.getItem("ne-userid");

// ==============================|| fetchOrderSummary (orders)||============================== //
export const fetchOrderSummary = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/orders/getordersummary`
  );
  console.log("fetchOrderSummary", response.data.details);
  return response.data.details;
};

// ==============================|| fetchLocationSummary (orders)||============================== //
export const fetchLocationSummary = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/orders/getlocationsummary`
  );
  console.log("fetchLocationSummary", response.data.details);

  return response.data.details;
};

// ==============================|| fetchOrderInsight (orders)||============================== //
export const fetchOrderInsight = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/orders/getorderinsight`
  );
  console.log("fetchOrderInsight", response.data.details);

  return response.data.details;
};

// ==============================|| fetchDeliverySummary (delivery)||============================== //
export const fetchDeliverySummary = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/deliveries/deliverysummary`
  );
  console.log("fetchDeliverySummary", response.data.details);
  return response.data.details;
};

// ==============================|| fetchDeliveryInsight (delivery)||============================== //
export const fetchDeliveryInsight = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/deliveries/getdeliveryinsight`
  );
  console.log("fetchDeliveryInsight", response.data.details);

  return response.data.details;
};
// ==============================|| fetchDeliveryLocationSummary (delivery)||============================== //
export const fetchDeliveryLocationSummary = async () => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/deliveries/getlocationsummary`
  );
  console.log("fetchDeliveryLocationSummary", response.data.details);

  return response.data.details;
};

// ==============================|| fetchAllTenants (clients)||============================== //

export const fetchAllTenants = async ({ queryKey }) => {
  const [size, status, pageno, search, rough] = queryKey;

  let url = "";
  if (search) {
    // url = `https://jupiter.nearle.app/live/api/v1/tenants/search/?keyword=${search}`;
    url = `${process.env.REACT_APP_URL}/tenants/search/?keyword=${search}`;
  } else {
    // url = `https://jupiter.nearle.app/live/api/v1/tenants/getalltenants?pageno=${pageno}&pagesize=${size}`;
    url = `${process.env.REACT_APP_URL}/tenants/getalltenants?pageno=${pageno}&pagesize=${size}`;
  }
  const response = await axios.get(url);

  // tenants/search/?keyword=${search}
  console.log("fetchAllTenants", response.data.details);

  return response.data.details;
};
// ==============================|| fetchCustomersList (customers)||============================== //
export const fetchCustomersList = async ({ queryKey }) => {
  const [pages] = queryKey;
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/customers/getallcustomers/?pageno=${pages}&pagesize=10`
  );
  console.log("fetchCustomersList", response.data.details);
  return response.data.details;
};
// ==============================|| fetchCustomersListBySearch (customers)||============================== //
export const fetchCustomersListBySearch = async ({ queryKey }) => {
  const [search] = queryKey;
  const response = await axios.get(
    search.lenght > 3 &&
      `${process.env.REACT_APP_URL}/customers/search/?keyword=${search}`
  );
  console.log("fetchCustomersListBySearch", response.data.details);
  return response.data.details;
};
// ==============================|| fetchOrdersSummary (orders summary)||============================== //
export const fetchOrdersSummary = async ({ queryKey }) => {
  console.log("queryKey for fetchOrdersSummary", queryKey);
  const [appId, startdate, enddate] = queryKey;
  const response = await axios.get(
    appId == 0
      ? `${process.env.REACT_APP_URL}/deliveries/getreportsummary/?&fromdate=${startdate}&todate=${enddate}`
      : `${process.env.REACT_APP_URL}/deliveries/getreportsummary/?applocationid=${appId}&fromdate=${startdate}&todate=${enddate}`
  );
  console.log("fetchOrdersSummary", response.data.details);

  return response.data.details;
};
// ==============================|| fetchLocations (orders summary))||============================== //

export const fetchLocations = async ({ queryKey }) => {
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/partners/getpartners`
  );
  const updatedLocations = [
    ...response.data.details,
    { partnername: "All", partnerid: -1 }, // Add your new object here
  ];
  console.log("fetchLocations", updatedLocations);

  return updatedLocations;
};
// ==============================|| fetchAppLocations (report summary))||============================== //

export const fetchAppLocations = async ({ queryKey }) => {
  // const response = await axios.get(`${process.env.REACT_APP_URL}/partners/getpartners`);
  const response = await axios.get(
    `${process.env.REACT_APP_URL}/utils/getapplocations`
  );
  const updatedLocations = [
    ...response.data.details,
    { locationname: "All", applocationid: 0 }, // Add your new object here
  ];
  console.log("fetchAppLocations", updatedLocations);

  return updatedLocations;
};

// ==============================|| fetchRidersSummary (riders summary)||============================== //

export const fetchRidersSummary = async ({ queryKey }) => {
  console.log("queryKey for fetchRidersSummary", queryKey);
  const [appId, startdate, enddate] = queryKey;
  const response = await axios.get(
    appId == 0
      ? `${process.env.REACT_APP_URL}/deliveries/getridersummary/?&fromdate=${startdate}&todate=${enddate}`
      : `${process.env.REACT_APP_URL}/deliveries/getridersummary/?applocationid=${appId}&fromdate=${startdate}&todate=${enddate}`
  );
  console.log("fetchRidersSummary", response.data.details);

  return response.data.details;
};

// ==============================|| fetchorderdetails (orders detail)||============================== //
export const fetchorderdetails = async ({ queryKey }) => {
  console.log("queryKey of fetchorderdetails", queryKey);
  const [tabValue, appId, startdate, enddate] = queryKey;
  let status =
    tabValue === 0 ? "delivered" : tabValue === 1 ? "pending" : "cancelled";
  const response = await axios.get(
    appId == 0
      ? `https://jupiter.nearle.app/live/api/v1/orders/getorders/?fromdate=${startdate}&todate=${enddate}`
      : `https://jupiter.nearle.app/live/api/v1/orders/getorders/?fromdate=${startdate}&todate=${enddate}&applocationid=${appId}`
  );
  console.log("fetchorderdetails", response.data.details);

  return response.data.details;
};
// ==============================|| fetchCount (orders detail)||============================== //
export const fetchCount = async ({ queryKey }) => {
  console.log("queryKey of fetchCount", queryKey);
  const [startdate, enddate] = queryKey;
  const response = await axios.get(
    `https://jupiter.nearle.app/live/api/v1/orders/getorders/?fromdate=${startdate}&todate=${enddate}`
  );
  const calculateOrderCounts = () => {
    let deliveredCount = 0;
    let pendingCount = 0;
    let cancelledCount = 0;

    response.data.details.forEach((order) => {
      const status = order.orderstatus;
      if (status === "delivered") {
        deliveredCount++;
      } else if (status === "pending") {
        pendingCount++;
      } else if (status === "cancelled") {
        cancelledCount++;
      }
    });

    return { deliveredCount, pendingCount, cancelledCount };
  };
  console.log("fetchCount", calculateOrderCounts());

  return calculateOrderCounts();
};

// ==============================|| getallproducts (products)||============================== //

export const getallproducts = async (subcatid) => {
  try {
    const response = await axios.get(
      `${process.env.REACT_APP_URL}/products/getallproducts?categoryid=1001&subcategoryid=${subcatid}`
    );
    console.log("getallproducts", response.data.data.products);
    return response.data.data.products;
  } catch (err) {
    console.log("getallproducts", err);
    return err;
  }
};
// ==============================|| getproductcategories (products)||============================== //
export const getproductcategories = async () => {
  try {
    const response = await axios.get(
      `${process.env.REACT_APP_URL}/products/getproductcategories`
    );
    console.log("getproductcategories", response.data.details);
    return response.data.details;
  } catch (err) {
    console.log("getproductcategories", err);
    return err;
  }
};
// ==============================|| getproductsubcategories (products)||============================== //

export const getproductsubcategories = async () => {
  try {
    const response = await axios.get(
      `${
        process.env.REACT_APP_URL
      }/products/getproductsubcategories?categoryid=${1001}`
    );
    console.log("getproductsubcategories", response.data.details);
    return response.data.details;
  } catch (err) {
    console.log("getproductsubcategories", err);
    return err;
  }
};

// ==============================|| productCreate (products)||============================== //

export const productCreate = async (props) => {
  try {
    const response = await axios.post(
      `${process.env.REACT_APP_URL}/products/create`,
      { ...props, tenantid: 1 }
    );
    console.log("productsUpdate", response.data.data);
    return response.data.data;
  } catch (err) {
    console.log("productsUpdate", err);
    return err;
  }
};

// ==============================|| productsUpdate (products)||============================== //

export const productsUpdate = async (props) => {
  try {
    const response = await axios.put(
      `${process.env.REACT_APP_URL}/products/update`,
      props
    );
    console.log("productsUpdate", response.data.data);
    return response.data.data;
  } catch (err) {
    console.log("productsUpdate", err);
    return err;
  }
};
// ==============================|| productsDelete (products)||============================== //

export const productsDelete = async (props) => {
  try {
    const response = await axios.delete(
      `${process.env.REACT_APP_URL}/products/delete?productid=${props.productid}`,
      props
    );
    console.log("productsDelete", response.data.data);
    return response.data.data;
  } catch (err) {
    console.log("productsDelete", err);
    return err;
  }
};
// ==============================|| getProductUnits (products)||============================== //

export const getProductUnits = async () => {
  try {
    const response = await axios.get(
      `${process.env.REACT_APP_URL}/utils/getapptypes/?tag=uom`
    );
    console.log("productUnits", response.data.details);
    return response.data.details;
  } catch (err) {
    console.log("productUnits", err);
    return err;
  }
};
