<?php

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

if ( ! function_exists( 'aieo_server_event_dispatch' ) ) :

function aieo_server_event_flush_fn() {
	if ( function_exists( 'fastcgi_finish_request' ) ) {
		return 'fastcgi_finish_request';   
	}
	if ( function_exists( 'litespeed_finish_request' ) ) {
		return 'litespeed_finish_request'; 
	}
	return '';                             
}

function aieo_server_event_dispatch( $hook, $order_id ) {
	$hook     = (string) $hook;
	$order_id = (int) $order_id;
	if ( '' === $hook || $order_id <= 0 ) {
		return;
	}

	
	
	
	if ( '' !== aieo_server_event_flush_fn() ) {
		if ( empty( $GLOBALS['aieo_server_event_pending'] ) || ! is_array( $GLOBALS['aieo_server_event_pending'] ) ) {
			$GLOBALS['aieo_server_event_pending'] = array();
		}
		$GLOBALS['aieo_server_event_pending'][] = array( $hook, $order_id );
		if ( empty( $GLOBALS['aieo_server_event_shutdown_hooked'] ) ) {
			$GLOBALS['aieo_server_event_shutdown_hooked'] = true;
			
			
			register_shutdown_function( 'aieo_server_event_flush_pending' );
		}
		return;
	}

	
	if ( function_exists( 'as_enqueue_async_action' ) ) {
		as_enqueue_async_action( $hook, array( $order_id ), 'aieo-server-events' );
	} elseif ( function_exists( 'wp_schedule_single_event' ) ) {
		wp_schedule_single_event( time() + 1, $hook, array( $order_id ) );
	}
}

function aieo_server_event_flush_pending() {
	if ( empty( $GLOBALS['aieo_server_event_pending'] ) || ! is_array( $GLOBALS['aieo_server_event_pending'] ) ) {
		return;
	}
	$pending = $GLOBALS['aieo_server_event_pending'];
	$GLOBALS['aieo_server_event_pending'] = array();

	
	if ( function_exists( 'ignore_user_abort' ) ) {
		ignore_user_abort( true );
	}
	$flush = aieo_server_event_flush_fn();
	if ( '' !== $flush ) {
		$flush(); 
	}
	
	if ( function_exists( 'set_time_limit' ) ) {
		@set_time_limit( 60 ); 
	}

	foreach ( $pending as $p ) {
		if ( is_array( $p ) && isset( $p[0], $p[1] ) ) {
			do_action( $p[0], (int) $p[1] );
		}
	}
}

function aieo_server_event_log_dispatched( $platform, $event_name, $order, $event_id ) {
	if ( ! function_exists( 'aieo_server_events_log_table' ) || ! is_a( $order, 'WC_Order' ) ) {
		return;
	}
	global $wpdb;
	$t = aieo_server_events_log_table();
	if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $t ) ) !== $t ) {
		return;
	}
	$now = current_time( 'mysql', true );
	
	$wpdb->query( $wpdb->prepare(
		"INSERT INTO `{$t}`
			(event_id, platform, event_name, order_id, session_id, user_id, status, http_code, fbtrace_id,
			 attempts, request_payload, response_body, error_message, value, currency, test_mode, created_at, sent_at)
		 VALUES (%s, %s, %s, %d, %s, %d, 'dispatched', 0, '', 1, '', '', '', %f, %s, 0, %s, %s)
		 ON DUPLICATE KEY UPDATE status = 'dispatched', attempts = attempts + 1, sent_at = VALUES(sent_at)",
		(string) $event_id,
		(string) $platform,
		(string) $event_name,
		(int) $order->get_id(),
		(string) $order->get_meta( '_aieo_attr_sid' ),
		(int) $order->get_customer_id(),
		(float) $order->get_total(),
		(string) $order->get_currency(),
		$now,
		$now
	) );
}

endif;
