<?php

namespace Bright_Deposits_For_WooCommerce\Helpers;

/**
 * @package Bright_Deposits_For_WooCommerce\Helpers
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for handling the ajax requests for the plugin.
 */
class Ajax {
	
	public function __construct() {
		add_action( 'wp_ajax_bdfwbp_deposit_cart_btn', [$this, 'deposit_pro_cart_btn_callback'] );
		add_action( 'wp_ajax_nopriv_bdfwbp_deposit_cart_btn', [$this, 'deposit_pro_cart_btn_callback'] );
		add_action( 'wp_ajax_bdfwbp_get_variation_deposit_info', [$this, 'get_variation_deposit_info_callback'] );
		add_action( 'wp_ajax_nopriv_bdfwbp_get_variation_deposit_info', [$this, 'get_variation_deposit_info_callback'] );
	}

	/**
	 * Handle the deposit pro cart button callback.
	 * @since 1.0.0
	 * @return void
	 */
	public function deposit_pro_cart_btn_callback() {
		if ( !DOING_AJAX ) {
			wp_die();
		} // Not Ajax

		// Check for nonce security
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'deposits_nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Invalid nonce.', 'bright-deposits-for-woocommerce' ) ], 403 );
		}

		if ( ! isset( $_POST['product_id'] ) ) {
			wp_send_json_error( [ 'message' => __( 'Product ID is missing.', 'bright-deposits-for-woocommerce' ) ], 400 );
		}

		if ( ! isset( $_POST['deposit_enabled'] ) ) {
			wp_send_json_error( [ 'message' => __( 'Deposit status is missing.', 'bright-deposits-for-woocommerce' ) ], 400 );
		}

	$product_id = absint( $_POST['product_id'] );
	$deposit_enabled = sanitize_text_field( wp_unslash( $_POST['deposit_enabled'] ) );

	/**
         * BDFWBP_CUSTOM_FILTER_HOOK: Allows developers to filter the deposit status.
         * @since 1.0.0
         * @param string    $deposit_enabled The deposit status.
         * @param int       $product_id      The product id.
         * @param string    $context     The context.
         */
	$deposit_status = apply_filters( 'bdfwbp_ajax_deposit_data', $deposit_enabled, $product_id );

	WC()->session->set( 'deposit_checked', $deposit_status );

	/**
	 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to hook after the deposit pro cart button.
	 * @since 1.0.0
	 * @param int    $product_id      The product ID (sanitized).
	 * @param string $deposit_status  The deposit status (sanitized).
	 */
	do_action( 'bdfwbp_after_deposit_pro_cart_btn', $product_id, $deposit_status );

		wp_send_json_success();
	}

	/**
	 * Handle the variation deposit info callback.
	 * @since 1.0.0
	 * @return void
	 */
	public function get_variation_deposit_info_callback() {
		if ( !DOING_AJAX ) {
			wp_die();
		} // Not Ajax

		// Check for nonce security
		if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'deposits_nonce' ) ) {
			wp_send_json_error( [ 'message' => __( 'Invalid nonce.', 'bright-deposits-for-woocommerce' ) ], 403 );
		}

		if ( ! isset( $_POST['variation_id'] ) ) {
			wp_send_json_error( [ 'message' => __( 'Variation ID is missing.', 'bright-deposits-for-woocommerce' ) ], 400 );
		}

		$variation_id = absint( $_POST['variation_id'] );
		$variation = wc_get_product( $variation_id );

		if ( ! $variation || ! $variation->is_type( 'variation' ) ) {
			wp_send_json_error( [ 'message' => __( 'Invalid variation.', 'bright-deposits-for-woocommerce' ) ], 400 );
		}

		// Use the Product class method to calculate deposit data
		$product_class = \Bright_Deposits_For_WooCommerce\Pages\Product::instance();
		$deposit_data = $product_class->calculateDepositData( $variation_id );
		
		if ( ! $deposit_data ) {
			wp_send_json_error( [ 'message' => __( 'Could not calculate deposit data.', 'bright-deposits-for-woocommerce' ) ], 400 );
		}

		$response_data = array(
			'deposit_text' => $deposit_data['deposit_text'],
			'deposit_amount' => $deposit_data['deposit_amount'],
			'custom_text' => $deposit_data['custom_text'],
			'is_deposit_forced' => $deposit_data['is_deposit_forced'],
			'deposit_enabled' => $deposit_data['deposit_enabled']
		);

		wp_send_json_success( $response_data );
	}

}