<?php
/**
 * Upgrade Prompt - renders Pro upgrade prompts, lock overlays, and comparison tables
 *
 * @package LangForge
 */

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

class LF_Upgrade_Prompt {

    private static $instance = null;

    /**
 * Feature descriptions for lock overlays.
 *
 * @var array<string, string>
 */
    private $feature_descriptions = [];

    /**
 * Get the singleton instance.
 *
 * @return LF_Upgrade_Prompt
 */
    public static function instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->feature_descriptions = [
            'ai_translation'      => __('Translate your content automatically with AI-powered engines.', 'langforge'),
            'visual_editor'       => __('Side-by-side visual editor for translating posts and pages.', 'langforge'),
            'bulk_actions'        => __('Duplicate and translate your entire site in one click.', 'langforge'),
            'translation_memory'  => __('Reuse previously translated segments for consistency and speed.', 'langforge'),
            'glossary'            => __('Maintain brand terminology dictionaries across languages.', 'langforge'),
            'woocommerce'         => __('Translate WooCommerce products, categories, and checkout.', 'langforge'),
            'import_export'       => __('Import and export translations in XLIFF and CSV formats.', 'langforge'),
            'analytics'           => __('Track translation progress and activity with visual dashboards.', 'langforge'),
            'roles'               => __('Assign translator roles and send email notifications.', 'langforge'),
            'frontend_editor'     => __('Translate directly on the live page with click-to-edit.', 'langforge'),
            'translation_qa'      => __('AI-powered quality checks for your translations.', 'langforge'),
            'content_diff'        => __('See exactly what changed in source content since last translation.', 'langforge'),
            'live_preview'        => __('Preview translations before publishing.', 'langforge'),
            'media_translation'   => __('Use different images for each language.', 'langforge'),
            'comment_translation' => __('Auto-translate visitor comments into the current language.', 'langforge'),
            'unlimited_languages' => __('Add unlimited languages to your site.', 'langforge'),
        ];

        $this->init_hooks();
    }

    private function init_hooks() {
        add_action('admin_enqueue_scripts', [$this, 'enqueue_inline_styles']);

        // NOTE: LangForge intentionally does NOT touch admin notices it does not
        // own. An earlier multi-layer "notice stripper" (remove_action on foreign
        // callbacks + output-buffer filtering + CSS display:none + a JS DOM walker)
        // that hid other plugins' and WordPress-core notices on LangForge screens
        // was removed — it violated WordPress.org Detailed Plugin Guideline 11
        // (do not interfere with other plugins / do not hijack wp-admin). The
        // upsell UI is rendered only in LangForge's own containers
        // (render_upgrade_banner / locked_page / inline_lock).
    }


    
    // Inline styles
    

    /**
 * Enqueue inline CSS on LangForge admin pages.
 *
 * @param string $hook Current admin page hook.
 * @return void
 */
    public function enqueue_inline_styles($hook) {
        if (strpos($hook, 'langforge') === false && strpos($hook, 'lf-') === false && strpos($hook, 'lang-forge') === false) {
            return;
        }

        $css = $this->get_inline_css();
        wp_register_style('lf-upgrade-prompt', false, [], LF_VERSION);
        wp_enqueue_style('lf-upgrade-prompt');
        wp_add_inline_style('lf-upgrade-prompt', $css);
    }

    /**
 * Return all inline CSS for upgrade prompt components.
 *
 * @return string
 */
    private function get_inline_css() {
        return '
.lf-admin .lf-upgrade-lock-overlay {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 400px;
    padding: 48px 24px;
    text-align: center;
    background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
    border: 1px solid #e2e8f0;
    border-radius: 16px;
    max-width: 580px;
    margin: 40px auto;
}
.lf-admin .lf-upgrade-lock-overlay .lf-upgrade-lock-icon {
    font-size: 48px;
    color: #94a3b8;
    margin-bottom: 16px;
}
.lf-admin .lf-upgrade-lock-overlay h2 {
    margin: 0 0 8px;
    font-size: 24px;
    font-weight: 700;
    color: #1e293b;
}
.lf-admin .lf-upgrade-lock-overlay p {
    margin: 0 0 24px;
    font-size: 15px;
    color: #64748b;
    line-height: 1.6;
    max-width: 440px;
}
.lf-admin .lf-upgrade-lock-overlay .lf-upgrade-cta {
    display: inline-block;
    padding: 12px 32px;
    background: linear-gradient(135deg, #0EC49E, #0AA882);
    color: #fff !important;
    text-decoration: none !important;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 600;
    transition: transform 0.15s, box-shadow 0.15s;
    box-shadow: 0 2px 8px rgba(14, 196, 158, 0.3);
}
.lf-admin .lf-upgrade-lock-overlay .lf-upgrade-cta:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 16px rgba(14, 196, 158, 0.4);
    color: #fff !important;
}

/* Inline lock badge */
.lf-admin .lf-upgrade-inline-lock {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    position: relative;
    cursor: default;
}
.lf-admin .lf-upgrade-inline-lock .dashicons {
    font-size: 16px;
    width: 16px;
    height: 16px;
    color: #dba617;
}
.lf-admin .lf-upgrade-inline-lock .lf-upgrade-tooltip {
    display: none;
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    margin-bottom: 6px;
    padding: 6px 10px;
    background: #1d2327;
    color: #fff;
    font-size: 14px;
    white-space: nowrap;
    border-radius: 4px;
    z-index: 100;
}
.lf-admin .lf-upgrade-inline-lock:hover .lf-upgrade-tooltip {
    display: block;
}

/* Pro badge */
.lf-pro-badge {
    display: inline-block;
    padding: 2px 6px;
    background: #dba617;
    color: #fff;
    font-size: 14px;
    font-weight: 700;
    line-height: 1.4;
    border-radius: 3px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    vertical-align: middle;
}

/* Comparison table */
.lf-admin .lf-upgrade-comparison-table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
    background: #fff;
    border: 1px solid #e0e0e0;
    border-radius: 6px;
    overflow: hidden;
}
.lf-admin .lf-upgrade-comparison-table th,
.lf-admin .lf-upgrade-comparison-table td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid #f0f0f0;
    font-size: 14px;
}
.lf-admin .lf-upgrade-comparison-table th {
    background: #f6f7f7;
    font-weight: 600;
    color: #1d2327;
}
.lf-admin .lf-upgrade-comparison-table td:nth-child(2),
.lf-admin .lf-upgrade-comparison-table td:nth-child(3),
.lf-admin .lf-upgrade-comparison-table th:nth-child(2),
.lf-admin .lf-upgrade-comparison-table th:nth-child(3) {
    text-align: center;
    width: 100px;
}
.lf-admin .lf-upgrade-comparison-table .lf-upgrade-check {
    color: #00a32a;
    font-size: 18px;
}
.lf-admin .lf-upgrade-comparison-table .lf-upgrade-cross {
    color: #cc1818;
    font-size: 18px;
}

