import { Injectable } from "@nestjs/common";
import {
  AddCharge,
  makepayment,
  UpdateCharge,
  makepaymentV3,
} from "./entities/billing.entity";
import { DataSource } from "typeorm";
import axios from "axios";

@Injectable()
export class BillingService {
  constructor(private readonly connection: DataSource) { }

  async create(Entity: AddCharge) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const Addcharges = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing`,
        Entity
      );
      return Addcharges.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findAll(
    hospital_id: number,
    from_date: any,
    to_date: any,
    patient_id: any,
    payment_method: any,
    date: any
  ) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );

      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-billing?hospital_id=${hospital_id}`;
      if (from_date) {
        apiURL += `&from_date=${from_date}`;
      }
      if (to_date) {
        apiURL += `&to_date=${to_date}`;
      }
      if (date) {
        apiURL += `&date=${date}`;
      }
      if (patient_id) {
        apiURL += `&patient_id=${patient_id}`;
      }
      if (payment_method) {
        apiURL += `&payment_method=${payment_method}`;
      }
      const findAllBilling = await axios.get(apiURL);
      return findAllBilling.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async getInvoice(transaction_id: any, hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const get_invoice = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/getInvoice?hospital_id=${hospital_id}&transaction_id=${transaction_id}`
      );
      return get_invoice.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findonePatCharge(hospital_id: number, pat_charge_id: any) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_one_pat_charge = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/getOnePatCharge?hospital_id=${hospital_id}&patient_charge_id=${pat_charge_id}`
      );
      return find_one_pat_charge.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findOne(patient_id: number, hospital_id: number, filter: string) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_one = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/individualpending?hospital_id=${hospital_id}&patient_id=${patient_id}&filter=${filter}`
      );
      return find_one.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findpending(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_pending = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/pending?hospital_id=${hospital_id}`
      );
      return find_pending.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findChargeType(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_charge_type = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/chargeType?hospital_id=${hospital_id}`
      );
      return find_charge_type.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findChargeCategory(hospital_id: number, charge_type_id: any) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_charge_category = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/chargeCategory?hospital_id=${hospital_id}&charge_type_id=${charge_type_id}`
      );
      return find_charge_category.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findChargeName(hospital_id: number, charge_category_id: any) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_charge_name = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/chargeName?hospital_id=${hospital_id}&charge_category_id=${charge_category_id}`
      );
      return find_charge_name.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findChargeDetails(id: any, hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_charge_details = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/chargeName/${id}?hospital_id=${hospital_id}`
      );
      return find_charge_details.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async makePayment(Entity: makepayment) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const make_payment = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/makePayment`,
        Entity
      );
      console.log(make_payment, "make_payment");

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

      throw new Error(error);
    }
  }

  async update(id: number, Entity: UpdateCharge) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const update_payment = await axios.patch(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/${id}`,
        Entity
      );
      return update_payment.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  //V2 API's

  async V2findAll(
    hospital_id: number,
    from_date: any,
    to_date: any,
    patient_id: any,
    payment_method: any,
    date: any,
    limit: number,
    page: number
  ) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );

      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-billing/v2/paid?hospital_id=${hospital_id}&limit=${limit}&page=${page}`;
      if (from_date) {
        apiURL += `&from_date=${from_date}`;
      }
      if (to_date) {
        apiURL += `&to_date=${to_date}`;
      }
      if (date) {
        apiURL += `&date=${date}`;
      }
      if (patient_id) {
        apiURL += `&patient_id=${patient_id}`;
      }
      if (payment_method) {
        apiURL += `&payment_method=${payment_method}`;
      }
      const findAllBilling = await axios.get(apiURL);
      return findAllBilling.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async V2findpending(hospital_id: number, limit: number, page: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_pending = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/pending/v2?hospital_id=${hospital_id}&limit=${limit}&page=${page}`
      );
      return find_pending.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async getpendingApptofPat(hospital_id: number, patient_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const find_getpendingApptofPat = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/v2/getPendingSectionList?patient_id=${patient_id}`
      );
      return find_getpendingApptofPat.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async v2makePayment(Entity: makepayment) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const make_payment = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/makePayment/v2`,
        Entity
      );
      console.log(make_payment, "make_payment");

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

      throw new Error(error);
    }
  }

  async v2create(Entity: AddCharge) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const Addcharges = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/v2/addCharge`,
        Entity
      );
      return Addcharges.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async pendingbysectionID(
    hospital_id: number,
    patient_id: number,
    sectionId: any
  ) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const pendingbysectionID = await axios.get(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/individualpending/bySectionId?hospital_id=${hospital_id}&patient_id=${patient_id}&sectionId=${sectionId}`
      );
      return pendingbysectionID.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async v3makePayment(Entity: makepaymentV3) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [Entity.Hospital_id]
      );
      const make_payment = await axios.post(
        `${getBaseURL.op_hub_base_url}/op-hub-billing/makePayment/v3`,
        Entity
      );
      console.log(make_payment, "make_payment");

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

      throw new Error(error);
    }
  }
  async getAllCharges(hospital_id: number) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      const get_all_charges = await axios.get(
        `${getBaseURL.op_hub_base_url}/setup-hospital-charge-charges/v2/charges_list`
      );
      if (get_all_charges.data) {
        return {
          status: 'success',
          message: 'Charges list fetched successfully.',
          data: get_all_charges.data,
        }
      }
      return {
        status: 'failed',
        data: [],
      }

    } catch (error) {
      return {
        status: 'failed',
        data: [],
      }
    }
  }
}
