<?php
namespace Svea_Webpay_For_Woocommerce;

use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Invoice;
use Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Part_Pay;

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Class to handle cron functions for the Svea plugin
 */
class Cron_Functions {

	/**
	 * Action for checking strong auth on an order
	 */
	const CHECK_AUTH_ACTION = 'webpay_strong_auth_check';

	/**
	 * Initialize the class
	 *
	 * @return void
	 */
	public function init() {
		add_action( self::CHECK_AUTH_ACTION, [ $this, 'check_authentication_results' ], 10, 3 );
	}

	/**
	 * Get the gateway class
	 *
	 * @return object|false
	 */
	public function get_gateway( string $payment_method ) {
		switch ( $payment_method ) {
			case 'invoice':
				return WC_Gateway_Svea_Invoice::init();

			case 'part_pay':
				return WC_Gateway_Svea_Part_Pay::init();

			default:
				return false; // Not supported payment method
		}
	}

	/**
	 * Check if current order is authenticated and handle the results
	 *
	 * @return void
	 */
	public function check_authentication_results( int $order_id, $svea_order_id, string $payment_method ) {
		$wc_order = wc_get_order( $order_id );
		$svea_order_id = (string) $svea_order_id;

		if ( ! $wc_order ) {
			return; // Order not found
		}

		if ( (string) $wc_order->get_meta( '_svea_order_id' ) !== $svea_order_id ) {
			return; // Order ID does not match
		}

		/** @var \Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Invoice|\Svea_Webpay_For_Woocommerce\Gateways\WC_Gateway_Svea_Part_Pay|false $gateway */
		$gateway = $this->get_gateway( $payment_method );

		if ( ! $gateway ) {
			return;
		}

		$gateway->set_config( $wc_order->get_billing_country(), $wc_order->get_meta( '_svea_customer_type' ) );

		try {
			$confirmed = $gateway->check_strong_auth_svea_order( $wc_order, $svea_order_id );
		} catch ( \Exception $e ) {
			return; // Could not confirm the order status with Svea
		}

		if ( is_null( $confirmed ) ) {
			return; // No response was returned from Svea
		}

		if ( $confirmed ) {
			$this->finish_order( $order_id, $svea_order_id );
		} else {
			$this->cancel_order( $order_id );
		}
	}

	/**
	 * Finish the order by marking it as completed.
	 *
	 * @param int    $order_id The ID of the order to finish.
	 * @param string $svea_order_id The Svea order ID.
	 * @return void
	 */

	public function finish_order( $order_id, $svea_order_id ) {
		$wc_order = wc_get_order( $order_id );

		if ( ! $wc_order || $wc_order->is_paid() ) {
			return; // Order is already completed
		}

		$wc_order->payment_complete( $svea_order_id );
	}

	/**
	 * Cancels an order by updating its status to 'cancelled'.
	 *
	 * @param int $order_id The ID of the order to cancel.
	 * @return void
	 */
	public function cancel_order( $order_id ) {
		$wc_order = wc_get_order( $order_id );

		if ( ! $wc_order || $wc_order->get_status() === 'cancelled' ) {
			return; // Order is already cancelled
		}

		$wc_order->update_status( 'cancelled', 'Order cancelled by the system' );
	}

	/**
	 * Setup strong auth check cron
	 *
	 * @param int $order_id
	 * @param string $svea_order_id
	 * @param string $payment_method
	 * @return void
	 */
	public static function setup_strong_auth_check_cron( int $order_id, string $svea_order_id, string $payment_method ) {
		wp_schedule_single_event(
			strtotime( 'now' ) + 600, // now + 10 minutes
			Cron_Functions::CHECK_AUTH_ACTION,
			[ $order_id, $svea_order_id, $payment_method ]
		);
	}
}
