import { Injectable } from '@nestjs/common';
import { ClinicalNote } from './entities/clinical-note.entity';
import { DataSource } from 'typeorm';
import axios from 'axios';

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

  async create(createPrescriptionDto: ClinicalNote) {
    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])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-clinical-notes/post`;
      const check_old = await axios.post(apiURL, createPrescriptionDto)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAll(createPrescriptionDto: ClinicalNote) {
    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])
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-clinical-notes/get`;
      const check_old = await axios.post(apiURL, createPrescriptionDto)
      return check_old.data
    } catch (error) {
      throw new Error(error);
    }
  }
}
