<?php

namespace lucidpen;

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

if (!function_exists('lucidpen_fs')) {
    // Create a helper function for easy SDK access.
    function lucidpen_fs()
    {
        global $lucidpen_fs;

        if (!isset($lucidpen_fs)) {
            // Include Freemius SDK.
            require_once LCPN_PLUGIN_DIR . '/vendor/freemius/start.php';
            $lucidpen_fs = fs_dynamic_init(array(
                'id' => '20105',
                'slug' => 'lucidpen',
                'premium_slug' => 'lucidpen-enigtech-ai-premium',
                'type' => 'plugin',
                'public_key' => 'pk_2e895952cbad208fd46f317d6cec5',
                'is_premium' => false,
                'has_addons' => true,
                'has_paid_plans' => false,
                'is_org_compliant' => true,   // Required for WordPress.org directory compliance
                'menu' => array(
                    'slug' => 'lucidpen',
                    'account' => false,
                    'support' => false,
                    'parent' => array(
                        'slug' => 'options-general.php',
                    ),
                ),
            ));
        }

        return $lucidpen_fs;
    }

    // Init Freemius.
    lucidpen_fs();
    // Signal that SDK was initiated.
    do_action('lucidpen_fs_loaded');
}


use core\SingletonTrait;
use core\Result;


class LicenseManager
{
    use SingletonTrait;

    private $lucidpen_fs;

    public function __construct()
    {

    }

    public function get_license_key(): string
    {
        // Get the license key from Freemius
        if (function_exists('lucidpen_fs')) {
            return lucidpen_fs()->get_license_key();
        }

        return '';
    }

    public function check_license(): Result
    {
        if (!isset($GLOBALS['fs_plugin']) || !$GLOBALS['fs_plugin']->can_use_premium_code()) {
            return Result::failure("EnigTech Hosted AI requires a valid premium license.");
        }

        return Result::success();
    }
}
