import { BadRequestException, Injectable } from "@nestjs/common";
import { ABHAProfile, Patientprofile } from "./entities/patient-profile.entity";
import { DataSource } from "typeorm";
import axios from "axios";

@Injectable()
export class PatientprofileService {
  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 getPatientDetails(patientId: number, Hospital_id: number) {
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile?patientId=${patientId}&hospital_id=${Hospital_id}`
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async updatePatientDetails(
    patientId: number,
    Entity: Patientprofile,
    Hospital_id: number
  ) {
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    if (this.containsHtml(Entity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile?patientId=${patientId}&hospital_id=${Hospital_id}`,
        Entity
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async updatePatientABHAaddress(
    patientId: number,
    Entity: Patientprofile,
    Hospital_id: number
  ) {
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    if (this.containsHtml(Entity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile/abha-address?patientId=${patientId}&hospital_id=${Hospital_id}`,
        Entity
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async updatePatientABHANumber(
    patientId: number,
    Entity: ABHAProfile,
    Hospital_id: number
  ) {
    if (this.containsHtml(Entity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile/abha-number?patientId=${patientId}&hospital_id=${Hospital_id}`,
        Entity
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async updatePatientVerifyABHANumber(
    patientId: number,
    Entity: ABHAProfile,
    Hospital_id: number
  ) {
    if (this.containsHtml(Entity)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile/verify-abha-number?patientId=${patientId}&hospital_id=${Hospital_id}`,
        Entity
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async getRegistrationStatus(abhaDetails: any, hospital_id: number) {
    if (!hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    if (this.containsHtml(abhaDetails)) {
      throw new BadRequestException(
        'HTML tags are not allowed in request payload',
      );
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const bookAppointment = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile/get-registration-status?hospital_id=${hospital_id}`,
        abhaDetails
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async updateOnlyABHANumber(
    aaysh_unique_id: any,
    abhaNumber: any,
    Hospital_id: number
  ) {
    if (!Hospital_id) {
      return [
        {
          status: "failed",
          message: "Enter hospital_id to get patient details",
        },
      ];
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Hospital_id]
      );
      const bookAppointment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-patient-profile/update-abha-number?aayush_unique_id=${aaysh_unique_id}&abhaNumber=${abhaNumber}`
      );
      return bookAppointment.data;
    } catch (error) {
      throw new Error(error);
    }
  }
}
