<?php
/**
 * Pricing / Upgrade section.
 * When Freemius is connected, its built-in account/pricing page is rendered.
 * Otherwise, a comparison of plans is shown.
 */
if ( function_exists( 'ffs_fs' ) ) :
    return;
endif;

$is_pro = fast_fuzzy_search_is_pro();

$plans = [
    'free' => [
        'name'  => __('Free', 'fast-fuzzy-search'),
        'tag'   => __('Forever', 'fast-fuzzy-search'),
        'price' => '$0',
        'period' => __('/ forever', 'fast-fuzzy-search'),
        'icon'  => 'bolt',
        'featured' => false,
        'cta'  => [
            'label' => $is_pro ? __('Plan not available', 'fast-fuzzy-search') : __('Current plan', 'fast-fuzzy-search'),
            'href'  => null,
            'disabled' => true,
        ],
        'features' => [
            ['label' => __('Fuzzy search for posts & pages', 'fast-fuzzy-search'), 'included' => true],
            ['label' => __('WooCommerce product search', 'fast-fuzzy-search'),    'included' => true],
            ['label' => __('Shortcode integration', 'fast-fuzzy-search'),          'included' => true],
            ['label' => __('AJAX instant results', 'fast-fuzzy-search'),            'included' => true],
            ['label' => __('Polylang compatibility', 'fast-fuzzy-search'),         'included' => true],
            ['label' => __('Custom post type support', 'fast-fuzzy-search'),       'included' => true],
            ['label' => __('Search by SKU', 'fast-fuzzy-search'),                   'included' => true],
            ['label' => __('Search by attributes (color, size, etc.)', 'fast-fuzzy-search'), 'included' => false],
            ['label' => __('Premium themes & styling', 'fast-fuzzy-search'),       'included' => false],
            ['label' => __('Priority support', 'fast-fuzzy-search'),               'included' => false],
            ['label' => __('Custom integrations', 'fast-fuzzy-search'),            'included' => false],
        ],
    ],
    'pro' => [
        'name'  => __('Pro', 'fast-fuzzy-search'),
        'tag'   => __('Most popular', 'fast-fuzzy-search'),
        'price' => '$29',
        'period' => __('/ year', 'fast-fuzzy-search'),
        'icon'  => 'crown',
        'featured' => true,
        'cta'  => [
            'label' => $is_pro
                ? __('Active on this site', 'fast-fuzzy-search')
                : (function_exists('ffs_fs') ? __('Upgrade to Pro', 'fast-fuzzy-search') : __('Get Pro', 'fast-fuzzy-search')),
            'href'  => $is_pro
                ? null
                : (function_exists('ffs_fs') ? ffs_fs()->get_upgrade_url() : 'https://redoxbird.com/product/fast-fuzzy-search/'),
            'external' => !$is_pro && !function_exists('ffs_fs'),
            'disabled' => $is_pro,
        ],
        'features' => [
            ['label' => __('Fuzzy search for posts & pages', 'fast-fuzzy-search'), 'included' => true],
            ['label' => __('WooCommerce product search', 'fast-fuzzy-search'),    'included' => true],
            ['label' => __('Shortcode integration', 'fast-fuzzy-search'),          'included' => true],
            ['label' => __('AJAX instant results', 'fast-fuzzy-search'),            'included' => true],
            ['label' => __('Polylang compatibility', 'fast-fuzzy-search'),         'included' => true],
            ['label' => __('Custom post type support', 'fast-fuzzy-search'),       'included' => true],
            ['label' => __('Search by SKU', 'fast-fuzzy-search'),                   'included' => true],
            ['label' => __('Search by attributes (color, size, etc.)', 'fast-fuzzy-search'), 'included' => true],
            ['label' => __('Premium themes & styling', 'fast-fuzzy-search'),       'included' => true],
            ['label' => __('Priority support', 'fast-fuzzy-search'),               'included' => true],
            ['label' => __('Custom integrations', 'fast-fuzzy-search'),            'included' => false],
        ],
    ],
    'custom' => [
        'name'  => __('Custom', 'fast-fuzzy-search'),
        'tag'   => __('Tailored for you', 'fast-fuzzy-search'),
        'price' => '$299',
        'period' => __('/ project', 'fast-fuzzy-search'),
        'icon'  => 'puzzle',
        'featured' => false,
        'cta'  => [
            'label' => __('Get a quote', 'fast-fuzzy-search'),
            'href'  => 'https://redoxbird.com/request-integration/',
            'external' => true,
        ],
        'features' => [
            ['label' => __('Fuzzy search for posts & pages', 'fast-fuzzy-search'), 'included' => true],
            ['label' => __('WooCommerce product search', 'fast-fuzzy-search'),    'included' => true],
            ['label' => __('Shortcode integration', 'fast-fuzzy-search'),          'included' => true],
            ['label' => __('AJAX instant results', 'fast-fuzzy-search'),            'included' => true],
            ['label' => __('Polylang compatibility', 'fast-fuzzy-search'),         'included' => true],
            ['label' => __('Custom post type support', 'fast-fuzzy-search'),       'included' => true],
            ['label' => __('Search by SKU', 'fast-fuzzy-search'),                   'included' => true],
            ['label' => __('Search by attributes (color, size, etc.)', 'fast-fuzzy-search'), 'included' => true],
            ['label' => __('Premium themes & styling', 'fast-fuzzy-search'),       'included' => true],
            ['label' => __('Priority support', 'fast-fuzzy-search'),               'included' => true],
            ['label' => __('Custom integrations', 'fast-fuzzy-search'),            'included' => true],
        ],
    ],
];
?>

