import { Injectable } from '@nestjs/common';
import { CheckForDuplicateAppointment } from './entities/check-for-duplicate-appointment.entity';
import { DataSource } from 'typeorm';

@Injectable()
export class CheckForDuplicateAppointmentService {
  constructor(private readonly connection: DataSource,
  ) { }
  async create(AppointmentEntity: CheckForDuplicateAppointment) {
    if (AppointmentEntity.Hospital_id) {
      try {
        const checkDuplicate = await this.connection.query(`select * from appointment 
        where patient_id = ? and doctor = ? and shift_id = ? and date = ? and appointment_status_id <> 4`, [
          AppointmentEntity.patient_id,
          AppointmentEntity.doctor,
          AppointmentEntity.shift_id,
          AppointmentEntity.date
        ])
        if (checkDuplicate.length > 0) {
          return {
            "status": process.env.SUCCESS_STATUS_ABHA_ADDRESS_MAPPING,
            "message": process.env.DUPLICATE_APPT_ERROR,
            "is_duplicate_appointment": true
          }
        } else {
          return {
            "status": process.env.SUCCESS_STATUS_ABHA_ADDRESS_MAPPING,
            "message": process.env.CAN_BOOK_APPT_MESSAGE,
            "is_duplicate_appointment": false
          }
        }
      } catch (error) {
        return {
          "status": process.env.FAILED_STATUS_ABHA_ADDRESS_MAPPING,
          "message": "unable to check duplicate appointment",
          "error": error
        }
      }
    } else {
      return {
        "status": "failed",
        "message": process.env.DUP_CHECK_HOS_ID_MISSING_ERROR
      }
    }


  }

}
