import { Injectable } from "@nestjs/common";
import { DataSource } from "typeorm";
import { ClinicalNotesWithAbha } from "./entities/clinical-notes-with-abha.entity";
import axios from "axios";
@Injectable()
export class ClinicalNotesWithAbhaService {
  constructor(private readonly connection: DataSource,
  ) { }
  async create(clinicalNotes: ClinicalNotesWithAbha) {
    if (!clinicalNotes.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 = ?`, [clinicalNotes.hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-clinical-notes-with-abha`;
      const check_old = await axios.post(apiURL, clinicalNotes)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAll(opd_id: any, hospital_id: any) {

    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-clinical-notes-with-abha?opd_id=${opd_id}&hospital_id=${hospital_id}`;
      const check_old = await axios.get(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async update(clinicalNotes: any) {

    if (!clinicalNotes.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 = ?`, [clinicalNotes.hospital_id])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-clinical-notes-with-abha`;
      const check_old = await axios.patch(apiURL, clinicalNotes)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async delCompBasic(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/chief-complaint-basic?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
  async delCompDetail(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/chief-complaint-detail?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async delTreatHis(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/past-treatment-history?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async delTreatHisDocs(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/past-treatment-history-documents?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async delTreatmentAdvice(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/treatment-advice?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
  async delDiagReport(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/diagnosis-report?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
  async delDietPlan(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/diet-plan?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
  async delFollowUp(id: any, hospital_id: any) {
    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-clinical-notes-with-abha/follow-up?id=${id}&hospital_id=${hospital_id}`;
      const check_old = await axios.delete(apiURL)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
}
