import { Injectable } from "@nestjs/common";
import { CreateBundleGenerationDto } from "./dto/create-bundle-generation.dto";
import { UpdateBundleGenerationDto } from "./dto/update-bundle-generation.dto";
import { DataSource } from "typeorm";
import axios from "axios";

@Injectable()
export class BundleGenerationService {
  constructor(private connection: DataSource) { }

  async create(
    followupDetails: any,
    other_observations: any,
    Lifestyle: any,
    women_health: any,
    generalAssment: any,
    physicalActivity: any,
    vitalDetails: any,
    body_measurement: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: any,
    file: string
  ) {
    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 upload_doc_Req_body = {
        followupDetails: followupDetails,
        other_observations: other_observations,
        Lifestyle: Lifestyle,
        women_health: women_health,
        generalAssment: generalAssment,
        physicalActivity: physicalActivity,
        vitalDetails: vitalDetails,
        body_measurement: body_measurement,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        file: file,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/wellness`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }
  async createDischargeSummary(
    followupDetails: any,
    clinicalPresentation: any,
    investigationsPerformed: any,
    treatment_given: any,
    condition_at_discharge: any,
    discharge_advice: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: 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 upload_doc_Req_body = {
        followupDetails: followupDetails,
        clinicalPresentation: clinicalPresentation,
        investigationsPerformed: investigationsPerformed,
        treatment_given: treatment_given,
        condition_at_discharge: condition_at_discharge,
        discharge_advice: discharge_advice,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/discharge_summary`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }
  async createPrescription(
    prescriptionDetails: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: any,
    file: string
  ) {
    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 upload_doc_Req_body = {
        prescriptionDetails: prescriptionDetails,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        file: file,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/prescription`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }
  async createOpConsultation(
    chiefComplaintsBasic: any,
    getPastTreatHis: any,
    followupDetails: any,
    hospital_id: any,
    opd_id: any,
    file: any,
    abhaAddress: any,
    diet_plan: any,
    treatmentAdvice: any,
    physicalExam: any,
    allergies: any,
    familyHistory: any,
    prescriptionDetails: any,
    investigationAdvice: any
  ) {
    if (!hospital_id) {
      return {
        status: "failed",
        messege: "enter hospital_id to post clinical notes",
      };
    }
    try {
      console.log(hospital_id, "hospital_id");

      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      let upload_doc_Req_body = {
        chiefComplaintsBasic: chiefComplaintsBasic,
        getPastTreatHis: getPastTreatHis,
        followupDetails: followupDetails,
        filename: file,
        diet_plan: diet_plan,
        treatmentAdvice: treatmentAdvice,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        physicalExam: physicalExam,
        allergies: allergies,
        familyHistory: familyHistory,
        prescriptionDetails: prescriptionDetails,
        investigationAdvice: investigationAdvice,
      };

      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/opConsultation`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }
  async createDiagnosis(
    prescriptionDetails: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: any,
    file: 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 upload_doc_Req_body = {
        diagnosisDetails: prescriptionDetails,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        file: file,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/diagnosis_report`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async createInvoice(
    prescriptionDetails: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: 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 upload_doc_Req_body = {
        invoiceDetails: prescriptionDetails,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/invoiceDetails`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async createLabresults(
    prescriptionDetails: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: any,
    file: string
  ) {
    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 upload_doc_Req_body = {
        labResults: prescriptionDetails,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        file: file,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/labresults`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async createImmunization(
    prescriptionDetails: any,
    hospital_id: any,
    opd_id: any,
    abhaAddress: any,
    file: string
  ) {
    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 upload_doc_Req_body = {
        immunizationDetails: prescriptionDetails,
        hospital_id: hospital_id,
        opd_id: opd_id,
        abhaAddress: abhaAddress,
        file: file,
      };
      const upload_doc = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/immunization`,
        upload_doc_Req_body
      );
      return upload_doc.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAll(transaction_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]
      );
      console.log(getBaseURL, "getBaseURL");

      const upload_doc = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-bundle-generation/get-charge-details?transaction_id=${transaction_id}`,

      );
      return upload_doc.data;
    } catch (error) {

      throw new Error(error);
    }
  }
}
