<?php
/**
 * Some common functions for Smart Offers
 *
 * @package   smart-offers/includes/
 * @since     4.5.0
 * @version   1.0.0
 */

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

use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController;

/**
 * Function to check whether WooCommerce HPOS is enabled or not.
 *
 * @return boolean true if HPOS is enabled, else false
 */
function so_is_hpos_enabled() {
    return ( class_exists( 'Automattic\WooCommerce\Utilities\OrderUtil' ) && is_callable( array( '\Automattic\WooCommerce\Utilities\OrderUtil', 'custom_orders_table_usage_is_enabled' ) ) ) ? \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled() : false;
}

/**
 * Function to get WooCommerce admin order screen ID.
 *
 * @return string WooCommerce admin order screen ID.
 */
function so_get_admin_orders_screen() {
    $screen = ( ( class_exists( 'Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController' ) && is_callable( array( '\Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController', 'custom_orders_table_usage_is_enabled' ) ) ) && ( wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled() ) ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order';
    return $screen;
}

/**
 * Function to confirm if we are on WooCommerce admin orders listing page.
 *
 * @return boolean true if we are, else false.
 */
function so_is_admin_orders_screen() {
	$current_screen    = is_callable( 'get_current_screen' ) ? get_current_screen() : null;
	$current_screen_id = ( ! empty( $current_screen ) && $current_screen instanceof WP_Screen ) ? ( ! empty( $current_screen->id ) ? $current_screen->id : '' ) : '';
	if ( empty( $current_screen_id ) ) {
		return false;
	}

	if ( is_callable( 'so_is_hpos_enabled' ) && so_is_hpos_enabled() ) {
		if ( so_get_admin_orders_screen() === $current_screen_id ) {
			// to make sure we are on the order listing page only.
			if ( ! empty( $_REQUEST ) && ( ! empty( $_REQUEST['page'] ) ) && ( 'wc-orders' === $_REQUEST['page'] ) ) { // phpcs:ignore
				// if we are on orders filter page.
				if ( isset( $_REQUEST['action'] ) ) {  // phpcs:ignore
					if ( -1 === $_REQUEST['action'] || is_numeric( $_REQUEST['action'] ) ) { // phpcs:ignore
						return true;
					}
				} else {
					return true;
				}
			}
		}
	} else {
		$current_screen_post_type = ( ! empty( $current_screen ) && $current_screen instanceof WP_Screen ) ? ( ! empty( $current_screen->post_type ) ? $current_screen->post_type : '' ) : '';
		if ( empty( $current_screen_post_type ) ) {
			return false;
		}
		// to make sure we are on the order listing page only.
		if ( 'edit-shop_order' === $current_screen_id && so_get_admin_orders_screen() === $current_screen_post_type ) {
			return true;
		}
	}

	return false;
}

/**
 * Function to get sale order ids of offers.
 *
 * @return array $offers_sale_order_ids sale order ids of offers.
 */
function so_get_offers_sale_order_ids() {
    $offers_sale_order_ids    = array();
    $valid_order_status       = array( 'wc-completed', 'wc-processing', 'wc-on-hold' );
    $get_valid_order_statuses = get_option( 'so_valid_order_statuses_for_earning', $valid_order_status );

    if ( is_callable( 'so_is_hpos_enabled' ) && so_is_hpos_enabled() ) {
        $offers_sale_order_ids = wc_get_orders(
            array(
                'type'        => 'shop_order',
                'return'      => 'ids',
                'limit'       => -1,
                'status'      => $get_valid_order_statuses,
                'meta_query'  => array( // phpcs:ignore
                    array(
                        'key' => 'smart_offers_meta_data',
                    ),
                ),
            )
        );
    } else {
        $offers_sale_order_ids_query = new WP_Query(
            array(
                'post_type'   => 'shop_order',
                'fields'      => 'ids',
                'nopaging'    => true,
                'post_status' => $get_valid_order_statuses,
                'meta_query'  => array( // phpcs:ignore
                    array(
                        'key' => 'smart_offers_meta_data',
                    ),
                ),
            )
        );

        // if we find valid posts with offer meta, fetch and return that.
        if ( $offers_sale_order_ids_query->post_count > 0 ) {
            $offers_sale_order_ids = $offers_sale_order_ids_query->posts;
        }
    }

    return $offers_sale_order_ids;
}
