<?php
namespace WalletUpPro\Core\Manager;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Premium Features Manager - WordPress.org compliant version
 */
class PremiumFeaturesManager
{
    private static $instance = null;
    private $premium_status = false;

    public static function getInstance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Private constructor
     */
    private function __construct()
    {
        // Check premium status on init
        $this->checkPremiumStatus();

        // Add hooks for premium status updates
        add_action('admin_init', array($this, 'checkPremiumStatus'));

        if (function_exists('wallet_up_fs')) {
            wallet_up_fs()->add_action('after_license_activation', array($this, 'refreshPremiumStatus'));
            wallet_up_fs()->add_action('after_license_change', array($this, 'refreshPremiumStatus'));
        }
    }

    public function checkPremiumStatus(): bool
    {
        if (function_exists('wallet_up_fs') && is_object(wallet_up_fs())) {
            $this->premium_status = wallet_up_fs()->can_use_premium_code();
        } else {
            $this->premium_status = false;
        }

        return $this->premium_status;
    }

    /**
     * Force refresh premium status
     */
    public function refreshPremiumStatus(): void
    {
        $this->checkPremiumStatus();
    }

    public function addScriptData(array $data): array
    {
        $data['isPremium'] = $this->premium_status;

        if (function_exists('wallet_up_fs') && is_object(wallet_up_fs())) {
            $fs = wallet_up_fs();

            if (method_exists($fs, 'checkout_url')) {
                $data['checkoutUrl'] = $fs->checkout_url();
            }

            if (method_exists($fs, 'get_upgrade_url')) {
                $data['upgradeUrl'] = $fs->get_upgrade_url();
            }
        }

        return $data;
    }
}