<?php
/**
 * Algo Forms Free / Pro gating.
 *
 * Free keeps existing behaviour. Pro unlocks spam suite, delivery log,
 * and submission inbox tools (detail / notes / resend).
 *
 * Enable Pro via:
 * - define( 'ALGO_FORMS_PRO', true ); in wp-config.php, or
 * - a valid AFPRO key from algorisys.com (Algo Forms → Pro), or
 * - local / publisher unlock on development and Algorisys sites
 *
 * @package AlgoForms
 */

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

require_once ALGO_FORMS_PLUGIN_DIR . 'includes/class-algo-freemium-license.php';

/**
 * Shared license client for this plugin.
 *
 * @return Algo_Freemium_License
 */
function algo_forms_license()
{
    static $client = null;
    if ($client instanceof Algo_Freemium_License) {
        return $client;
    }

    $client = new Algo_Freemium_License(array(
        'product'         => 'algo-forms-pro',
        'prefix'          => 'AFPRO',
        'option'          => 'algo_forms_license',
        'cron'            => 'algo_forms_license_recheck',
        'textdomain'      => 'algo-forms',
        'api_url_const'   => 'ALGO_FORMS_LICENSE_API_URL',
        'allow_dev_const' => 'ALGO_FORMS_ALLOW_DEV_UNLOCK',
        'publisher_hosts' => array('algorisys.com', 'www.algorisys.com'),
        'filter_api'      => 'algo_forms_license_api_url',
        'filter_dev'      => 'algo_forms_allow_local_unlock',
    ));

    return $client;
}

/**
 * Daily remote license re-check.
 *
 * @return void
 */
function algo_forms_license_recheck()
{
    algo_forms_license()->recheck();
}
add_action('algo_forms_license_recheck', 'algo_forms_license_recheck');

/**
 * Whether Pro features are active on this site.
 *
 * @return bool
 */
function algo_forms_is_pro()
{
    $enabled = false;

    if (defined('ALGO_FORMS_PRO') && ALGO_FORMS_PRO) {
        $enabled = true;
    } elseif (algo_forms_license()->is_active()) {
        $enabled = true;
    } elseif (algo_forms_license()->allow_local_unlock()) {
        // Publisher / local sites that already flipped the old Activate toggle.
        $opts = algo_forms_get_options();
        $enabled = !empty($opts['pro_active']);
    }

    /**
     * Filter Pro entitlement.
     *
     * @param bool $enabled Whether Pro is active.
     */
    return (bool) apply_filters('algo_forms_is_pro', $enabled);
}

/**
 * Public buy URL for Algo Forms Pro.
 *
 * @return string
 */
function algo_forms_pro_buy_url()
{
    if (function_exists('algo_commerce_buy_url')) {
        $local = algo_commerce_buy_url('algo-forms-pro');
        if (is_string($local) && $local !== '') {
            return $local;
        }
    }

    return 'https://algorisys.com/buy/algo-forms-pro/';
}

/**
 * Feature flags for Pro capabilities.
 *
 * @param string $feature Feature slug.
 * @return bool
 */
function algo_forms_pro_feature($feature)
{
    if (!algo_forms_is_pro()) {
        return false;
    }

    $map = array(
        'spam' => true,
        'delivery_log' => true,
        'submission_inbox' => true,
        'resend' => true,
        'notes' => true,
    );

    $allowed = !empty($map[$feature]);

    /**
     * Filter individual Pro features.
     *
     * @param bool   $allowed Whether the feature is available.
     * @param string $feature Feature slug.
     */
    return (bool) apply_filters('algo_forms_pro_feature', $allowed, $feature);
}

/**
 * Render a compact Pro badge.
 *
 * @return string
 */
function algo_forms_pro_badge_html()
{
    return '<span class="af-pro-badge">' . esc_html__('Pro', 'algo-forms') . '</span>';
}

/**
 * Upsell panel when a Pro page is locked.
 *
 * @param string $title
 * @param string $blurb
 * @return void
 */
function algo_forms_render_pro_upsell($title, $blurb)
{
    algo_forms_admin_shell_start($title, $blurb, 'lavender');
    ?>
    <div class="af-panel af-pro-upsell">
        <div class="af-pro-upsell-icon" aria-hidden="true"><?php echo algo_forms_admin_icon('warning'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
        <h2><?php esc_html_e('This is an Algo Forms Pro feature', 'algo-forms'); ?></h2>
        <p><?php echo esc_html($blurb); ?></p>
        <p>
            <a class="button button-primary" href="<?php echo esc_url(admin_url('admin.php?page=algo-forms-pro')); ?>">
                <?php esc_html_e('Open Pro settings', 'algo-forms'); ?>
            </a>
            <a class="button" href="<?php echo esc_url(algo_forms_pro_buy_url()); ?>" target="_blank" rel="noopener noreferrer">
                <?php esc_html_e('Buy Algo Forms Pro', 'algo-forms'); ?>
            </a>
            <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=algo-forms')); ?>">
                <?php esc_html_e('Back to Dashboard', 'algo-forms'); ?>
            </a>
        </p>
        <ul class="af-pro-list">
            <li><?php esc_html_e('Spam protection (honeypot, timing, rate limits)', 'algo-forms'); ?></li>
            <li><?php esc_html_e('Full email delivery log', 'algo-forms'); ?></li>
            <li><?php esc_html_e('Submission inbox with notes and resend', 'algo-forms'); ?></li>
        </ul>
    </div>
    <?php
    algo_forms_admin_shell_end();
}