<section id="ffs-pricing" class="ffs-content-card" aria-labelledby="ffs-pricing-title">
    <header class="ffs-content-card__header">
        <span class="ffs-content-card__header-icon ffs-content-card__header-icon--accent" aria-hidden="true">
            <span class="dashicons dashicons-star-filled"></span>
        </span>
        <div class="ffs-content-card__header-text">
            <h2 class="ffs-content-card__title" id="ffs-pricing-title">
                <?php esc_html_e('Upgrade to Fast Fuzzy Search Pro', 'fast-fuzzy-search'); ?>
            </h2>
            <p class="ffs-content-card__subtitle">
                <?php esc_html_e('Unlock advanced search, premium themes, and priority support.', 'fast-fuzzy-search'); ?>
            </p>
        </div>
    </header>

    <div class="ffs-content-card__body ffs-content-card__body--padded">
        <div class="ffs-pricing-grid">
            <?php foreach ($plans as $plan_key => $plan): ?>
                <article class="ffs-pricing-card<?php echo !empty($plan['featured']) ? ' ffs-pricing-card--featured' : ''; ?>">
                    <div class="ffs-pricing-card__header">
                        <span class="ffs-pricing-card__icon" aria-hidden="true">
                            <img
                                src="<?php echo esc_url(FAST_FUZZY_SEARCH_DIR_URL . 'icons/' . $plan['icon'] . '.svg'); ?>"
                                alt=""
                                loading="lazy"
                            />
                        </span>
                        <div class="ffs-pricing-card__plan">
                            <p class="ffs-pricing-card__plan-name"><?php echo esc_html($plan['name']); ?></p>
                            <p class="ffs-pricing-card__plan-tag"><?php echo esc_html($plan['tag']); ?></p>
                        </div>
                    </div>

                    <div class="ffs-pricing-card__price">
                        <span class="ffs-pricing-card__price-amount"><?php echo esc_html($plan['price']); ?></span>
                        <span class="ffs-pricing-card__price-period"><?php echo esc_html($plan['period']); ?></span>
                    </div>

                    <ul class="ffs-pricing-card__features">
                        <?php foreach ($plan['features'] as $feature): ?>
                            <li class="ffs-pricing-card__feature <?php echo empty($feature['included']) ? 'ffs-pricing-card__feature--missing' : ''; ?>">
                                <?php if (!empty($feature['included'])): ?>
                                    <svg class="ffs-pricing-card__feature-icon" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                                        <path fill-rule="evenodd" d="M16.704 5.29a1 1 0 010 1.42l-7.5 7.5a1 1 0 01-1.42 0l-3.5-3.5a1 1 0 111.42-1.42L8.5 12.08l6.79-6.79a1 1 0 011.414 0z" clip-rule="evenodd"/>
                                    </svg>
                                <?php else: ?>
                                    <svg class="ffs-pricing-card__feature-icon ffs-pricing-card__feature-icon--missing" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                                        <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"/>
                                    </svg>
                                <?php endif; ?>
                                <span class="ffs-pricing-card__feature-text"><?php echo esc_html($feature['label']); ?></span>
                            </li>
                        <?php endforeach; ?>
                    </ul>

                    <?php
                    $cta       = $plan['cta'];
                    $cta_label = $cta['label'] ?? '';
                    $cta_href  = $cta['href']  ?? null;
                    $cta_ext   = !empty($cta['external']);
                    $cta_disabled = !empty($cta['disabled']) || empty($cta_href);
                    ?>
                    <div class="ffs-pricing-card__cta">
                        <?php if ($cta_disabled): ?>
                            <button type="button" class="button button-primary ffs-w-full" disabled>
                                <?php echo esc_html($cta_label); ?>
                            </button>
                        <?php else: ?>
                            <a
                                href="<?php echo esc_url($cta_href); ?>"
                                class="button button-primary ffs-w-full"
                                <?php if ($cta_ext): ?>target="_blank" rel="noopener"<?php endif; ?>
                            >
                                <?php echo esc_html($cta_label); ?>
                                <?php if ($cta_ext): ?>
                                    <span class="dashicons dashicons-external" aria-hidden="true" style="margin-left:4px;"></span>
                                <?php endif; ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </article>
            <?php endforeach; ?>
        </div>
    </div>
</section>
