import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { DataSource } from 'typeorm';

@Injectable()
export class PatientAppointmentListService {
  constructor(
    private readonly connection: DataSource,
  ) { }




  async findAll(Hospital_id: number, patient_id: number, date: any, doctor: number) {
    if (!Hospital_id) {
      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/history?hospital_id=${Hospital_id}&patient_id=${patient_id}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_patient_appointmentlist = await axios.get(apiURL)
      return findall_patient_appointmentlist.data
    } catch (error) {
      throw new Error(error);

    }
  }



  async findAllV2(Hospital_id: number, patient_id: number, date: any, doctor: number,limit: number, page: number) {
    if (!Hospital_id) {
      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/v2/history?hospital_id=${Hospital_id}&patient_id=${patient_id}&limit=${limit}&page=${page}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_patient_appointmentlist = await axios.get(apiURL)
      return findall_patient_appointmentlist.data
    } catch (error) {
      console.log(error, "error");
      
      throw new Error(error);

    }
  }







  async findAllUpcomming(Hospital_id: number, patient_id: number, date: any, doctor: number) {
    if (!Hospital_id) {
      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/upcoming?hospital_id=${Hospital_id}&patient_id=${patient_id}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_upcoming_appointmentlist = await axios.get(apiURL)
      return findall_upcoming_appointmentlist.data
    } catch (error) {
      throw new Error(error)
    }


  }






  async findAllUpcommingV2(Hospital_id: number, patient_id: number, date: any, doctor: number,limit: number, page: number) {
    if (!Hospital_id) {
      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/v2/upcoming?hospital_id=${Hospital_id}&patient_id=${patient_id}&limit=${limit}&page=${page}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_upcoming_appointmentlist = await axios.get(apiURL)
      return findall_upcoming_appointmentlist.data
    } catch (error) {
      throw new Error(error)
    }


  }









  async findAllToday(Hospital_id: number, patient_id: number, date: any, doctor: number) {
    if (!Hospital_id) {

      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"

      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/today?hospital_id=${Hospital_id}&patient_id=${patient_id}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_alltoday_appointmentlist = await axios.get(apiURL)
      return findall_alltoday_appointmentlist.data
    } catch (error) {
      throw new Error(error)

    }

  }



  async findAllTodayV2(Hospital_id: number, patient_id: number, date: any, doctor: number,limit: number, page: number) {
    if (!Hospital_id) {

      return {
        "status": "failed",
        "message": "enter hospital_id to get the values"

      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [Hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-patient-appointment-list/v2/today?hospital_id=${Hospital_id}&patient_id=${patient_id}&limit=${limit}&page=${page}`
      if (date) {
        apiURL += `&date=${date}`
      }
      if (doctor) {
        apiURL += `&doctor=${doctor}`
      }
      const findall_alltoday_appointmentlist = await axios.get(apiURL)
      return findall_alltoday_appointmentlist.data
    } catch (error) {
      throw new Error(error)

    }

  }
}