<?php

namespace fluXtore\Core\Frontend;

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

if ( ! class_exists( 'fluXtore_Tracking_Helper' ) ) {
        class fluXtore_Tracking_Helper {
                public static function is_builder_preview() {
                        if ( isset( $_GET['elementor-preview'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                                return true;
                        }

                        if ( isset( $_GET['bricks'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                                return true;
                        }

                        if ( isset( $_GET['et_fb'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                                return true;
                        }

                        return false;
                }

                public static function get_step_context() {
                        global $post;

                        $step_id = null;
                        $step_tag = null;

                        if ( is_singular( FLUXTORE_STEP ) && $post instanceof \WP_Post ) {
                                $step_id = (int) $post->ID;

                                if ( function_exists( '\\fluxtore_get_step_tag' ) ) {
                                        $step_tag = \fluxtore_get_step_tag( $step_id );
                                }
                        }

                        $context = [
                                'id'  => $step_id,
                                'tag' => $step_tag,
                        ];

                        return apply_filters( 'fluxtore_tracking_step_context', $context );
                }

                public static function get_currency() {
                        $currency = 'USD';

                        if ( function_exists( 'get_woocommerce_currency' ) ) {
                                $currency = get_woocommerce_currency();
                        }

                        return apply_filters( 'fluxtore_tracking_currency', $currency );
                }

                public static function is_relevant_context() {
                        if ( is_singular( FLUXTORE_STEP ) ) {
                                return true;
                        }

                        if ( function_exists( 'is_checkout' ) && is_checkout() ) {
                                return true;
                        }

                        if ( function_exists( 'is_order_received_page' ) && is_order_received_page() ) {
                                return true;
                        }

                        return false;
                }

                public static function is_checkout_context( $step_tag = null ) {
                        if ( $step_tag === FLUXTORE_CHECKOUT_TAG ) {
                                return true;
                        }

                        if ( function_exists( 'is_checkout' ) && is_checkout() ) {
                                return true;
                        }

                        return false;
                }

                public static function get_order_items( \WC_Order $order ) {
                        $items = [];

                        foreach ( $order->get_items() as $item ) {
                                if ( ! $item instanceof \WC_Order_Item_Product ) {
                                        continue;
                                }

                                $product_id = $item->get_product_id();

                                if ( ! $product_id ) {
                                        continue;
                                }

                                $quantity   = (int) $item->get_quantity();
                                $total      = (float) $item->get_total();
                                $subtotal   = (float) $item->get_subtotal();
                                $unit_price = 0.0;

                                if ( $quantity > 0 ) {
                                        $unit_price = (float) ( $total / $quantity );
                                }

                                $product = $item->get_product();
                                $sku     = '';

                                if ( $product instanceof \WC_Product ) {
                                        $sku = (string) $product->get_sku();
                                }

                                $items[] = [
                                        'id'           => (string) $product_id,
                                        'product_id'   => (int) $product_id,
                                        'variation_id' => (int) $item->get_variation_id(),
                                        'quantity'     => $quantity,
                                        'total'        => $total,
                                        'subtotal'     => $subtotal,
                                        'unit_price'   => $unit_price,
                                        'name'         => $item->get_name(),
                                        'sku'          => $sku,
                                ];
                        }

                        return apply_filters( 'fluxtore_tracking_order_items', $items, $order );
                }
        }
}
