<?php

declare(strict_types=1);


/**
 * Cache & CDN Settings Configuration
 * 
 * REAL setup for Hetzner + CDN architecture
 * 
 * @package ProRank\SEO\Admin\Settings
 */

namespace ProRank\SEO\Admin\Settings;

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


/**
 * Get cache and CDN settings fields
 */


function get_cache_cdn_settings() {
    return [
        'cache_general' => [
            'title' => __('Local Cache Settings', 'prorank-seo'),
            'description' => __('Configure caching on your web server', 'prorank-seo'),
            'fields' => [
                'cache_enabled' => [
                    'label' => __('Enable Local Cache', 'prorank-seo'),
                    'type' => 'toggle',
                    'default' => true,
                    'description' => __('Cache pages on your web server', 'prorank-seo'),
                ],
                'cache_lifetime' => [
                    'label' => __('Local Cache Lifetime', 'prorank-seo'),
                    'type' => 'number',
                    'default' => 3600,
                    'min' => 60,
                    'max' => 86400,
                    'description' => __('How long to keep pages in local cache (seconds)', 'prorank-seo'),
                ],
                'cache_compression' => [
                    'label' => __('Compression Method', 'prorank-seo'),
                    'type' => 'select',
                    'options' => [
                        'gzip' => 'Gzip (Compatible)',
                        'brotli' => 'Brotli (Better compression)',
                        'both' => 'Both (Recommended)',
                    ],
                    'default' => 'both',
                    'description' => __('Compression for cached files', 'prorank-seo'),
                ],
            ],
        ],
        
        'cdn_settings' => [
            'title' => __('CDN Configuration', 'prorank-seo'),
            'description' => __('CDN caches your content globally for multi-region delivery', 'prorank-seo'),
            'fields' => [
                'cdn_provider' => [
                    'label' => __('CDN Provider', 'prorank-seo'),
                    'type' => 'select',
                    'options' => [
                        'none' => 'No CDN (Origin server only)',
                        'cloudflare' => 'Cloudflare (Free/Paid)',
                        'bunnycdn' => 'BunnyCDN (Cheap)',
                        'keycdn' => 'KeyCDN',
                        'fastly' => 'Fastly (Enterprise)',
                    ],
                    'default' => 'none',
                    'description' => __('Your CDN handles global distribution', 'prorank-seo'),
                ],
                
                // Cloudflare Settings
                'cloudflare_zone_id' => [
                    'label' => __('Cloudflare Zone ID', 'prorank-seo'),
                    'type' => 'text',
                    'dependency' => ['cdn_provider' => 'cloudflare'],
                    'description' => __('Found in Cloudflare dashboard', 'prorank-seo'),
                ],
                'cloudflare_api_token' => [
                    'label' => __('Cloudflare API Token', 'prorank-seo'),
                    'type' => 'password',
                    'dependency' => ['cdn_provider' => 'cloudflare'],
                    'description' => __('Create token with Zone:Cache Purge permission', 'prorank-seo'),
                ],
                'cloudflare_cache_everything' => [
                    'label' => __('Cache Everything Page Rule', 'prorank-seo'),
                    'type' => 'toggle',
                    'dependency' => ['cdn_provider' => 'cloudflare'],
                    'default' => true,
                    'description' => __('Must create "Cache Everything" page rule in Cloudflare', 'prorank-seo'),
                ],
                
                // BunnyCDN Settings
                'bunny_pullzone_id' => [
                    'label' => __('BunnyCDN Pull Zone ID', 'prorank-seo'),
                    'type' => 'text',
                    'dependency' => ['cdn_provider' => 'bunnycdn'],
                    'description' => __('Your pull zone ID from BunnyCDN panel', 'prorank-seo'),
                ],
                'bunny_api_key' => [
                    'label' => __('BunnyCDN API Key', 'prorank-seo'),
                    'type' => 'password',
                    'dependency' => ['cdn_provider' => 'bunnycdn'],
                    'description' => __('Account API key from BunnyCDN', 'prorank-seo'),
                ],
                'bunny_hostname' => [
                    'label' => __('BunnyCDN Hostname', 'prorank-seo'),
                    'type' => 'text',
                    'dependency' => ['cdn_provider' => 'bunnycdn'],
                    'placeholder' => 'yoursite.b-cdn.net',
                    'description' => __('Your BunnyCDN hostname', 'prorank-seo'),
                ],
                
                // Common CDN Settings
                'cdn_ttl' => [
                    'label' => __('CDN Cache TTL', 'prorank-seo'),
                    'type' => 'select',
                    'options' => [
                        '3600' => '1 Hour',
                        '14400' => '4 Hours',
                        '86400' => '1 Day',
                        '604800' => '1 Week',
                        '2592000' => '30 Days (Recommended)',
                    ],
                    'default' => '86400',
                    'description' => __('How long CDN keeps your content cached', 'prorank-seo'),
                ],
                'browser_cache_ttl' => [
                    'label' => __('Browser Cache TTL', 'prorank-seo'),
                    'type' => 'select',
                    'options' => [
                        '3600' => '1 Hour',
                        '14400' => '4 Hours (Recommended)',
                        '86400' => '1 Day',
                    ],
                    'default' => '14400',
                    'description' => __('How long browsers cache content', 'prorank-seo'),
                ],
            ],
        ],
        
        'reality_check' => [
            'title' => __('Architecture Reality Check', 'prorank-seo'),
            'description' => __('Understanding your actual setup', 'prorank-seo'),
            'fields' => [
                'server_location' => [
                    'label' => __('Origin Server Location', 'prorank-seo'),
                    'type' => 'select',
                    'options' => [
                        'aws' => 'Amazon Web Services',
                        'gcp' => 'Google Cloud Platform',
                        'azure' => 'Microsoft Azure',
                        'digitalocean' => 'DigitalOcean',
                        'linode' => 'Linode',
                        'vultr' => 'Vultr',
                        'hetzner' => 'Hetzner',
                        'other' => 'Other Provider',
                    ],
                    'default' => 'other',
                    'description' => __('Your actual server is in ONE location only', 'prorank-seo'),
                ],
                'cdn_edge_locations' => [
                    'label' => __('CDN Edge Locations', 'prorank-seo'),
                    'type' => 'info',
                    'value' => 'Depends on CDN provider (0-275 locations)',
                    'description' => __('These are CDN servers, NOT your servers', 'prorank-seo'),
                ],
                'actual_architecture' => [
                    'label' => __('Your Real Architecture', 'prorank-seo'),
                    'type' => 'info',
                    'value' => '1 Origin Server → CDN → Global Edge Locations',
                    'description' => __('Single server with CDN for global distribution', 'prorank-seo'),
                ],
            ],
        ],
        
        'performance_expectations' => [
            'title' => __('Realistic Performance Expectations', 'prorank-seo'),
            'description' => __('What you can actually expect', 'prorank-seo'),
            'fields' => [
                'first_byte_time' => [
                    'label' => __('Time to First Byte (TTFB)', 'prorank-seo'),
                    'type' => 'info',
                    'value' => 'Origin: 50-200ms | CDN Hit: 10-50ms',
                ],
                'cache_hit_rate' => [
                    'label' => __('Typical Cache Hit Rate', 'prorank-seo'),
                    'type' => 'info',
                    'value' => '70-90% with proper configuration',
                ],
                'bandwidth_savings' => [
                    'label' => __('Bandwidth Savings', 'prorank-seo'),
                    'type' => 'info',
                    'value' => '60-80% served from CDN instead of origin',
                ],
                'global_latency' => [
                    'label' => __('Global Latency', 'prorank-seo'),
                    'type' => 'info',
                    'value' => 'Without CDN: 50-350ms | With CDN: 10-50ms',
                ],
            ],
        ],
        
        'monitoring' => [
            'title' => __('Real Monitoring', 'prorank-seo'),
            'description' => __('Track actual performance', 'prorank-seo'),
            'fields' => [
                'enable_real_tracking' => [
                    'label' => __('Enable Real Stats Tracking', 'prorank-seo'),
                    'type' => 'toggle',
                    'default' => true,
                    'description' => __('Track actual cache hits/misses (no fake data)', 'prorank-seo'),
                ],
                'redis_host' => [
                    'label' => __('Redis Host', 'prorank-seo'),
                    'type' => 'text',
                    'default' => '127.0.0.1',
                    'description' => __('Redis for caching stats', 'prorank-seo'),
                ],
                'redis_port' => [
                    'label' => __('Redis Port', 'prorank-seo'),
                    'type' => 'number',
                    'default' => 6379,
                ],
            ],
        ],
    ];
}

