import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { Consultation } from './entities/consultation.entity';
import axios from 'axios';

@Injectable()
export class ConsultationStatusService {
  post(arg0: null, updateConsultation: Consultation): any {
    throw new Error('Method not implemented.');
  }

  constructor(private readonly connection: DataSource,
  ) { }

  async findAllColorCode(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [hospital_id])
      const find_all_color_codes = await axios.get(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process?hospital_id=${hospital_id}`)
      return find_all_color_codes.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAllProcessList(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [hospital_id])
      const find_all_process_list = await axios.get(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process/process?hospital_id=${hospital_id}`)
      return find_all_process_list.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAllProcessTrackList(hospital_id: number, appointment_id: string) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [hospital_id])
      const find_all_process_track_list = await axios.get(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process/tracking?hospital_id=${hospital_id}&appointment_id=${appointment_id}`)
      return find_all_process_track_list.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async update(updateconsultation: Consultation) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [updateconsultation.hospital_id])
      const update_consultation = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process`, updateconsultation)
      return update_consultation.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async ProcessTrackHistory(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [hospital_id])
      const process_track_history = await axios.get(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process/process-history?hospital_id=${hospital_id}`)
      return process_track_history.data
    } catch (error) {
      throw new Error(error);
    }
  }

  async gettrackstats(hospital_id: number, appointment_id: string) {
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [hospital_id])
      const get_track_stats = await axios.get(`${getBaseURL.op_hub_base_url}/op-hub-consultation-process/stats?hospital_id=${hospital_id}&appointment_id=${appointment_id}`)
      return get_track_stats.data
    } catch (error) {
      throw new Error(error);
    }
  }


}
