<?php

namespace Bookingor\Backend\Controller\Dashboard;

use GuzzleHttp\Client;

/**
 * Class representing a controller for managing Dashboard.
 */
class DashboardControl
{
    private static $bookingorDb;

    public static $name_prefix = 'bookingor';
    /**
     * Database table prefix for booking-related tables.
     *
     * @var string
     */
    public static $dp_prefix = 'bookingor_';

    /**
     * Retrieve the total number of bookings from the booking database.
     *
     * This method returns the total number of bookings from the booking
     * database. The count is based on the number of bookings in the
     * booked_appointment table.
     *
     * @return int The total number of bookings.
     */
    public static function bookingor_total_booking()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $appoint_view = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
        $query = self::$bookingorDb->get_var($wpdb->prepare('SELECT COUNT(book_id) FROM %1$s', $appoint_view));

        return $query;
    }

    /**
     * Retrieve the total number of customers from the booking database.
     *
     * This method returns the total number of customers from the booking
     * database. The count is based on the number of distinct customer IDs
     * found in the booked appointments table.
     *
     * @return int The total number of customers.
     */
    public static function bookingor_total_customer()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $appoint_view = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
        $query = self::$bookingorDb->get_var($wpdb->prepare('SELECT count(DISTINCT customer_get_id) FROM %1$s', $appoint_view));

        return $query;
    }

    /**
     * Retrieve the total revenue from the booking database.
     *
     * This method returns the total revenue from the booking database. The
     * revenue is based on the sum of the paid amounts in the customers_payment
     * table.
     *
     * @return float|int The total revenue.
     */
    public static function bookingor_total_revenue()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $customer_paid = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
        $query = self::$bookingorDb->get_var($wpdb->prepare('SELECT sum(paid_amount) FROM %1$s', $customer_paid));

        return $query;
    }

    /**
     * Retrieve the total number of approved appointments from the booking database.
     *
     * This method returns the total number of approved appointments from the booking
     * database. The count is based on the number of appointments with the status
     * "Approved" in the booked appointments table.
     *
     * @return int The total number of approved appointments.
     */
    public static function bookingor_total_approved_appointment()
    {
        global $wpdb;
        self::$bookingorDb = $wpdb;
        $appoint_view = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
        $query = self::$bookingorDb->get_var($wpdb->prepare('SELECT count(appoint_status) FROM %1$s', $appoint_view));

        return $query;
    }

    /**
     * Retrieve the daily sums of revenue for the last 7 days.
     *
     * This method returns the daily sums of revenue for the last 7 days.
     * The sums are based on the paid amounts in the customers_payment table.
     *
     * @return array The daily sums of revenue in the last 7 days.
     */
    public function bookingor_weekly_chart()
    {
        global $wpdb;
        $dailySums = [];
        self::$bookingorDb = $wpdb;
        $customer_paid = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
        $response = self::$bookingorDb->get_results($wpdb->prepare('SELECT DATE(created_at) AS date, SUM(paid_amount) AS daily_sum FROM  %1$s WHERE created_at >= NOW() - INTERVAL 7 DAY GROUP BY DATE(created_at)', $customer_paid));
        echo wp_json_encode($response);
        die();
    }
}
