<?php
namespace ARCHM\Helpers;

/**
 * Universal Subscription Helper Class
 * Detects subscription products and orders across multiple plugins
 *
 * @since 1.7.1
 * @package ArchiveMaster
 */
class SubscriptionHelper {

    /**
     * Universal method to check if a product is a subscription
     * Works across WooCommerce Subscriptions, WPS, YITH, and other plugins
     *
     * @param int|WC_Product $product
     */
    public static function is_subscription_product( $product ) {
        if ( is_numeric($product) ) {
            $product = wc_get_product($product);
        }

        if ( ! $product || ! ( $product instanceof \WC_Product ) ) {
            return false;
        }

        // Method 3: Check known meta keys on the product itself
        if ( self::check_via_meta_keys($product) ) {
            return true;
        }

        // If this is a variation, also check the parent product's meta keys
        if ( $product->is_type('variation') ) {
            $parent_id = $product->get_parent_id();
            if ( $parent_id ) {
                $parent = wc_get_product($parent_id);
                if ( $parent instanceof \WC_Product && self::check_via_meta_keys($parent) ) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Check if an order contains subscription products (Method 3 only)
     *
     * @param \WC_Order $order
     */
    public static function is_subscription_order( $order ) {
        // Method 3: Check if any line item is a subscription product
        foreach ( $order->get_items() as $item ) {
            if ( ! ( $item instanceof \WC_Order_Item_Product ) ) {
                continue;
            }

            $product = $item->get_product();
            if ( $product && self::is_subscription_product($product) ) {
                return true;
            }
        }

        return false;
    }

    /**
     * Check via known meta keys
     */
    private static function check_via_meta_keys( $product ) {
        // WPS Subscriptions for WooCommerce
        $wps_subscription = $product->get_meta('_wps_sfw_product', true);
        if ( 'yes' === $wps_subscription ) {
            return true;
        }

        // YITH WooCommerce Subscription
        $yith_subscription = $product->get_meta('_ywsbs_subscription', true);
        $yith_price_per = $product->get_meta('_ywsbs_price_is_per', true);
        if ( 'yes' === $yith_subscription && ! empty($yith_price_per) ) {
            return true;
        }

        // WooCommerce Subscriptions meta keys
        $wcs_meta_keys = [
            '_subscription_price',
            '_subscription_period',
            '_subscription_period_interval',
            '_subscription_length',
            '_subscription_trial_length',
            '_subscription_trial_period',
            '_subscription_sign_up_fee',
        ];

        foreach ( $wcs_meta_keys as $meta_key ) {
            $value = $product->get_meta($meta_key, true);
            if ( ! empty($value) ) {
                return true;
            }
        }

        return false;
    }
}
