<?php
/**
 *
 * Statistic Fetch Related Functions
 * 
 * Typically used to fetch specific or small groups of analytics / calculations
 *
 * @package Alpha Insights
 * @version 3.2.1
 * @since 3.2.1
 * @author WPDavies
 * @link https://wpdavies.dev/
 *
 */
defined( 'ABSPATH' ) || exit;

/**
 *
 *	Webhook data request
 *
 */
function wpdai_webhook_data_request( $from_date = null, $to_date = null, $JSON = true ) {

	$args = array();
	if ( is_string( $from_date ) && is_string( $to_date ) ) {
		$args['date_from'] = $from_date;
		$args['date_to'] = $to_date;
	}

	$args['data_table_limit'] = array(
		'orders' => -1,
		'expenses' => -1,
	);

	$data_warehouse = wpdai_data_warehouse( $args );
	$data_warehouse->fetch_data( array( 'orders', 'expenses', 'store_profit' ) );

	// Default array
	$webhook_data = array(
		'date_from' => $data_warehouse->get_date_from(),
		'date_to' => $data_warehouse->get_date_to(),
		'sales_data_summary' => $data_warehouse->get_data('orders', 'totals'),
		'expense_data_summary' => $data_warehouse->get_data('expenses', 'totals'),
		'store_profit_summary' => $data_warehouse->get_data('store_profit', 'totals'),
		'orders' => $data_warehouse->get_data('orders', 'data_table')['orders'],
		'expenses' => $data_warehouse->get_data('expenses', 'data_table')['expenses'] ?? array(),
	);

	// Prety print JSON
	if ( $JSON ) $webhook_data = json_encode( $webhook_data, JSON_PRETTY_PRINT );

	// Return results
	return $webhook_data;

}

/**
 *
 *	Execute Webhook Post
 *
 */
function wpdai_webhook_post_data( $from_date = null, $to_date = null ) {

	// Vars and data
	$response 			= array();
	$webhook_settings 	= get_option( 'wpd_ai_webhook_settings' );
	$url 				= $webhook_settings['webhook_url'];

	// Format according to date if necessary
	if ( $from_date === null && $to_date === null ) {

		$json_data = wpdai_webhook_data_request();

	} else {

		$json_data = wpdai_webhook_data_request( $from_date, $to_date );

	}

	// Post request
	$data = wp_remote_post( $url, array(

	    'headers'     => array( 'Content-Type' => 'application/json; charset=utf-8' ),
	    'body'        => $json_data,
	    'method'      => 'POST',
	    'data_format' => 'body',

	) );

	// Store data for debug
	$response['time_executed'] 	= wpdai_site_date_time( "D M d, Y G:i" );
	$response['url_sent_to'] 	= $url;

	// Check response
	if ( is_wp_error( $data ) ) {

		$response['message'] = 'Failed to connect to the provided URL - please check that your URL is correct.';
		$response['success'] = false;

	} else {

		$response_code = $data['response']['code'];
		$response_message = $data['response']['message'];

		$response['response_code'] 	= $response_code;
		$response['response_message'] 	= $response_message;

		if ( $response_code == 200 ) {

			$response['success'] = true;
			$response['success_message'] = 'Your data was succesfully posted to your requested URL.';

			// Store last succesful date
			$webhook_settings['webhook_schedule_last_run'] = $response['time_executed'];
			update_option( 'wpd_ai_webhook_settings', $webhook_settings );

		} else {

			$response['message'] = 'Failed to connect to the provided URL - ' . $response_message;
			$response['success'] = false;

		}

	}

	// Show data sent and response received
	$response['data_sent'] = json_decode( $json_data );

	wpdai_write_log( 'Webhook log report (' . $response['time_executed'] . '):', WPDAI_Webhook_Provider::$log_file );
	wpdai_write_log( $response, WPDAI_Webhook_Provider::$log_file );

	return $response;

}