/**
 * Get CDN status and recommendations
 */
function get_cdn_status() {
    $cdn_provider = get_option('prorank_cdn_provider', 'none');
    
    if ($cdn_provider === 'none') {
        return [
            'status' => 'warning',
            'message' => __('No CDN configured - serving from origin server only', 'prorank-seo'),
            'recommendation' => __('Add Cloudflare (free) for global distribution', 'prorank-seo'),
            'locations' => 1,
            'actual_setup' => __('Single origin server', 'prorank-seo'),
        ];
    }
    
    $cdn_configs = [
        'cloudflare' => [
            'locations' => 275,
            'actual' => __('Origin → Cloudflare → 275 edge locations', 'prorank-seo'),
        ],
        'bunnycdn' => [
            'locations' => 123,
            'actual' => __('Origin → BunnyCDN → 123 edge locations', 'prorank-seo'),
        ],
        'keycdn' => [
            'locations' => 34,
            'actual' => __('Origin → KeyCDN → 34 edge locations', 'prorank-seo'),
        ],
        'fastly' => [
            'locations' => 145,
            'actual' => __('Origin → Fastly → 145 edge locations', 'prorank-seo'),
        ],
    ];
    
    $config = $cdn_configs[$cdn_provider] ?? ['locations' => 0, 'actual' => 'Unknown'];
    
    return [
        'status' => 'success',
/* translators: %1$: value */
        'message' => sprintf(__('CDN configured: %1$s', 'prorank-seo'), ucfirst($cdn_provider)),
        'locations' => $config['locations'],
        'actual_setup' => $config['actual'],
        'is_multi_region' => true, // True because CDN provides multi-region
        'origin_server' => __('Single origin server', 'prorank-seo'),
    ];
}
