<?php
/**
 * Reusable Kantan SEO upsell box.
 *
 * Drop a contextual, value-first upgrade prompt onto any Analytics tab that has
 * SEO-relevant data. It is a no-op when Kantan SEO is already active (so paying
 * customers never see it), and its promotional intensity is gated by the
 * distribution channel: a full card with an Upgrade button on the directly
 * distributed build, a restrained card with a soft link on the wp.org build.
 */

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

/**
 * Echo a Kantan SEO upsell box.
 *
 * @param array $args {
 *     @type string $heading Card heading.
 *     @type string $body    One- or two-sentence value proposition.
 * }
 */
function kantan_render_seo_upsell($args = array()) {
    // Never upsell when the paid plugin is already installed.
    if (defined('KANTAN_SEO_VERSION')) {
        return;
    }

    $heading = isset($args['heading']) ? $args['heading'] : __('Go further with Kantan SEO', 'kantan-analytics');
    $body    = isset($args['body']) ? $args['body'] : '';
    $channel = defined('KANTAN_ANALYTICS_CHANNEL') ? KANTAN_ANALYTICS_CHANNEL : 'trykantan';
    $url     = 'https://trykantan.com/seo/';

    if ($channel !== 'wporg') : ?>
        <div class="rounded-xl border border-blue-200 bg-gradient-to-br from-blue-50 to-indigo-50 p-6 mt-8">
            <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                <div>
                    <span class="inline-block text-xs font-semibold uppercase tracking-wider text-blue-700 bg-blue-100 rounded px-2 py-0.5 mb-2">Kantan SEO</span>
                    <h2 class="text-lg font-bold text-gray-900"><?php echo esc_html($heading); ?></h2>
                    <?php if ($body) : ?><p class="text-gray-600 text-sm mt-1 max-w-2xl"><?php echo esc_html($body); ?></p><?php endif; ?>
                </div>
                <a href="<?php echo esc_url($url); ?>" target="_blank" rel="noopener" class="flex-shrink-0 text-center px-4 py-2.5 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 transition text-sm whitespace-nowrap"><?php esc_html_e('Upgrade to Kantan SEO', 'kantan-analytics'); ?></a>
            </div>
        </div>
    <?php else : ?>
        <div class="rounded-xl border border-gray-200 bg-gray-50 p-6 mt-8">
            <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                <div>
                    <h2 class="text-base font-semibold text-gray-900"><?php echo esc_html($heading); ?></h2>
                    <?php if ($body) : ?><p class="text-gray-600 text-sm mt-1 max-w-2xl"><?php echo esc_html($body); ?></p><?php endif; ?>
                </div>
                <a href="<?php echo esc_url($url); ?>" target="_blank" rel="noopener" class="flex-shrink-0 text-sm font-medium text-blue-600 hover:text-blue-700 hover:underline whitespace-nowrap"><?php esc_html_e('Learn more about Kantan SEO', 'kantan-analytics'); ?> &rarr;</a>
            </div>
        </div>
    <?php endif;
}
