import { Injectable } from '@nestjs/common';
import { Vital } from './entities/vital.entity';
import { DataSource } from 'typeorm';
import axios from 'axios';

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

  async create(createPrescriptionDto: Vital) {


    if (!createPrescriptionDto.Hospital_id) {
      return {
        "status": "failed",
        "message": "Enter hospital_id to upload prescription"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.Hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/post`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }

  async findAll(createPrescriptionDto: Vital) {
    if (!createPrescriptionDto.Hospital_id) {
      return {
        "status": "failed",
        "message": "Enter hospital_id to get prescription"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.Hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/get`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }

  }

  async postVitals(createPrescriptionDto: any) {
    if (!createPrescriptionDto.hospital_id) {
      return {
        status: "failed",
        message: "hospital_id is required",
        status_code: 400
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/create-vitals`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }


  async getVitals(createPrescriptionDto: any) {
    if (!createPrescriptionDto.hospital_id) {
      return {
        status: "failed",
        message: "hospital_id is required",
        status_code: 400
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/get-vitals`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }


  async updateVitals(createPrescriptionDto: any) {
    if (!createPrescriptionDto.hospital_id) {
      return {
        status: "failed",
        message: "hospital_id is required",
        status_code: 400
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/update-vitals`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }


  async deleteVitals(createPrescriptionDto: any) {
    if (!createPrescriptionDto.hospital_id) {
      return {
        status: "failed",
        message: "hospital_id is required",
        status_code: 400
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPrescriptionDto.hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-vitals/delete-vitals`, createPrescriptionDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }

}
