import { Injectable } from "@nestjs/common";
import { CreateStatisticDto } from "./dto/create-statistic.dto";
import { UpdateStatisticDto } from "./dto/update-statistic.dto";
import { DataSource } from "typeorm";
import { query } from "express";
import axios from "axios";

@Injectable()
export class StatisticsService {
  constructor(private readonly connection: DataSource) {}
  async create(hospital_id: number) {
    try {
      let query = `select * from hospitals `;
      if (hospital_id) {
        query += `where plenome_id = ${hospital_id}`;
      }
      const getHosDetails = await this.connection.query(query);
      return {
        statusCode: 200,
        status: "success",
        message: "Hospital details fetched successfully",
        data: getHosDetails,
        count: getHosDetails?.length,
      };
    } catch (error) {
      console.log(error);

      return {
        statusCode: 500,
        status: "error",
        message: "Something went wrong while fetching hospital details",
      };
    }
  }

  async findAll(hospital_id: number) {
    try {
      let query = `select staff.*,roles.name role from staff
       left join staff_roles on staff.id = staff_roles.staff_id
       left join roles on staff_roles.role_id = roles.id `;
      let count_query = `select count(staff.id) total from staff `;

      if (hospital_id) {
        query += `left join hospital_staffs on hospital_staffs.staff_id = staff.id where hospital_staffs.hospital_id = ${hospital_id}`;
        count_query += `left join hospital_staffs on hospital_staffs.staff_id = staff.id where hospital_staffs.hospital_id = ${hospital_id}`;
      }
      const getHosDetails = await this.connection.query(query);
      const [getStaffCount] = await this.connection.query(count_query);
      return {
        statusCode: 200,
        status: "success",
        message: "Staff details fetched successfully",
        data: getHosDetails,
        count: getStaffCount?.total,
      };
    } catch (error) {
      console.log(error);

      return {
        statusCode: 500,
        status: "error",
        message: "Something went wrong while fetching hospital details",
      };
    }
  }

  async findAlAppt(hospital_id: number) {
    try {
      let query = `select * from appointment `;
      let count_query = `select count(id) total from appointment `;

      if (hospital_id) {
        query += ` where Hospital_id = ${hospital_id}`;
        count_query += `where Hospital_id = ${hospital_id}`;
      }
      const getHosDetails = await this.connection.query(query);
      const [getStaffCount] = await this.connection.query(count_query);
      return {
        statusCode: 200,
        status: "success",
        message: "Appointment details fetched successfully",
        data: getHosDetails,
        count: getStaffCount?.total,
      };
    } catch (error) {
      return {
        statusCode: 500,
        status: "error",
        message: "Something went wrong while fetching hospital details",
      };
    }
  }

  async findAlTrans(hospital_id: number) {
    try {
      let query = `select * from transactions `;
      let count_query = `select count(id) total,sum(amount) amount,sum(temp_appt_amount) temp_appt_amount from transactions `;

      if (hospital_id) {
        query += ` where Hospital_id = ${hospital_id}`;
        count_query += `where Hospital_id = ${hospital_id}`;
      }
      const getHosDetails = await this.connection.query(query);
      const [getStaffCount] = await this.connection.query(count_query);
      return {
        statusCode: 200,
        status: "success",
        message: "Transaction details fetched successfully",
        data: getHosDetails,
        amount: getStaffCount?.amount,
        temp: getStaffCount?.temp_appt_amount,
        count: getStaffCount?.total,
      };
    } catch (error) {
      return {
        statusCode: 500,
        status: "error",
        message: "Something went wrong while fetching hospital details",
      };
    }
  }

  async findAllPatient(hospital_id: number) {
    try {
      let query = `select * from patients`;
      let count_query = `select count(id) total from patients `;

      if (hospital_id) {
        try {
          const [get_base_url] = await this.connection.query(
            `select phr_api_base_url from hospitals where plenome_id = ?`,
            [hospital_id]
          );
          const response = await axios.get(
            `${get_base_url.phr_api_base_url}/setup-patient-new-patient/v3/getAllpatient`
          );
          return response.data;
        } catch (error) {
          return error;
        }
      }
      const getHosDetails = await this.connection.query(query);
      const [getStaffCount] = await this.connection.query(count_query);
      return {
        statusCode: 200,
        status: "success",
        message: "Staff details fetched successfully",
        details: getHosDetails,
        count: getStaffCount?.total,
      };
    } catch (error) {
      return {
        statusCode: 500,
        status: "error",
        message: "Something went wrong while fetching hospital details",
      };
    }
  }

  remove(id: number) {
    return `This action removes a #${id} statistic`;
  }
}
