<?php

namespace Bookingor\Backend\Controller\Payment;

class PaymentControl
{

	private static $bookingorDb;
	/**
	 * Database table prefix for booking-related tables.
	 *
	 * @var string
	 */
	public static $dp_prefix = "bookingor_";
	/**
	 * Flag to indicate whether the payment view is enabled or disabled.
	 *
	 * @var bool
	 */
	public static $payment_view;
	/**
	 * Retrieves a list of all customer payments with their respective appointment details.
	 *
	 * @return array|null An array of customer payments with their respective appointment details, or null if the
	 *                    tables are empty.
	 */
	public static function customer_payment_view()
	{
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$customer_detail = $wpdb->prefix . self::$dp_prefix . 'customer_add';
		$staff_detail = $wpdb->prefix . self::$dp_prefix . 'staff_add';
		$service_detail = $wpdb->prefix . self::$dp_prefix . 'services';
		$customer_booked = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
		$payment = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
		if (!empty($customer_booked) && !empty($customer_detail) && !empty($staff_detail) && !empty($service_detail) && !empty($payment)) {
			return self::$payment_view =  self::$bookingorDb->get_results(
				$wpdb->prepare(
					"SELECT Distinct 
					cb.book_id, 
					cb.service_get_id, 
					cb.customer_get_id, 
					cb.picked_date, 
					cb.picked_date_end, 
					cb.staff_get_id, 
					sd.service_icon, 
					sd.service_name, 
					sd.service_duration, 
					sd.service_cost, 
					sd.service_tax, 
					cb.appoint_status, 
					cb.picked_date, 
					cb.picked_date_end, 
					cb.payment_method, 
					cb.service_get_price, 
					cd.customer_id, 
					p.created_at, 
					p.payment_status, 
					p.pay_id, 
					p.paid_amount, 
					p.payment_method, 
					cd.customer_first_name, 
					cd.customer_last_name, 
					cd.customer_email, 
					cd.customer_phone, 
					st.staff_first_name, 
					st.staff_last_name, 
					st.staff_email, 
					st.staff_phone, 
					st.staff_icon, 
					p.payment_status 
				FROM 
					%1\$s cb 
				LEFT JOIN 
					%2\$s cd ON cb.customer_get_id = cd.customer_id 
				LEFT JOIN 
					%3\$s sd ON cb.service_get_id = sd.service_id 
				LEFT JOIN 
					%4\$s st ON cb.staff_get_id = st.staff_id 
				LEFT JOIN 
					%5\$s p ON p.book_get_id = cb.book_id",
					$customer_booked,
					$customer_detail,
					$service_detail,
					$staff_detail,
					$payment
				)
			);
		}
	}
	/**
	 * Update paid and pay status.
	 *
	 * When update button is clicked, this function will update the paid and pay status of the payment.
	 *
	 * @since 1.0.0
	 */
	public function update_paid_n_pay_status()
	{
		$response  = new \stdclass();
		$response->success = false;
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$customer_detail = $wpdb->prefix . self::$dp_prefix . 'customer_add';
		$staff_detail = $wpdb->prefix . self::$dp_prefix . 'staff_add';
		$service_detail = $wpdb->prefix . self::$dp_prefix . 'services';
		$customer_booked = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
		$payment = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
		$get_pay_id = isset($_REQUEST['get_pay_id']) ? intval($_REQUEST['get_pay_id']) : 0;
		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);
			}
		}

		$response = self::$bookingorDb->get_results($wpdb->prepare(
			"SELECT Distinct 
				cb.book_id,
				cb.service_get_id,
				cb.customer_get_id,
				cb.picked_date,
				cb.picked_date_end,
				cb.staff_get_id,
				cb.service_get_price,
				cb.discount_get_price,
				cb.appoint_status,
				cb.picked_date,
				cb.picked_date_end,
				cb.payment_method,
				p.created_at,
				p.payment_status,
				p.pay_id,
				p.paid_amount,
				p.due_amount,
				p.payment_method,
				p.payment_status
			FROM 
				{$wpdb->prefix}bookingor_booked_appointment cb
			LEFT JOIN 
				{$wpdb->prefix}bookingor_customers_payment p ON p.book_get_id = cb.book_id
			WHERE 
				p.pay_id = %s",
			$get_pay_id
		));
		if (!isset($customer_booked) && !isset($customer_detail) && !isset($staff_detail) && !isset($service_detail) && !isset($payment)) {
		}
		echo wp_json_encode($response);
		die;
	}
	/**
	 * Changes the payment status in the database.
	 *
	 * @return object Json response, contains success and message
	 */
	public function change_pay_status()
	{
		$response  = new \stdclass();
		$response->success = false;
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$payment = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
		$get_pay_id = isset($_REQUEST['pay_stats_id']) ? intval($_REQUEST['pay_stats_id']) : 0;
		$get_pay_status = sanitize_text_field(wp_unslash($_POST['pay_status'] ?? ""));
		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();
		}
		if ($response) {
			$response->success = true;
			self::$bookingorDb->update(
				$payment,
				array(
					'payment_status' => $get_pay_status
				),
				array('pay_id' => $get_pay_id)
			);
			$response->message = "payment Status " . $get_pay_status . " Updated";
		} else {
			$response->message = 'Error occurred while saving record';
		}
		echo wp_json_encode($response);
		die;
	}
	/**
	 * Update paid amount.
	 *
	 * When update button is clicked, this function will update the paid amount of the payment.
	 *
	 * @since 1.0.0
	 */
	public function update_paid_amount()
	{
		$response  = new \stdclass();
		$response->success = false;
		global $wpdb;
		self::$bookingorDb = $wpdb;
		$payment = $wpdb->prefix . self::$dp_prefix . 'customers_payment';
		$get_pay_id = isset($_REQUEST['pay_stats_id']) ? intval($_REQUEST['pay_stats_id']) : 0;
		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);
			}
		}
		$get_paid_amount = sanitize_text_field(wp_unslash($_POST['paid_amount'] ?? ""));
		$get_due_amount = sanitize_text_field(wp_unslash($_POST['due_amount'] ?? ""));
		if ($response) {
			$response->success = true;
			self::$bookingorDb->update(
				$payment,
				array(
					'paid_amount' => $get_paid_amount,
					'due_amount' => $get_due_amount
				),
				array('pay_id' => $get_pay_id)
			);
			$response->message = "Paid Amount " . $get_paid_amount . " Updated";
		} else {
			$response->message = 'Error occurred while saving record';
		}
		echo wp_json_encode($response);
		die;
	}
}
