import { Injectable } from "@nestjs/common";
import axios from "axios";
import { DataSource } from "typeorm";
@Injectable()
export class DoctorsService {
  constructor(private readonly connection: DataSource) { }

  async findAll(
    search: string,
    hospital_id: number,
    date: any,
    gender: string,
    specialist_id: any
  ) {
    if (!hospital_id) {
      return {
        status: "failed",
        message: "Enter hospital_id to get shift",
      };
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      let url = `${getBaseURL.op_hub_base_url}/op-hub-doctors?hospital_id=${hospital_id}`;
      if (search) {
        url += `&search=${search}`;
      }
      if (date) {
        url += `&date=${date}`;
      }
      if (gender) {
        url += `&gender=${gender}`;
      }
      if (specialist_id) {
        url += `&specialist_id=${specialist_id}`;
      }
      const find_all_process_list = await axios.get(url);
      return find_all_process_list.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  //v2

  async V2findAll(
    search: string,
    hospital_id: number,
    date: any,
    gender: string,
    limit: number,
    page: number,
    staff_id: number
  ) {
    if (!hospital_id) {
      return {
        status: "failed",
        message: "Enter hospital_id to get shift",
      };
    }
    try {
      const [getBaseURL] = await this.connection.query(
        `select op_hub_base_url from hospitals where plenome_id = ?`,
        [hospital_id]
      );
      let url = `${getBaseURL.op_hub_base_url}/op-hub-doctors/v2?hospital_id=${hospital_id}&limit=${limit}&page=${page}`;
      if (search) {
        url += `&search=${search}`;
      }
      if (date) {
        url += `&date=${date}`;
      }
      if (gender) {
        url += `&gender=${gender}`;
      }
      if (staff_id) {
        url += `&staff_id=${staff_id}`;
      }
      console.log(url, "url");

      const find_all_process_list = await axios.get(url);
      return find_all_process_list.data;
    } catch (error) {
      throw new Error(error);
    }
  }

  async findalldoctors(hospital_id: number, limit: number, page: number) {
    const [getBaseURL] = await this.connection.query(
      `select op_hub_base_url from hospitals where plenome_id = ?`,
      [hospital_id]
    );
    let url = `${getBaseURL.op_hub_base_url}/op-hub-doctors/getDocName?hospital_id=${hospital_id}&limit=${limit}&page=${page}`;

    const find_all_doctors = await axios.get(url);
    return find_all_doctors.data;
  }
  catch(error) {
    throw new Error(error);
  }
}
