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) {
    if (hospital_id) {
      try {
        const [base_url] = await this.connection.query(
          `select phr_api_base_url from hospitals where plenome_id = ?`,
          [hospital_id]
        );
        const result = await axios.get(
          `${base_url.phr_api_base_url}/phr-consultation-process?hospital_id=${hospital_id}`
        );
        return result.data;
      } catch (error) {
        return error;
      }
    } else {
      return {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.APPT_ID_TO_GET_VAL_ERR
      };
    }
  }




  async findAllProcessList(appointment_id: number) {
    if (appointment_id) {
      try {
        const [hospital_id] = await this.connection.query(
          `select Hospital_id from appointment where id = ?`,
          [appointment_id]
        );
        const [base_url] = await this.connection.query(
          `select phr_api_base_url from hospitals where plenome_id = ?`,
          [hospital_id.Hospital_id]
        );
        const result = await axios.get(
          `${base_url.phr_api_base_url}/phr-consultation-process/process?appointment_id=${appointment_id}`
        );
        return result.data;
      } catch (error) {
        return error;
      }
    } else {
      return {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.APPT_ID_TO_GET_VAL_ERR
      };
    }
  }



  async findAllProcessTrackList(appointment_id: string) {
    if (appointment_id) {
      try {
        const [hospital_id] = await this.connection.query(
          `select Hospital_id from appointment where id = ?`,
          [appointment_id]
        );
        const [base_url] = await this.connection.query(
          `select phr_api_base_url from hospitals where plenome_id = ?`,
          [hospital_id.Hospital_id]
        );
        const result = await axios.get(
          `${base_url.phr_api_base_url}/phr-consultation-process/tracking?appointment_id=${appointment_id}`
        );
        return result.data;
      } catch (error) {
        return error;
      }
    } else {
      return {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.APPT_ID_TO_GET_VAL_ERR
      };
    }

  }







  async ProcessTrackHistory(appointment_id: number) {
    if (appointment_id) {

      const [getHosId] = await this.connection.query(` select * from appointment where id = ? `, [appointment_id])
      if (getHosId.appointment_status_id > 4) {
        try {
          const [get_base_url] = await this.connection.query(`select phr_api_base_url from hospitals where plenome_id = ?`, [getHosId.Hospital_id])
          const response = await axios.get(`${get_base_url.phr_api_base_url}/phr-consultation-process/process_history?appointment_id=${appointment_id}`)
          return response.data
        } catch (error) {

          return error;
        }
      } else {
        return {
          "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
          "message": process.env.APPT_INPROGRESS_STATUS_ERROR
        }
      }
    } else {
      return {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.APPT_ID_TO_GET_VAL_ERR
      };
    }
  }




  async gettrackstats(appointment_id: string) {
    if (appointment_id) {
      const [getHosId] = await this.connection.query(`select * from appointment where id = ?`, [appointment_id])
      try {
        const [get_base_url] = await this.connection.query(`select phr_api_base_url from hospitals where plenome_id = ?`, [getHosId.Hospital_id])
        const response = await axios.get(`${get_base_url.phr_api_base_url}/phr-consultation-process/stats?appointment_id=${appointment_id}`)
        return response.data
      } catch (error) {

        return error;
      }
    } else {
      return {
        status: process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        message: process.env.APPT_ID_TO_GET_VAL_ERR
      };
    }

  }





  async checkbooked(updateconsultation: Consultation) {
    if (!updateconsultation.appointment_id) {
      return [{
        "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        "message": process.env.APPT_ID_UPDATE_PROCESS
      }];
    }
    const [getApptDetails] = await this.connection.query(` select * from appointment where id = ? `, [updateconsultation.appointment_id])
    if (getApptDetails.appointment_status_id >= 3 && getApptDetails.appointment_status_id != 4) {
      try {
        const [get_base_url] = await this.connection.query(`select phr_api_base_url from hospitals where plenome_id = ?`, [getApptDetails.Hospital_id])
        const response = await axios.post(`${get_base_url.phr_api_base_url}/phr-consultation-process/check`, updateconsultation)
        return response.data
      } catch (error) {
        return [{
          "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
          "message": process.env.APPT_ERR_IP_PROCESS_UPDATE
        }];
      }
    } else {
      return {
        "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        "message": process.env.APPT_INPROGRESS_STATUS_ERROR
      }
    }
  }








  async createWaiting(updateconsultation: Consultation) {
    if (!updateconsultation.appointment_id) {
      return [{
        "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        "message": process.env.APPT_ID_UPDATE_PROCESS
      }];
    }

    const [getApptDetails] = await this.connection.query(` select * from appointment where id = ? `, [updateconsultation.appointment_id])
    if (getApptDetails.appointment_status_id > 4) {

      const [getApptDetails] = await this.connection.query(` select * from appointment where id = ? `, [updateconsultation.appointment_id])
      try {
        const [get_base_url] = await this.connection.query(`select phr_api_base_url from hospitals where plenome_id = ?`, [getApptDetails.Hospital_id])
        const response = await axios.post(`${get_base_url.phr_api_base_url}/phr-consultation-process`, updateconsultation)
        return response.data
      } catch (error) {
        return [{
          "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
          "message": process.env.PROCESS_UPDATE_ERROR
        }];
      }
    } else {
      return {
        "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
        "message": process.env.APPT_INPROGRESS_STATUS_ERROR
      }
    }
  }


  async findAppointment(patient_id: number, hospital_id: number) {

    const getTodayAppointment = await this.connection.query(` 
  SELECT
appointment.id PHR_APPOINTMENT_ID,
concat("APPN",appointment.hos_appointment_id) appointment_id,
staff.id AS doctor_id,
  CONCAT(staff.name, ' ', staff.surname) AS doctor_name,
  staff.image,
  hospitals.plenome_id AS hospital_id,
  hospitals.hospital_name,
      hospitals.lattitude,
    hospitals.longitude,
  hospitals.address,
      appointment.date,
  appointment.time,
  appointment.appointment_status,
  appointment.appointment_status_id,
  appointment_status.color_code,
  patients.id AS patient_id,
  GROUP_CONCAT(specialist.specialist_name) AS specialist_names
FROM
  appointment
LEFT JOIN staff ON staff.id = appointment.doctor
LEFT JOIN hospital_staffs ON hospital_staffs.staff_id = staff.id
LEFT JOIN hospitals ON hospitals.plenome_id = appointment.Hospital_id
LEFT JOIN patients ON patients.id = appointment.patient_id
LEFT JOIN appointment_status ON appointment_status.id = appointment.appointment_status_id
LEFT JOIN specialist ON 
  IF(
      JSON_VALID(staff.specialist) AND JSON_CONTAINS(staff.specialist, CAST(specialist.id AS JSON)),
      1,
      0
  ) 
WHERE
  patients.id = ? and appointment.date = date(now()) 
  and appointment.is_deleted = 0 and appointment.Hospital_id = ?
  and appointment.appointment_status_id = 5 
GROUP BY
PHR_APPOINTMENT_ID, doctor_id, doctor_name, hospital_id, hospital_name, date, time, appointment_status, patient_id,appointment_id `, [
      patient_id, hospital_id
    ])

    return getTodayAppointment

  }


}