<?php
/**
 * A1-WooBay - License / Freemius Gating Helper
 *
 * Central place for all "is this feature allowed on the Free plan?" checks.
 * Every other class asks this helper instead of talking to Freemius directly,
 * so the free/paid feature split stays in one file.
 *
 * Free plan limits (updated 2026-08-12 — listing/profile caps removed, and
 * scheduled/automatic sync is now available on both plans, for WP.org
 * Guideline 5 compliance; unlimited listings/profiles are a core,
 * always-present capability and can't be gated behind a license check, and
 * scheduled sync shares its implementation with the free manual "Sync Now"
 * button so it couldn't be split into a separately-stripped feature. What
 * remains Pro-exclusive below is genuinely separate code, physically absent
 * from the free build via Freemius's __premium_only stripping):
 *   - Unlimited eBay listings.
 *   - Unlimited eBay profiles.
 *   - No image sync to eBay (listings publish without photos).
 *   - No eBay order import (manual or scheduled).
 *
 * @package A1_WooBay
 * @since   1.1.0
 */

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

class A1WooBay_License {

	/**
	 * True if this site has an active Pro (paid or trial) license.
	 * Falls back to `true` (unrestricted) if the Freemius SDK isn't
	 * loaded yet, so local testing/dev never gets locked out by accident.
	 */
	public static function is_premium(): bool {
		if ( ! function_exists( 'aw_fs' ) ) {
			return true;
		}
		return (bool) aw_fs()->can_use_premium_code();
	}

	/**
	 * Count of listings actively taking up a "slot" on eBay (verified or
	 * live). Informational only - as of 2026-08-12 this no longer gates
	 * anything, per WP.org Guideline 5 (listing count can't be capped on
	 * the free plan since the underlying feature is fully present).
	 */
	public static function count_gated_listings(): int {
		$query = new WP_Query( array(
			'post_type'      => 'product',
			'post_status'    => 'any',
			'posts_per_page' => -1,
			'fields'         => 'ids',
			'meta_query'     => array(
				array(
					'key'     => A1WooBay_Product_Meta::META_EBAY_STATUS,
					'value'   => array(
						A1WooBay_Product_Meta::STATUS_VERIFIED,
						A1WooBay_Product_Meta::STATUS_ACTIVE,
					),
					'compare' => 'IN',
				),
			),
		) );
		return (int) $query->post_count;
	}

	/**
	 * Whether a NEW listing is allowed to proceed. Always true - listing
	 * creation is a core capability and isn't capped on either plan (see
	 * class docblock). Kept as a method (rather than removed outright) so
	 * callers don't need to change; free vs. Pro differentiation happens
	 * via allow_image_sync() / allow_order_import() instead, which gate
	 * genuinely separate, code-stripped features.
	 */
	public static function can_add_listing( int $product_id ): bool {
		return true;
	}

	/** Public alias used by the listings page counter. */
	public static function get_active_listing_count(): int {
		return self::count_gated_listings();
	}

	/**
	 * Whether image URLs should be sent to eBay for this listing.
	 * Free plan does not sync product photos.
	 */
	public static function allow_image_sync(): bool {
		return self::is_premium();
	}

	/**
	 * Whether a new eBay profile can be created. Always true - profile
	 * count isn't capped on either plan, same reasoning as
	 * can_add_listing() above.
	 */
	public static function can_add_profile(): bool {
		return true;
	}

	/**
	 * Whether eBay order import (manual or scheduled) is allowed at all.
	 * Free plan does not include order import.
	 */
	public static function allow_order_import(): bool {
		return self::is_premium();
	}

	/** User-facing message for the order-import upsell. */
	public static function order_import_message(): string {
		return __( 'eBay order import is a Pro feature. Upgrade to A1 Marketplace Sync with eBay for WooCommerce Pro to import orders automatically.', 'a1-marketplace-sync-with-ebay-for-woocommerce' );
	}
}
