<?php

declare(strict_types=1);

namespace App\Framework\UserAgent\Patterns;

use App\Framework\UserAgent\Enums\EngineType;

/**
 * Browser engine detection patterns
 */
final readonly class EnginePatterns
{
    /**
     * Get engine detection patterns in priority order
     * @return array<array{pattern: string, engine: EngineType, versionPattern: string}>
     */
    public static function getPatterns(): array
    {
        return [
            // Blink (Chrome 28+, Opera 15+, Edge 79+)
            [
                'pattern' => '/Chrome\/\d+.*Safari\/\d+/',
                'engine' => EngineType::BLINK,
                'versionPattern' => '/Chrome\/([\d.]+)/', // Use Chrome version as proxy
            ],

            // WebKit (Safari, older Chrome)
            [
                'pattern' => '/AppleWebKit\/([\d.]+).*Version\/[\d.]+.*Safari/',
                'engine' => EngineType::WEBKIT,
                'versionPattern' => '/AppleWebKit\/([\d.]+)/',
            ],

            // Gecko (Firefox)
            [
                'pattern' => '/Gecko\/([\d]+).*Firefox/',
                'engine' => EngineType::GECKO,
                'versionPattern' => '/Gecko\/([\d]+)/',
            ],

            // Trident (Internet Explorer)
            [
                'pattern' => '/Trident\/([\d.]+)/',
                'engine' => EngineType::TRIDENT,
                'versionPattern' => '/Trident\/([\d.]+)/',
            ],

            // EdgeHTML (Legacy Edge 12-18)
            [
                'pattern' => '/Edge\/([\d.]+)/',
                'engine' => EngineType::EDGE_HTML,
                'versionPattern' => '/Edge\/([\d.]+)/',
            ],
        ];
    }

    /**
     * Get fallback engine detection based on browser
     * @return array<string, EngineType>
     */
    public static function getBrowserEngineMap(): array
    {
        return [
            'chrome' => EngineType::BLINK,
            'edge' => EngineType::BLINK,
            'opera' => EngineType::BLINK,
            'samsung_browser' => EngineType::BLINK,
            'brave' => EngineType::BLINK,
            'vivaldi' => EngineType::BLINK,
            'safari' => EngineType::WEBKIT,
            'firefox' => EngineType::GECKO,
            'internet_explorer' => EngineType::TRIDENT,
        ];
    }

    /**
     * Map Gecko build numbers to readable versions
     * @return array<string, string>
     */
    public static function getGeckoVersionMap(): array
    {
        return [
            '20100101' => '2.0',   // Firefox 4+
            '20090715' => '1.9.1', // Firefox 3.5
            '20081217' => '1.9.0', // Firefox 3.0
        ];
    }

    /**
     * Convert Gecko build number to version
     */
    public static function formatGeckoVersion(string $buildNumber): string
    {
        $map = self::getGeckoVersionMap();

        // Try exact match first
        if (isset($map[$buildNumber])) {
            return $map[$buildNumber];
        }

        // For modern Firefox, use a simplified approach
        if ((int) $buildNumber >= 20100101) {
            return '2.0+';
        }

        return $buildNumber;
    }

    /**
     * Get minimum engine versions for modern web features
     * @return array<string, array<string, string>>
     */
    public static function getFeatureSupport(): array
    {
        return [
            'css-grid' => [
                'blink' => '57.0',
                'webkit' => '10.1',
                'gecko' => '52.0',
            ],
            'es6' => [
                'blink' => '51.0',
                'webkit' => '10.0',
                'gecko' => '45.0',
            ],
            'service-worker' => [
                'blink' => '40.0',
                'webkit' => '11.1',
                'gecko' => '44.0',
            ],
        ];
    }
}
