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

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

  async create(createPrescriptionDto: Prescription) {
    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 bookAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-prescription/post`, createPrescriptionDto)
      return bookAppointment.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAll(createPrescriptionDto: Prescription) {
    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 bookAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-prescription/get`, createPrescriptionDto)
      return bookAppointment.data
    } catch (error) {
      throw new Error(error);
    }
  }


}
