<?php
if (!defined("ABSPATH")) exit;

// Pinery Flow — Freemius License Integration
// =============================================================================
// Add these to wp-config.php to activate:
//
//   define('PINERY_FLOW_FREEMIUS_ID',  1234);
//   define('PINERY_FLOW_FREEMIUS_PK',  'pk_abc123...');
//
// Sign up at https://freemius.com, create a Plugin product.
// =============================================================================

if (!function_exists('pinery_flow_fs')) {
    /**
     * Helper to access the Freemius SDK instance for Pinery Flow.
     *
     * @return Freemius
     */
    function pinery_flow_fs() {
        global $pinery_flow_fs;

        if (!isset($pinery_flow_fs)) {
            require_once PINERY_FLOW_DIR . '/freemius/start.php';

            $pinery_flow_fs = fs_dynamic_init(array(
                'id'                  => PINERY_FLOW_FREEMIUS_ID,
                'slug'                => 'pinery-flow',
                'type'                => 'plugin',
                'public_key'          => defined('PINERY_FLOW_FREEMIUS_PK') ? PINERY_FLOW_FREEMIUS_PK : '',
                'is_premium'          => true,
                'has_premium_version' => false,
                'has_paid_plans'      => true,
                'is_org_compliant'    => true,
                'bundle_plan'         => 'pinery_bundle',
                'menu'                => array(
                    'slug'    => 'pinery-flow-license',
                    'parent'  => 'admin.php?page=pinery-flow',
                    'account' => true,
                    'contact' => false,
                    'support' => false,
                ),
            ));
        }

        return $pinery_flow_fs;
    }

    // Initialize Freemius
    pinery_flow_fs();

    do_action('pinery_flow_fs_loaded');
}

// ── License Gating ──
// Pipeline execution, API usage, and all automated features require a valid license.

/**
 * Check whether the user has a valid license for Pinery Flow.
 * Returns true only when Freemius is active AND the user is paying (or on a valid trial).
 */
function pinery_flow_has_license() {
    // Freemius not configured yet — allow usage (development / pre-launch)
    if (!function_exists('pinery_flow_fs')) return true;

    // Freemius is active — enforce license check
    return pinery_flow_fs()->is_paying_or_trial();
}

// ── Lifecycle Callbacks ──

/**
 * Called when the user opts in / activates a license.
 */
function pinery_flow_fs_after_optin() {
    update_option('pinery_flow_freemius_connected', true);
}

/**
 * Called when the user uninstalls the plugin through Freemius.
 * Clean up plugin-specific data — runs via AJAX; theme functions may not be available.
 */
function pinery_flow_fs_uninstall_cleanup() {
    delete_option('pinery_flow_freemius_connected');
    delete_option('pinery_flow_api_key');
    delete_option('pinery_flow_creators_client_id');
    delete_option('pinery_flow_creators_client_secret');
}

pinery_flow_fs()->add_action('after_optin', 'pinery_flow_fs_after_optin');
pinery_flow_fs()->add_action('after_uninstall', 'pinery_flow_fs_uninstall_cleanup');
