import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { DataSource } from 'typeorm';
@Injectable()
export class GetPatDetailsByAayushUniqueIdService {
  constructor(private readonly connection: DataSource
  ) { }

  async findAll(aayush_unique_id: string, hospital_id: number) {
    if (!hospital_id) {
      return {
        "status": "failed",
        "messege": "enter hospital_id to post clinical notes"
      }
    }
    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-get-patient-details-by-aayush-unique-id`;
      let body = {
        "aayush_unique_id": aayush_unique_id,
        "hospital_id": hospital_id
      }
      const check_old = await axios.post(apiURL, body)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }

  }
  async findAllAbhaAddress(aayush_unique_id: string, hospital_id: number, abhaAddress: string) {
    if (abhaAddress.trim() == "") {
      return {
        "status": "failed",
        "message": "Abha address cannot be empty",
      }
    }
    if (!hospital_id) {
      return {
        "status": "failed",
        "messege": "enter hospital_id to post clinical notes"
      }
    }
    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-get-patient-details-by-aayush-unique-id/abhaAddress`;
      let body = {
        "aayush_unique_id": aayush_unique_id,
        "hospital_id": hospital_id,
        "abhaAddress": abhaAddress
      }
      const check_old = await axios.post(apiURL, body)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
  async checkAbhaAddress(abhaAddress: string, hospital_id: number) {
    if (abhaAddress.trim() == "") {
      return {
        "status": "failed",
        "message": "Abha address cannot be empty",
      }

    }
    if (!hospital_id) {
      return {
        "status": "failed",
        "messege": "enter hospital_id to post clinical notes"
      }
    }
    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-get-patient-details-by-aayush-unique-id/check/abhaAddress`;
      let body = {
        "hospital_id": hospital_id,
        "abhaAddress": abhaAddress
      }
      const check_old = await axios.post(apiURL, body)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }



  async checkAbhaNumberAndAddress(abhaAddress: string, ABHANumber: string, hospital_id: number) {
    if (abhaAddress?.trim() == "") {
      return {
        "status": "failed",
        "message": "Abha address cannot be empty",
      }

    }
    if (!hospital_id) {
      return {
        "status": "failed",
        "messege": "enter hospital_id to post clinical notes"
      }
    }
    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-get-patient-details-by-aayush-unique-id/check/abhaNumber/abhaAddress`;
      let body = {
        "abhaNumber": ABHANumber,
        "hospital_id": hospital_id,
        "abhaAddress": abhaAddress
      }
      const check_old = await axios.post(apiURL, body)
      return check_old.data
    } catch (error) {
      console.log(error,"errr");
      
      throw new Error(error);
    }
  }
}