import { axiosInstance } from "../config/api";
import { CLINIC_TYPE } from "../constants";
import { clinicsData, registeredClinicsData } from "../mock/clinics";

export const getClinics = (params) => {
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination?.pageIndex + 1 ?? 1);
  searchParams.append("filter_type", params?.type ?? CLINIC_TYPE.REGISTERED);
  searchParams.append("search", params?.globalFilter ?? "");

  let url = `/clinics/?${searchParams.toString()}`;

  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getLatestClinicId = () => {
  const url = `/auth/clinic/latest-id`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getClinicsById = (id) => {
  const url = `/clinics/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const generatePaymentSession = () => {
  const url = `/stripe/create-payment-session`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateClinicStatus = (data) => {
  const url = `/admin/clinic/status`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getClinicDocs = (id) => {
  const url = `/clinics/verified-files/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateClinic = (id, data) => {
  const url = `/clinics/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getClinicsDashboardStatsById = () => {
  const url = `/companies/dashboard-stats`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const createClinicOffer = (data) => {
  const url = `/admin/clinic/offer`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getClinicOffer = (params) => {
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination.pageIndex + 1 ?? 1);
  searchParams.append("search", params?.globalFilter ?? "");

  const url = `/admin/clinic/offers?${searchParams.toString()}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateClinicOffer = (data, id) => {
  const url = `/admin/clinic/offer/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const deleteClinicOffer = (id) => {
  const url = `/admin/clinic/offer/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .delete(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getClinicDoctors = (params) => {
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination.pageIndex + 1 ?? 1);
  searchParams.append("search", params?.globalFilter ?? "");

  const url = `/clinic-doctors?${searchParams.toString()}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const createDoctor = (data) => {
  const url = `/clinic-doctors/`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const updateDoctor = (data, id) => {
  const url = `/clinic-doctors/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .put(url, data)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const deleteDoctor = (id) => {
  const url = `/clinic-doctors/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .delete(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const getCallTranscripts = (params) => {
  let searchParams = new URLSearchParams();
  searchParams.append("limit", params?.pagination?.pageSize ?? 10);
  searchParams.append("page", params?.pagination.pageIndex + 1 ?? 1);
  searchParams.append("search", params?.globalFilter ?? "");
  if(params?.startDate){
    searchParams.append("startDate", params.startDate.toISOString());
  }
  if(params?.endDate){
    searchParams.append("endDate", params.endDate.toISOString());
  }

  if (params?.sortingString) {
    const sortingParams = new URLSearchParams(params.sortingString);
    sortingParams.forEach((value, key) => {
      searchParams.append(key, value);
    });
  }

  const url = `/call-transcripts?${searchParams.toString()}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const downloadCallTranscript = (id) => {
  const url = `/call-transcripts/${id}`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .get(url)
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};

export const downloadBulkCallTranscripts = (data) => {
  const url = `/call-transcripts/bulk-download`;
  return new Promise((resolve, reject) => {
    axiosInstance
      .post(url, data, {
        responseType: "blob",
        transitional: {
          forcedJSONParsing: false,
        },
      })
      .then((response) => resolve(response))
      .catch((err) => reject(err));
  });
};