/* Trial banner */
.lf-admin .lf-upgrade-trial-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    margin: 10px 0;
    background: #fef8ee;
    border-left: 4px solid #dba617;
    border-radius: 0 4px 4px 0;
}
.lf-admin .lf-upgrade-trial-banner p {
    margin: 0;
    font-size: 14px;
    color: #1d2327;
}
.lf-admin .lf-upgrade-trial-banner .lf-upgrade-trial-dismiss {
    background: none;
    border: none;
    cursor: pointer;
    color: #999;
    font-size: 16px;
    padding: 0 0 0 12px;
}
.lf-admin .lf-upgrade-trial-banner .lf-upgrade-trial-dismiss:hover {
    color: #1d2327;
}
';
    }

    
    // 1. Full-page lock overlay
    

    /**
 * Render a full-page lock overlay for a Pro-only admin page.
 *
 * @param string $feature_slug Feature slug (e.g. 'visual_editor').
 * @return void
 */
    public static function locked_page($feature_slug) {
        $self        = self::instance();
        $features    = LF_License::instance()->get_features();
        $label       = $feature_slug;
        $description = '';

        foreach ($features as $f) {
            if ($f['slug'] === $feature_slug) {
                $label = $f['label'];
                break;
            }
        }

        if (isset($self->feature_descriptions[$feature_slug])) {
            $description = $self->feature_descriptions[$feature_slug];
        }

        $upgrade_url = esc_url(LF_License::upgrade_url());

        echo '<div class="wrap lf-admin">';
        if (class_exists('LF_Admin_Tabs')) { LF_Admin_Tabs::render(); }
        echo '<div style="display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:400px;padding:48px 24px;text-align:center;background:linear-gradient(135deg,#f8fafc 0%,#f1f5f9 100%);border:1px solid #e2e8f0;border-radius:16px;max-width:580px;margin:40px auto;">';
        echo '<div style="font-size:48px;color:#94a3b8;margin-bottom:16px;"><span class="dashicons dashicons-lock" style="font-size:48px;width:48px;height:48px;"></span></div>';
        echo '<h2 style="margin:0 0 8px;font-size:24px;font-weight:700;color:#1e293b;">' . esc_html(sprintf(
            /* translators: %s: the name of the Pro-only feature the user tried to access. */
            __('%s is a Pro Feature', 'langforge'),
            $label
        )) . '</h2>';

        if ($description) {
            echo '<p style="margin:0 0 24px;font-size:15px;color:#64748b;line-height:1.6;max-width:440px;">' . esc_html($description) . '</p>';
        }

        echo '<a href="' . $upgrade_url . '" target="_blank" style="display:inline-block;padding:12px 32px;background:linear-gradient(135deg,#0EC49E,#0AA882);color:#fff !important;text-decoration:none !important;border-radius:12px;font-size:15px;font-weight:600;box-shadow:0 2px 8px rgba(14,196,158,0.3);transition:transform 0.15s,box-shadow 0.15s;">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $upgrade_url already escaped via esc_url at assignment
        echo esc_html__('Upgrade to Pro', 'langforge');
        echo '</a>';
        echo '</div>';
        echo '</div>'; // .wrap
    }

    
    // 2. Inline lock badge + tooltip
    

    /**
 * Return HTML for an inline lock badge with tooltip.
 *
 * @param string $feature_slug Feature slug.
 * @return string HTML.
 */
    public static function inline_lock($feature_slug) {
        $tooltip_text = sprintf(
            /* translators: %s: upgrade URL */
            __('Requires Pro plan', 'langforge')
        );

        $html  = '<span class="lf-upgrade-inline-lock">';
        $html .= '<span class="dashicons dashicons-lock"></span>';
        $html .= self::pro_badge();
        $html .= '<span class="lf-upgrade-tooltip">' . esc_html($tooltip_text) . '</span>';
        $html .= '</span>';

        return $html;
    }

    
    // 3. Feature comparison table
    

    /**
 * Render an HTML table comparing Free vs Pro features.
 *
 * @return void
 */
    public static function feature_comparison_table() {
        $features = LF_License::instance()->get_features();

        echo '<div class="lf-admin">';
        echo '<table class="lf-upgrade-comparison-table">';
        echo '<thead><tr>';
        echo '<th>' . esc_html__('Feature', 'langforge') . '</th>';
        echo '<th>' . esc_html__('Free', 'langforge') . '</th>';
        echo '<th>' . esc_html__('Pro', 'langforge') . '</th>';
        echo '</tr></thead>';
        echo '<tbody>';

        foreach ($features as $feature) {
            $is_free = in_array('free', $feature['plans'], true);

            echo '<tr>';
            echo '<td>' . esc_html($feature['label']) . '</td>';
            echo '<td>';
            echo $is_free
                ? '<span class="lf-upgrade-check">&#10003;</span>'
                : '<span class="lf-upgrade-cross">&#10005;</span>';
            echo '</td>';
            echo '<td><span class="lf-upgrade-check">&#10003;</span></td>';
            echo '</tr>';
        }

        echo '</tbody></table>';
        echo '</div>';
    }

    
    // 4. Pro badge
    

    /**
 * Return the PRO badge HTML.
 *
 * @return string
 */
    public static function pro_badge() {
        return '<span class="lf-pro-badge" style="background:#0EC49E;color:#fff;font-size:14px;font-weight:700;padding:1px 5px;border-radius:3px;margin-left:4px;vertical-align:middle;">' . esc_html__('PRO', 'langforge') . '</span>';
    }

    
    // 5. Trial banner
    

    /**
 * Show a dismissible banner with trial days remaining.
 *
 * @return void
 */
    public static function maybe_show_trial_banner() {
        if (LF_License::get_plan() !== 'trial') {
            return;
        }

        $data = get_option('lf_license_data', []);
        if (empty($data['trial_expires'])) {
            return;
        }

        $expires   = strtotime($data['trial_expires']);
        $now       = current_time('timestamp');
        $remaining = max(0, ceil(($expires - $now) / DAY_IN_SECONDS));

        $message = sprintf(
            /* translators: %d: number of days remaining in trial */
            _n(
                'Your LangForge Pro trial expires in %d day.',
                'Your LangForge Pro trial expires in %d days.',
                $remaining,
                'langforge'
            ),
            $remaining
        );

        $upgrade_url = esc_url(LF_License::upgrade_url());

        echo '<div class="lf-admin">';
        echo '<div class="lf-upgrade-trial-banner">';
        echo '<p>' . esc_html($message) . ' ';
        echo '<a href="' . $upgrade_url . '" target="_blank">' . esc_html__('Upgrade now', 'langforge') . '</a>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $upgrade_url already escaped via esc_url at assignment
        echo '</p>';
        echo '<button type="button" class="lf-upgrade-trial-dismiss" onclick="this.closest(\'.lf-upgrade-trial-banner\').style.display=\'none\'">';
        echo '<span class="dashicons dashicons-dismiss"></span>';
        echo '</button>';
        echo '</div>';
        echo '</div>';
    }

}
