<?php
/**
 * PeachPay Payment util class.
 *
 * Note: Server dependency removed. Transaction IDs are now generated locally.
 *
 * @package PeachPay/Payments
 */

if ( ! defined( 'PEACHPAY_ABSPATH' ) ) {
	exit;
}

/**
 * Utility class for PeachPay payment transactions.
 * All transaction operations are now local - no server calls.
 */
final class PeachPay_Payment {

	/**
	 * Constructor - hooks into WooCommerce cart events.
	 */
	private function __construct() {
		add_action( 'woocommerce_cart_emptied', array( 'PeachPay_Payment', 'reset_session' ) );
	}

	/**
	 * Creates a PeachPay transaction for an order and stores the details.
	 *
	 * @param WC_Order $order The WooCommerce order.
	 * @param string   $session_id The session ID.
	 * @param string   $transaction_location The checkout location (e.g., 'checkout-page', 'subscription-renewal').
	 * @param string   $peachpay_mode The mode ('test', 'live', or 'detect').
	 * @return array Result array with 'success' and 'data' keys.
	 */
	public static function create_order_transaction( $order, $session_id, $transaction_location, $peachpay_mode = 'detect' ) {
		$result = self::create_transaction( $session_id, $order->get_payment_method(), $transaction_location, $peachpay_mode );

		if ( true === $result['success'] ) {
			PeachPay_Stripe_Order_Data::set_peachpay_details(
				$order,
				array(
					'session_id'     => $session_id,
					'transaction_id' => $result['data']['transaction_id'],
				)
			);
		}

		return $result;
	}

	/**
	 * Updates a PeachPay transaction for an order.
	 * Note: Server dependency removed. This is now a no-op that returns success.
	 *
	 * @param WC_Order $order The order to update for.
	 * @param array    $options The order details (kept for backward compatibility).
	 * @return array Result array with 'success' key.
	 */
	public static function update_order_transaction( $order, $options = array() ) {
		return array( 'success' => true );
	}

	/**
	 * Creates a PeachPay transaction locally.
	 *
	 * Note: Server dependency removed. Transaction IDs are now generated locally
	 * using a combination of timestamp, random number, and session ID hash.
	 *
	 * @param string $session_id The session id for the order.
	 * @param string $gateway_id The gateway id for the order (kept for backward compatibility).
	 * @param string $checkout_location The checkout location for the order (kept for backward compatibility).
	 * @param string $peachpay_mode The mode to create the transaction (kept for backward compatibility).
	 * @return array Result array with 'success' and 'data' keys.
	 */
	public static function create_transaction( $session_id, $gateway_id, $checkout_location, $peachpay_mode = 'detect' ) {
		$timestamp      = time();
		$random         = wp_rand( 10000, 99999 );
		$session_hash   = substr( md5( $session_id ), 0, 8 );
		$transaction_id = "pp_tx_{$timestamp}_{$random}_{$session_hash}";

		return array(
			'success' => true,
			'data'    => array(
				'transaction_id' => $transaction_id,
			),
		);
	}

	/**
	 * Updates a PeachPay transaction.
	 * Note: Server dependency removed. This is now a no-op that returns success.
	 *
	 * @param string $transaction_id The transaction id (kept for backward compatibility).
	 * @param string $session_id The session id (kept for backward compatibility).
	 * @param array  $options The order details (kept for backward compatibility).
	 * @param string $peachpay_mode The mode (kept for backward compatibility).
	 * @return array Result array with 'success' key.
	 */
	public static function update_transaction( $transaction_id, $session_id, $options = array(), $peachpay_mode = 'detect' ) {
		return array( 'success' => true );
	}

	/**
	 * Gets the PeachPay checkout session id. If it does not yet exists then it is created.
	 *
	 * @return string The session id.
	 */
	public static function get_session() {
		$session_id = WC()->session->get( 'peachpay_session_id' );

		if ( ! $session_id ) {
			$session_id = wp_generate_uuid4();
			WC()->session->set( 'peachpay_session_id', $session_id );
		}

		return $session_id;
	}

	/**
	 * Resets the PeachPay checkout session id.
	 */
	public static function reset_session() {
		unset( WC()->session['peachpay_session_id'] );

		WC()->session->save_data();
	}

	/**
	 * Updates a PeachPay transaction with purchase order details.
	 * Note: Server dependency removed. This is now a no-op that returns success.
	 *
	 * @param WC_Order $order The order (kept for backward compatibility).
	 * @param string   $session_id The session id (kept for backward compatibility).
	 * @param string   $transaction_id The transaction id (kept for backward compatibility).
	 * @param string   $purchase_order_number The purchase order number (kept for backward compatibility).
	 * @return array Result array with 'success' key.
	 */
	public static function update_transaction_purchase_order( $order, $session_id, $transaction_id, $purchase_order_number ) {
		return array( 'success' => true );
	}

	/**
	 * Gets the available PeachPay gateways instances.
	 */
	public static function available_gateways() {
		$gateways = array();

		foreach ( WC()->payment_gateways->payment_gateways() as $gateway ) {
			if ( $gateway instanceof PeachPay_Payment_Gateway ) {
				$gateways[] = $gateway;
			}
		}

		return $gateways;
	}
}
