<?php
/**
 * Centralized license/tier check.
 *
 * All Pro and Business gating in the plugin goes through this class. It wraps
 * Freemius's `can_use_premium_code()` / `is_plan()` and adds:
 *   - a `gofactox_can_use_premium_code` filter for staging/dev environments
 *   - a one-shot `gofactox_dev_unlock_premium` option that overrides without code
 *   - a `gofactox_can_use_business_code` filter / equivalent for the Business tier
 *
 * Usage:
 *   if ( GOFACTOX_License::is_pro() ) { ... }
 *   if ( GOFACTOX_License::is_business() ) { ... }
 *
 * @package FacturX_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

class GOFACTOX_License {

    /**
     * Is the site eligible for Pro features?
     *
     * Pro = Pro plan, Business plan (Business contains Pro), or an explicit override.
     */
    public static function is_pro(): bool {
        $can = function_exists( 'gofactox_fs' ) && gofactox_fs()->can_use_premium_code();

        if ( get_option( 'gofactox_dev_unlock_premium', '0' ) === '1' ) {
            $can = true;
        }

        /**
         * Filter the Pro-tier check.
         *
         * Useful for staging environments, integration tests, or paid customers
         * whose Freemius opt-in cannot complete (e.g. no outgoing SMTP).
         */
        return (bool) apply_filters( 'gofactox_can_use_premium_code', $can );
    }

    /**
     * Is the site eligible for Business features?
     *
     * Business = Business plan, trial of Business, or explicit override. By default
     * Business implies Pro, but Pro does NOT imply Business.
     */
    public static function is_business(): bool {
        $can = function_exists( 'gofactox_fs' )
            && gofactox_fs()->can_use_premium_code()
            && gofactox_fs()->is_plan_or_trial( 'business', true );

        if ( get_option( 'gofactox_dev_unlock_premium', '0' ) === '1' ) {
            // The dev override unlocks the highest tier so all features are reachable in testing.
            $can = true;
        }

        /**
         * Filter the Business-tier check.
         */
        return (bool) apply_filters( 'gofactox_can_use_business_code', $can );
    }
}
