<?php

namespace Bookingor\Backend\Controller\Customer;

/**
 * Class representing a controller for managing customers.
 * 
 * This class provides methods for retrieving customer views and deleting customers.
 */
class CustomerControl
{

	private static $bookingorDb;

	/**
	 * Database table prefix for booking-related tables.
	 *
	 * @var string
	 */
	public static $dp_prefix = "bookingor_";
	/**
	 * Flag to indicate whether the admin is viewing customer bookings.
	 *
	 * @var bool
	 */
	public static $admin_customer_view;
	/**
	 * Retrieve a list of customers with their booking details.
	 *
	 * This method returns a list of customers with their booking details.
	 * The list is grouped by customer email and includes the customer's
	 * first name, last name, email, phone number, booking start time,
	 * picked date, end time, payment method, and the total number of
	 * bookings.
	 *
	 * @return array List of customers with their booking details.
	 */


	public static function bp_get_customer_view()
	{
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$customer_view = $wpdb->prefix . self::$dp_prefix . 'customer_add';
		$appoint_view = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
		$query = "SELECT DISTINCT a.book_id, c.customer_id, a.customer_get_id, a.paid_amount, c.customer_first_name, c.customer_last_name, c.customer_email, c.customer_phone, a.start_time, a.picked_date, a.end_time, a.payment_method, c.created_at, COUNT(a.customer_get_id) AS total_booking
		FROM %1\$s AS a
		INNER JOIN %2\$s AS c ON c.customer_id = a.customer_get_id
		GROUP BY c.customer_email";
		return self::$admin_customer_view = self::$bookingorDb->get_results(self::$bookingorDb->prepare($query, $appoint_view, $customer_view));
	}
	/**
	 * Delete a customer from the database.
	 *
	 * This method deletes a customer from the database, but only if the customer does not have any bookings.
	 * If the customer has bookings, this method will return an error message.
	 *
	 * This method is called via an AJAX request from the customer management page.
	 */
	public static function customer_delete()
	{
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$customer = $wpdb->prefix . self::$dp_prefix . 'customer_add';
		$booked = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';


		if (sanitize_text_field(wp_unslash(isset($_REQUEST["id"])))) {


			if (isset($_POST['nonce'])) {
				if (!wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'bookingor_ajax_nonce')) {
					$response = [
						'status' => htmlspecialchars('error'),
						'message' => htmlspecialchars('Nonce verification failed.')
					];
					echo wp_json_encode($response);
				}
			}
			if (!current_user_can('manage_options')) {
				$response = [
					'status' => htmlspecialchars('error'),
					'message' => htmlspecialchars('You do not have permission.')
				];
				echo wp_json_encode($response);
				die();
			}


			$id = stripslashes(filter_var(wp_unslash($_REQUEST["id"]), FILTER_SANITIZE_NUMBER_INT));

			$get_customer_in_booking = self::$bookingorDb->get_results(
				$wpdb->prepare(
					"SELECT book_id, duration_service, start_time, end_time, picked_date_end, picked_date, service_get_price,  discount_get_price,  paid_amount, payment_method, customer_time_zone, token_code, appoint_status, category_get_id, service_get_id, staff_get_id, location_get_id, updated_at, created_at FROM {$wpdb->prefix}bookingor_booked_appointment  WHERE customer_get_id = %d",
					$id
				)
			);
			if (empty($get_customer_in_booking)) {
				self::$bookingorDb->delete($customer, array(
					'customer_id' => $id
				));
				$response = [
					'status' => htmlspecialchars('success'),
					'message' => htmlspecialchars('Customer Deleted successfully.')
				];
				echo wp_json_encode($response);
				exit;
			} else {
				$response = [
					'status' => htmlspecialchars('error'),
					'message' => htmlspecialchars('Customer has already Bookings')
				];
				echo wp_json_encode($response);
				exit;
			}
		}
	}
}
