<?php
declare(strict_types=1);

namespace ProRank\SEO\Modules\Performance;

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

/**
 * Free-package compression helper.
 *
 * The free build only uses local compression capabilities. Premium cloud
 * compression and edge push are not part of this package.
 */
class ProRankCompressionService {
    public static function compress_via_cloud(string $content, string $license_key, array $options = []): array {
        if (function_exists('brotli_compress')) {
            $compressed = brotli_compress($content, 11);
            if ($compressed !== false) {
                return [
                    'success' => true,
                    'data' => $compressed,
                    'encoding' => 'br',
                    'method' => 'local',
                    'size' => strlen($compressed),
                ];
            }
        }

        $gzipped = gzencode($content, 9);
        if ($gzipped !== false) {
            return [
                'success' => true,
                'data' => $gzipped,
                'encoding' => 'gzip',
                'method' => 'local_fallback',
                'size' => strlen($gzipped),
            ];
        }

        return [
            'success' => true,
            'data' => $content,
            'encoding' => 'identity',
            'method' => 'uncompressed',
            'size' => strlen($content),
        ];
    }

    public static function push_to_edge(string $url, string $content, string $license_key, array $options = []): array {
        return [
            'success' => false,
            'error' => __('Edge push is not available in the free plugin.', 'prorank-seo'),
        ];
    }

    public static function get_cloud_stats(string $license_key): array {
        return [
            'compressions_today' => 0,
            'bandwidth_saved' => 0,
            'edge_hits' => 0,
            'global_locations' => 0,
        ];
    }

    public static function purge_edge_cache(string $license_key, array $urls = []): bool {
        return false;
    }

    public static function test_service(string $license_key): array {
        return [
            'success' => false,
            'error' => __('Cloud compression is not available in the free plugin.', 'prorank-seo'),
        ];
    }

    public static function get_capabilities(string $license_key): array {
        return [
            'available' => false,
            'brotli' => function_exists('brotli_compress'),
            'gzip' => function_exists('gzencode'),
        ];
    }
}
