<?php
/**
 * TrackSharp Consent
 * 
 * Handles consent checking via WP Consent API with fallback logic.
 *
 * @package TrackSharp
 */

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

class TrackSharp_Consent
{
    /**
     * Check if a consent category is granted.
     *
     * @param string $category Consent category (e.g., 'marketing', 'statistics', 'functional').
     * @return bool True if consent is granted.
     */
    public function wp_consent_has($category)
    {
        if (function_exists('wp_has_consent')) {
            return (bool) wp_has_consent($category);
        }

        // No WP Consent API installed: use fallback
        $fallback = get_option('tracksharp_consent_fallback', 'grant_immediately');
        if ($fallback === 'grant_immediately') {
            return true;
        }

        return false;
    }

    /**
     * Check if server-side consent is granted for a specific channel.
     *
     * @param string $channel Channel name ('meta' or 'ga4').
     * @return bool True if consent is granted for the channel.
     */
    public function server_consent_granted_for($channel)
    {
        // Allow integrators to remap categories if their CMP uses different names.
        $marketing_cat = apply_filters('tracksharp_consent_category_marketing', 'marketing');
        $stats_cat = apply_filters('tracksharp_consent_category_statistics', 'statistics');

        if ($channel === 'meta') {
            return $this->wp_consent_has($marketing_cat);
        }

        if ($channel === 'ga4') {
            $needs_marketing = apply_filters('tracksharp_ga4_requires_marketing', false);
            if ($needs_marketing) {
                return $this->wp_consent_has($marketing_cat);
            }

            // Also accept "statistics-anonymous"
            $stats_anon_cat = apply_filters('tracksharp_consent_category_statistics_anonymous', 'statistics-anonymous');
            return $this->wp_consent_has($stats_cat) || $this->wp_consent_has($stats_anon_cat);
        }

        // Default conservative
        return false;
    }

    /**
     * Filter callback to allow statistics-anonymous consent for GA4.
     *
     * @param bool  $granted  Current consent state.
     * @param array $payload  Request payload.
     * @return bool Updated consent state.
     */
    public function allow_stats_anonymous_for_ga4($granted, $payload)
    {
        if (!$granted && function_exists('wp_has_consent')) {
            if (wp_has_consent('statistics-anonymous') || wp_has_consent('statistics')) {
                return true;
            }
        }
        return $granted;
    }

    /**
     * Check if consent is granted based on request payload.
     *
     * @param array $payload Request payload with consent data.
     * @return bool True if consent is granted.
     */
    public function is_consent_granted($payload)
    {
        $c = isset($payload['consent']) ? $payload['consent'] : [];
        $granted = (
            isset($c['ad_storage'], $c['ad_user_data'], $c['ad_personalization']) &&
            $c['ad_storage'] === 'granted' &&
            $c['ad_user_data'] === 'granted' &&
            $c['ad_personalization'] === 'granted'
        );
        return apply_filters('tracksharp_tracker_is_consent_granted', $granted, $payload);
    }
}
