import { BadRequestException, Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { Patient, PatientFromQr } from './entities/patient_from_qr.entity';
import axios from 'axios';

@Injectable()
export class PatientFromQrService {

  constructor(private readonly connection: DataSource
  ) { }
  containsHtml(value: any): boolean {
    // Case 1: String → check for HTML
    if (typeof value === 'string') {
      return /<[^>]*>/.test(value);
    }

    // Case 2: Array → check each item
    if (Array.isArray(value)) {
      return value.some(item => this.containsHtml(item));
    }

    // Case 3: Object → check each field
    if (typeof value === 'object' && value !== null) {
      return Object.values(value).some(val => this.containsHtml(val));
    }

    // Case 4: Other types (number, boolean, null)
    return false;
  }
  async create(createPatientFromQr: PatientFromQr) {
    if (this.containsHtml(createPatientFromQr)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    if (!createPatientFromQr.hospital_id) {
      return [{ "message": "Enter hospital_id" }]
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [createPatientFromQr.hospital_id])
      const create_qr = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-patient-from-qr/scan`, createPatientFromQr)
      return create_qr.data
    } catch (error) {
      throw new Error(error)
    }
  }

  async CreateManually(PatientEntity: Patient) {
    if (this.containsHtml(PatientEntity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    if (!PatientEntity.dial_code) {
      PatientEntity.dial_code = '91'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'm') {
      PatientEntity.gender = 'Male'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'f') {
      PatientEntity.gender = 'Female'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'o') {
      PatientEntity.gender = 'Others'
    }
    if (!PatientEntity.hospital_id) {
      return [{
        status: "failed",
        "message": "Enter hospital_id"
      }]
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [PatientEntity.hospital_id])
      const create_qr = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-patient-from-qr/AddNewPatient`, PatientEntity)
      console.log(create_qr.data, "create_qr.data");

      return create_qr.data
    } catch (error) {
      console.log(error, "error");

      throw new Error(error)
    }

  }


  async checkDetails(PatientEntity: Patient) {
    if (this.containsHtml(PatientEntity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    if (!PatientEntity.dial_code) {
      PatientEntity.dial_code = '91'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'm' || PatientEntity.gender.toLocaleLowerCase() == 'male') {
      PatientEntity.gender = 'Male'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'f' || PatientEntity.gender.toLocaleLowerCase() == 'female') {
      PatientEntity.gender = 'Female'
    }
    if (PatientEntity.gender.toLocaleLowerCase() == 'o' || PatientEntity.gender.toLocaleLowerCase() == 'others') {
      PatientEntity.gender = 'Others'
    }
    if (!PatientEntity.hospital_id) {
      return [{
        status_code: 400,
        status: "Failed",
        message: "Enter hospital_id"
      }]
    }
    try {
      const [getBaseURL] = await this.connection.query(`select op_hub_base_url from hospitals where plenome_id = ?`, [PatientEntity.hospital_id])
      const create_qr = await axios.post(`${getBaseURL.op_hub_base_url}/op-hub-patient-from-qr/checkExistingPatient`, PatientEntity)
      console.log(create_qr.data, "create_qr.data");

      return create_qr.data
    } catch (error) {
      console.log(error, "error");

      throw new Error(error)
    }

  }
}