import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { UnlinkAbhaFromPatient } from './entities/unlink-abha-from-patient.entity';
import axios from 'axios';

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

  async create(createUnlinkAbhaFromPatientDto: UnlinkAbhaFromPatient) {
    if (!createUnlinkAbhaFromPatientDto.hospital_id) {
      return {
        "status": "failed",
        "message": "hospital_id is required"
      }
    }
    if (!createUnlinkAbhaFromPatientDto.patient_id) {
      return {
        "status": "failed",
        "message": "patient_id is required"
      }
    }
    if (!createUnlinkAbhaFromPatientDto.abhaAddress) {
      return {
        "status": "failed",
        "message": "abhaAddress is required"
      }
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createUnlinkAbhaFromPatientDto.hospital_id])
      const getAppointment = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-unlink-abha-from-patient`, createUnlinkAbhaFromPatientDto)
      return getAppointment.data
    } catch (error) {
      console.log(error, "error");
      throw new Error(error);
    }
  }
}
