<?php
/**
 * PeachPay Authorize.net order util class.
 *
 * @package PeachPay
 */

defined( 'PEACHPAY_ABSPATH' ) || exit;

/**
 * Utility class to handle checking authnet transactions
 */
class PeachPay_Authnet_Order_Data extends PeachPay_Order_Data {

	/**
	 * Adds metadata about an authnet transaction.
	 *
	 * @param WC_Order $order A order.
	 * @param array    $transaction_details Details about an authnet transaction.
	 *
	 * @return boolean Indicating if successful.
	 */
	public static function set_transaction_details( $order, $transaction_details ) {
		return self::set_order_metadata( $order, '_pp_authnet_transaction_details', $transaction_details );
	}


	/**
	 * Gets metadata about Authnet transaction for an order.
	 *
	 * @param WC_Order $order A order.
	 * @param string   $key Metadata key.
	 *
	 * @return mixed|null The details value or null.
	 */
	public static function get_transaction_details( $order, $key ) {
		return self::get_order_metadata( $order, '_pp_authnet_transaction_details', $key );
	}

	/**
	 * Stores the CIM customerProfileId and customerPaymentProfileId on an order.
	 *
	 * @param WC_Order $order              The order.
	 * @param string   $customer_profile_id Authorize.net customerProfileId.
	 * @param string   $payment_profile_id  Authorize.net customerPaymentProfileId.
	 * @return bool
	 */
	public static function set_cim_profile( $order, $customer_profile_id, $payment_profile_id ) {
		return self::set_order_metadata(
			$order,
			'_pp_authnet_cim_profile',
			array(
				'customer_profile_id' => $customer_profile_id,
				'payment_profile_id'  => $payment_profile_id,
			)
		);
	}

	/**
	 * Returns the stored CIM profile IDs for an order, or null if not present.
	 *
	 * @param WC_Order $order The order.
	 * @return array{customer_profile_id: string, payment_profile_id: string}|null
	 */
	public static function get_cim_profile( $order ) {
		if ( ! $order->meta_exists( '_pp_authnet_cim_profile' ) ) {
			return null;
		}

		$data = $order->get_meta( '_pp_authnet_cim_profile', true );

		if ( ! is_array( $data ) || empty( $data['customer_profile_id'] ) || empty( $data['payment_profile_id'] ) ) {
			return null;
		}

		return $data;
	}

	/**
	 * Returns true if the order has a valid CIM profile stored.
	 *
	 * @param WC_Order $order The order.
	 * @return bool
	 */
	public static function has_cim_profile( $order ) {
		return null !== self::get_cim_profile( $order );
	}

	/**
	 * Stores the Authorize.net networkTransId on an order (used for MIT compliance on renewals).
	 *
	 * @param WC_Order $order    The order.
	 * @param string   $trans_id The networkTransId from the initial transaction response.
	 */
	public static function set_network_trans_id( $order, $trans_id ) {
		$order->update_meta_data( '_pp_authnet_network_trans_id', $trans_id );
		$order->save();
	}

	/**
	 * Returns the stored networkTransId for an order, or null if not present.
	 *
	 * @param WC_Order $order The order.
	 * @return string|null
	 */
	public static function get_network_trans_id( $order ) {
		$value = $order->get_meta( '_pp_authnet_network_trans_id', true );
		return $value ? $value : null;
	}
}
