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

class FORMFORGE_Upgrade_Prompt {
    private static $instance = null;

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

    private function __construct() {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_styles']);
    }

    /**
 * Enqueue inline CSS only on FormForge admin pages.
 */
    public function enqueue_styles($hook) {
        $screen = get_current_screen();
        if (!$screen || strpos($screen->id, 'formforge') === false) {
            return;
        }

        wp_add_inline_style('formforge-admin', $this->get_inline_css());
    }

    /**
 * Render a full-page lock overlay for a gated feature.
 *
 * @param string $feature_slug
 */
    public function locked_page($feature_slug) {
        $license  = FORMFORGE_License::instance();
        $features = $license->get_features();
        $feature  = $features[$feature_slug] ?? null;
        $name     = $feature['name'] ?? ucfirst(str_replace('_', ' ', $feature_slug));
        $desc     = $feature['description'] ?? __('This feature requires FormForge Pro.', 'formforge');
        $url      = $license->upgrade_url();

        echo '<div class="wrap ff-admin">';
        if (class_exists('FORMFORGE_Admin_Tabs')) { FORMFORGE_Admin_Tabs::render(); }
        echo '<div class="ff-upgrade-locked-page">';
        echo '<div class="ff-upgrade-locked-icon">&#128274;</div>';
        echo '<h2 class="ff-upgrade-locked-title">' . esc_html($name) . '</h2>';
        echo '<p class="ff-upgrade-locked-desc">' . esc_html($desc) . '</p>';
        echo '<a href="' . esc_url($url) . '" class="ff-upgrade-cta-button" target="_blank">';
        echo esc_html__('Upgrade to Pro', 'formforge');
        echo '</a>';
        echo '</div>';
        echo '</div>';
    }

    /**
 * Return HTML for an inline lock badge with tooltip.
 *
 * @param string $feature_slug
 * @return string
 */
    public function inline_lock($feature_slug) {
        $license  = FORMFORGE_License::instance();
        $features = $license->get_features();
        $feature  = $features[$feature_slug] ?? null;
        $name     = $feature['name'] ?? ucfirst(str_replace('_', ' ', $feature_slug));
        $url      = $license->upgrade_url();

        $html  = '<span class="ff-upgrade-inline-lock">';
        $html .= '<span class="ff-upgrade-lock-icon">&#128274;</span>';
        $html .= '<span class="ff-upgrade-lock-tooltip">';
        $html .= esc_html(sprintf(
            /* translators: %s: feature name */
            __('%s requires Pro. Upgrade to unlock.', 'formforge'),
            $name
        ));
        $html .= ' <a href="' . esc_url($url) . '" target="_blank">' . esc_html__('Upgrade', 'formforge') . '</a>';
        $html .= '</span>';
        $html .= '</span>';

        return $html;
    }

    /**
 * Render an HTML comparison table of Free vs Pro features.
 *
 * @return string
 */
    public function feature_comparison_table() {
        $features = FORMFORGE_License::instance()->get_features();

        $html  = '<div class="ff-admin">';
        $html .= '<table class="ff-upgrade-comparison-table">';
        $html .= '<thead><tr>';
        $html .= '<th>' . esc_html__('Feature', 'formforge') . '</th>';
        $html .= '<th>' . esc_html__('Free', 'formforge') . '</th>';
        $html .= '<th>' . esc_html__('Pro', 'formforge') . '</th>';
        $html .= '</tr></thead>';
        $html .= '<tbody>';

        foreach ($features as $slug => $feature) {
            $name    = $feature['name'] ?? ucfirst(str_replace('_', ' ', $slug));
            $in_free = !empty($feature['free']);
            $in_pro  = true;

            $html .= '<tr>';
            $html .= '<td>' . esc_html($name) . '</td>';
            $html .= '<td class="ff-upgrade-check-cell">' . ($in_free ? '&#10003;' : '&#8212;') . '</td>';
            $html .= '<td class="ff-upgrade-check-cell">' . ($in_pro ? '&#10003;' : '&#8212;') . '</td>';
            $html .= '</tr>';
        }

        $html .= '</tbody></table>';
        $html .= '</div>';

        return $html;
    }

    /**
 * Return a Pro badge span.
 *
 * @return string
 */
    public function pro_badge() {
        return '<span class="ff-pro-badge">PRO</span>';
    }

