<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class SS_Upgrade_Popup {

    public static function init() {
        add_action('wp_enqueue_scripts', [self::class, 'enqueue']);
        add_action('admin_enqueue_scripts', [self::class, 'enqueue']);
        add_action('admin_footer', [self::class, 'render']);
        add_action('wp_footer', [self::class, 'render']);
    }

    public static function enqueue() {

        $ver = defined('SEARCHSHIFTER_VERSION') ? SEARCHSHIFTER_VERSION : time();

        wp_register_style(
            'ss-upgrade-popup',
            SEARCHSHIFTER_PLUGIN_URL . 'assets/css/ss-upgrade-popup.css',
            [],
            $ver
        );

        wp_register_script(
            'ss-upgrade-popup',
            SEARCHSHIFTER_PLUGIN_URL . 'assets/js/ss-upgrade-popup.js',
            ['jquery'],
            $ver,
            true
        );

        $license = class_exists('SS_Licensing') ? SS_Licensing::get_cached_status() : [];
        $current_plan = strtolower($license['plan'] ?? 'free');

        wp_localize_script('ss-upgrade-popup', 'SS_UPGRADE', [
            'currentPlan' => $current_plan,
            'pricingUrl'  => 'https://searchshifter.ai/pricing/',

            'plans' => [
                'free' => [
                    'name' => 'Free',
                    'benefits' => ['Term name & definition', 'A–Z index']
                ],
                'pro' => [
                    'name' => 'Pro',
                    'benefits' => ['Why it Matters', 'Simple Example', 'Related Terms']
                ],
                'authority' => [
                    'name' => 'Authority',
                    'benefits' => ['Video embed', 'Auto linking', 'Dataset import']
                ]
            ],

            'i18n' => [
                'title' => __('This feature requires a higher plan', 'searchshifter')
            ]
        ]);

        wp_enqueue_style('ss-upgrade-popup');
        wp_enqueue_script('ss-upgrade-popup');
    }

    public static function render() {

        static $printed = false;
        if ($printed) return;
        $printed = true;
        ?>

        <div id="ss-upgrade-modal" class="ss-upgrade-modal" aria-hidden="true" role="dialog" aria-modal="true">
            <div class="ss-upgrade-modal-backdrop" data-ss-close></div>

            <div class="ss-upgrade-modal-inner" role="document">

                <button class="ss-upgrade-close" type="button" data-ss-close>&times;</button>

                <div class="ss-upgrade-content">

                    <h2 id="ss-upgrade-title"></h2>
                    <p id="ss-upgrade-feature"></p>

                    <div id="ss-upgrade-compare"></div>

                    <div class="ss-upgrade-actions">
                        <a id="ss-upgrade-cta" class="ss-upgrade-cta" target="_blank">
                            Upgrade to Pro
                        </a>

                        <button class="ss-upgrade-secondary" type="button" data-ss-close>
                            Close
                        </button>
                    </div>

                </div>
            </div>
        </div>

        <?php
    }
}