import { forwardRef, Inject, Injectable } from "@nestjs/common";
import { CreateOpHubNewBillingDto } from "./dto/create-op-hub-new-billing.dto";
import { UpdateOpHubNewBillingDto } from "./dto/update-op-hub-new-billing.dto";
import { DataSource } from "typeorm";
import axios from "axios";
import { CryptoService } from "src/qr-encrpyt/qr-encrpyt.service";
import { makepaymentV3 } from "./entities/op-hub-new-billing.entity";
import { DeductDiscountDto } from "./dto/deduct_discount.dto";

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

  async findAll(
    hospital_id: number,
    patient_id: string,
    payment_module: string,
    bill_type: string,
    limit: number,
    page: number,
    search_text?: string,
    appointment_id?: 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}/new-billing/billing-summary-bills?patient_id=${patient_id}&limit=${limit}&page=${page}`;
      if (payment_module) {
        apiURL += `&payment_module=${payment_module}`;
      }
      if (appointment_id) {
        apiURL += `&appointment_id=${appointment_id}`;
      }
      if (bill_type) {
        apiURL += `&bill_type=${bill_type}`;
      }
      if (search_text) {
        apiURL += `&searchText=${search_text}`;
      }

      //  if (appointment_id) {
      //   apiURL += `&appointment_id=${appointment_id}`;
      // }

      const getfindall = await axios.get(apiURL);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async findtransactiondetails(
    hospital_id: number,
    patient_id: number,
    payment_module: string,
    limit: number,
    page: number,
    appointment_id: 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}/new-billing/transaction-list?patient_id=${patient_id}&limit=${limit}&page=${page}`;
      if (payment_module) {
        apiURL += `&payment_module=${payment_module}`;
      }
      if (appointment_id) {
        apiURL += `&appointment_id=${appointment_id}`;
      }

      const getfindall = await axios.get(apiURL);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async findpatient_wallet(hospital_id: number, patient_id: string) {
    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}/new-billing/patient-wallet?patient_id=${patient_id}`;
      const getfindall = await axios.post(apiURL);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async coins_used(
    hospital_id: number,
    patient_id: string,
    coins_used: 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}/new-billing/coins-used?patient_id=${patient_id}&coins_used= ${coins_used}`;
      const getfindall = await axios.post(apiURL);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async makepaymentV3(hospital_id: number, Entity: makepaymentV3) {
    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}/new-billing/makePayment`;
      const getfindall = await axios.post(apiURL, Entity);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async get_transaction_details(
    hospital_id: number,
    hos_transaction_id: string
  ) {
    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}/new-billing/get-transaction-details?hospital_id=${hospital_id}&hos_transaction_id=${hos_transaction_id}`;
      const getfindall = await axios.get(apiURL);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async add_overall_charges(
    aayush_unique_id: string,
    hospital_id: number,
    body: 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}/new-billing/add-in-overallcharge?aayush_unique_id=${aayush_unique_id}&hospital_id=${hospital_id}`;

      const getfindall = await axios.post(apiURL, body);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async update_overall_charges(
    aayush_unique_id: string,
    hospital_id: number,
    body: 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}/new-billing/update-in-overallcharge?aayush_unique_id=${aayush_unique_id}&hospital_id=${hospital_id}`;
      const getfindall = await axios.post(apiURL, body);
      return getfindall.data;
    } catch (error) {
      console.log(error, "err");

      throw new Error(error);
    }
  }

  async overall_dashboard_data(
    hospital_id: number,
    limit: number,
    page: number,
    payment_status?: string,
    search?: string
  ) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      console.log(payment_status, "payment_status");

      let apiURL = `${getBaseURL.op_hub_base_url}/new-billing/dashboard?limit=${limit}&page=${page}`;
      console.log(apiURL, "api");
      if (payment_status) {
        apiURL += `&payment_status=${payment_status}`;
      }
      if (search) {
        apiURL += `&searchText=${search}`;
      }

      const getfindall = await axios.get(apiURL);
      console.log(getfindall.data, "getfindall.data");

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

      throw new Error(error);
    }
  }

  async dashboard_mongo_data(
    hospital_id: number,
    limit: number,
    page: number,
    search: string,
    start_date: any,
    end_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}/new-billing/dashboard-mongo-data?limit=${limit}&page=${page}`;
      console.log(apiURL, "api");
      if (start_date) {
        apiURL += `&start_date=${start_date}`;
      }
      if (end_date) {
        apiURL += `&end_date=${end_date}`;
      }
      if (search) {
        apiURL += `&search=${search}`;
      }

      const getfindall = await axios.get(apiURL);

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

      throw new Error(error);
    }
  }

  async dashboard_count(hospital_id: 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}/new-billing/dashboard-count?hospital_id=${hospital_id}`;
      console.log(apiURL, "api");

      const getfindall = await axios.get(apiURL);

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

      throw new Error(error);
    }
  }

  async balance_by_patient(hospital_id: number, aayush_unique_id: string) {
    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}/new-billing/patient-balance?aayush_unique_id=${aayush_unique_id}`;
      console.log(apiURL, "api");

      const getfindall = await axios.get(apiURL);

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

      throw new Error(error);
    }
  }

  async finddepartment_id(
    hospital_id: number,
    patient_id: string,
    module: string
  ) {
    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}/new-billing/app?patient_id=${patient_id}&hospital_id=${hospital_id}`;
      console.log(apiURL, "api");

      if (module) {
        apiURL += `&module=${module}`;
      }

      const getfindall = await axios.get(apiURL);

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

      throw new Error(error);
    }
  }

  async department_details() {
    let array = [
      {
        id: 1,
        module: "Appointment",
        module_code: "APPN",
      },
      {
        id: 2,
        module: "OPD",
        module_code: "OPDN",
      },
      {
        id: 3,
        module: "IPD",
        module_code: "IPDN",
      },
    ];

    return {
      status_code: 200,
      status: "success",
      message: "data fetched successfully",
      data: array,
    };
  }

  async delete_charges(
    patient_charge_id: number,
    opd_id: string,
    hospital_id: number,
    ipd_id?: string
  ) {
    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}/new-billing/delete-charges?patient_charge_id=${patient_charge_id}&hospital_id=${hospital_id}`;
      console.log(apiURL, "api");
      if (opd_id) {
        apiURL += `&opd_id=${opd_id}`;
      } if (ipd_id) {
        apiURL += `&ipd_id=${ipd_id}`;
      }
      const getfindall = await axios.delete(apiURL);

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

      throw new Error(error);
    }
  }

  async discount_update_overall_charges(
    deductDiscountDto: DeductDiscountDto
  ) {
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [deductDiscountDto.hospital_id]
      );
      let apiURL = `${getBaseURL.op_hub_base_url}/op-hub-appointment/deduct_discount_from_overall_charges`;

      const getfindall = await axios.put(apiURL, deductDiscountDto);
      return getfindall.data;
    } catch (error) {
      throw new Error(error);
    }
  }
}