    /**
 * Render a dismissible trial days-remaining banner.
 */
    public function maybe_show_trial_banner() {
        $license = FORMFORGE_License::instance();

        if (!method_exists($license, 'is_trial') || !$license->is_trial()) {
            return;
        }

        $days_remaining = method_exists($license, 'trial_days_remaining')
            ? (int) $license->trial_days_remaining()
            : 0;

        if ($days_remaining <= 0) {
            return;
        }

        $dismissed = get_user_meta(get_current_user_id(), 'ff_trial_banner_dismissed', true);
        if ($dismissed) {
            return;
        }

        $url = $license->upgrade_url();

        echo '<div class="ff-admin">';
        echo '<div class="ff-upgrade-trial-banner" id="ff-trial-banner">';
        echo '<p>';
        echo esc_html(sprintf(
            /* translators: %d: number of days remaining in trial */
            _n(
                'Your FormForge Pro trial expires in %d day.',
                'Your FormForge Pro trial expires in %d days.',
                $days_remaining,
                'formforge'
            ),
            $days_remaining
        ));
        echo ' <a href="' . esc_url($url) . '" target="_blank">' . esc_html__('Upgrade now', 'formforge') . '</a>';
        echo '</p>';
        echo '<button type="button" class="ff-upgrade-trial-dismiss" onclick="jQuery.post(ajaxurl,{action:\'ff_dismiss_trial_banner\',_wpnonce:\'' . esc_js(wp_create_nonce('ff_dismiss_trial')) . '\'},function(){document.getElementById(\'ff-trial-banner\').style.display=\'none\'});">';
        echo '&times;';
        echo '</button>';
        echo '</div>';
        echo '</div>';
    }

    /**
 * Inline CSS scoped under .ff-admin.
 *
 * @return string
 */
    private function get_inline_css() {
        return '
.ff-admin .ff-upgrade-locked-page {
    text-align: center;
    padding: 80px 20px;
    max-width: 480px;
    margin: 60px auto;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 8px;
}
.ff-admin .ff-upgrade-locked-icon {
    font-size: 48px;
    margin-bottom: 16px;
}
.ff-admin .ff-upgrade-locked-title {
    font-size: 22px;
    margin: 0 0 8px;
}
.ff-admin .ff-upgrade-locked-desc {
    color: #666;
    margin: 0 0 24px;
}
.ff-admin .ff-upgrade-cta-button {
    display: inline-block;
    padding: 10px 28px;
    background: #4f46e5;
    color: #fff;
    text-decoration: none;
    border-radius: 4px;
    font-weight: 600;
}
.ff-admin .ff-upgrade-cta-button:hover {
    background: #4338ca;
    color: #fff;
}
.ff-admin .ff-upgrade-inline-lock {
    position: relative;
    cursor: pointer;
    display: inline-block;
}
.ff-admin .ff-upgrade-lock-icon {
    font-size: 14px;
    vertical-align: middle;
}
.ff-admin .ff-upgrade-lock-tooltip {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: #1e1e1e;
    color: #fff;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 14px;
    white-space: nowrap;
    z-index: 100;
}
.ff-admin .ff-upgrade-lock-tooltip a {
    color: #a5b4fc;
}
.ff-admin .ff-upgrade-inline-lock:hover .ff-upgrade-lock-tooltip {
    display: block;
}
.ff-admin .ff-upgrade-comparison-table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    background: #fff;
}
.ff-admin .ff-upgrade-comparison-table th,
.ff-admin .ff-upgrade-comparison-table td {
    padding: 10px 16px;
    border: 1px solid #ddd;
    text-align: left;
}
.ff-admin .ff-upgrade-comparison-table thead th {
    background: #f7f7f7;
    font-weight: 600;
}
.ff-admin .ff-upgrade-check-cell {
    text-align: center;
    font-size: 16px;
}
.ff-admin .ff-pro-badge {
    display: inline-block;
    background: #4f46e5;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    padding: 2px 6px;
    border-radius: 3px;
    text-transform: uppercase;
    vertical-align: middle;
    line-height: 1.4;
}
.ff-admin .ff-upgrade-trial-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #fef3c7;
    border: 1px solid #f59e0b;
    border-radius: 4px;
    padding: 10px 16px;
    margin: 10px 0 20px;
}
.ff-admin .ff-upgrade-trial-banner p {
    margin: 0;
    font-size: 14px;
}
.ff-admin .ff-upgrade-trial-banner a {
    font-weight: 600;
}
.ff-admin .ff-upgrade-trial-dismiss {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: #92400e;
    padding: 0 4px;
    line-height: 1;
}
';
    }
}
