<?php
/**
 * TrackSharp Features
 *
 * Helper class for feature detection and upsell UI.
 *
 * @package TrackSharp
 */

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

class TrackSharp_Features
{
    /**
     * Whether TrackSharp should use revenue excluding VAT/tax.
     */
    public static function is_revenue_excl_tax(): bool
    {
        return (bool) get_option('tracksharp_revenue_excl_tax', false);
    }

    /**
     * GA4 event name for server-side purchases.
     *
     * Default: "purchase". Optional comparison mode: "purchase_sst".
     */
    public static function ga4_purchase_event_name(): string
    {
        return (bool) get_option('tracksharp_ga4_use_purchase_sst', false) ? 'purchase_sst' : 'purchase';
    }

    /**
     * GA4 purchase strategy.
     *
     * - ads_first (default): server-side purchase is canonical (best for Ads imports).
     * - reporting_first: browser purchase is canonical (best for GA4 session attribution).
     *
     * Reporting-first is ignored when purchase_sst comparison mode is enabled
     * (comparison mode is server-only to reduce duplicates).
     *
     * @return string 'ads_first'|'reporting_first'
     */
    public static function ga4_attribution_mode(): string
    {
        $mode = (string) get_option('tracksharp_ga4_attribution_mode', 'ads_first');
        if (!in_array($mode, ['ads_first', 'reporting_first'], true)) {
            $mode = 'ads_first';
        }

        // Comparison mode is intended for debugging and is server-only to reduce duplicates.
        if (self::ga4_purchase_event_name() === 'purchase_sst') {
            return 'ads_first';
        }

        return $mode;
    }

    /**
     * Whether Pro functionality is active for this install.
     */
    public static function is_pro_active(): bool
    {
        $active = false;

        if (function_exists('tracksharp_fs')) {
            // This IF block is auto-removed from the Free version by Freemius.
            $fs = tracksharp_fs();
            if (is_object($fs) && method_exists($fs, 'is__premium_only') && $fs->is__premium_only()) {
                try {
                    $active = method_exists($fs, 'can_use_premium_code') ? (bool) $fs->can_use_premium_code() : false;
                } catch (\Throwable $e) {
                    $active = false;
                }
            }
        }

        return $active;
    }

    /**
     * Whether premium code is present (not license state).
     */
    private static function has_premium_code(): bool
    {
        return class_exists('TrackSharp_Hasher') || class_exists('TrackSharp_Meta');
    }

    /**
     * Get upgrade URL (Freemius pricing page).
     */
    public static function get_upgrade_url(): string
    {
        if (function_exists('tracksharp_fs')) {
            try {
                $fs = tracksharp_fs();
                if (is_object($fs) && method_exists($fs, 'get_upgrade_url')) {
                    return (string) $fs->get_upgrade_url();
                }
            } catch (\Throwable $e) {
                // fall through
            }
        }

        return (string) admin_url('admin.php?page=tracksharp-audit-pricing');
    }

    /**
     * Check if Meta CAPI is available (Pro only).
     */
    public static function is_meta_available(): bool
    {
        return self::is_pro_active() && class_exists('TrackSharp_Meta');
    }

    /**
     * Check if Google Ads attribution is available (Pro only).
     */
    public static function is_google_ads_available(): bool
    {
        return self::is_pro_active();
    }

    /**
     * Check if Enhanced Conversions are available (Pro only).
     */
    public static function is_enhanced_conversions_available(): bool
    {
        return self::is_pro_active() && class_exists('TrackSharp_Hasher');
    }

    /**
     * Check if full dashboard is available (Pro only).
     */
    public static function is_full_dashboard_available(): bool
    {
        return self::is_pro_active();
    }

    /**
     * Render a Pro badge.
     */
    public static function render_pro_badge(bool $inline = true): string
    {
        $style = $inline ? 'display:inline-block;' : 'display:block;margin-top:8px;';
        return '<span class="ws-pro-badge" style="' . esc_attr($style) . 'background:#158a5f;color:#fff;font-size:11px;font-weight:600;padding:2px 8px;border-radius:3px;text-transform:uppercase;">Pro</span>';
    }

    /**
     * Render an upgrade prompt box.
     */
    public static function render_upgrade_prompt(string $title = '', string $message = ''): string
    {
        $upgrade_url = self::get_upgrade_url();

        if ($title === '') {
            $title = 'Unlock Google Ads & Meta CAPI';
        }
        if ($message === '') {
            $message = 'Upgrade to Pro for full Google Ads attribution (gclid), Meta Conversions API, Enhanced Conversions and advanced analytics.';
        }

        $html = '<div class="ws-upgrade-prompt" style="background:#f0fdf4;border:1px solid #bbf7d0;border-radius:12px;padding:24px;margin:16px 0;">';
        $html .= '<h3 style="margin:0 0 8px;color:#166534;font-size:16px;">' . esc_html($title) . '</h3>';
        $html .= '<p style="margin:0 0 16px;color:#15803d;font-size:14px;line-height:1.5;">' . esc_html($message) . '</p>';
        $html .= '<a class="button button-primary" href="' . esc_url($upgrade_url) . '" style="background:#158a5f;border-color:#127a54;">';
        $html .= 'View Pro Plans';
        $html .= '</a>';
        $html .= '</div>';

        return $html;
    }
}
