<?php

declare(strict_types=1);

namespace App\Framework\UserAgent\Patterns;

use App\Framework\UserAgent\Enums\PlatformType;

/**
 * Platform/OS detection patterns
 */
final readonly class PlatformPatterns
{
    /**
     * Get platform detection patterns in priority order
     * @return array<array{pattern: string, platform: PlatformType, versionPattern: string}>
     */
    public static function getPatterns(): array
    {
        return [
            // iOS (check before macOS as iOS UA contains Mac OS X)
            [
                'pattern' => '/iPhone OS ([\d_]+)/',
                'platform' => PlatformType::IOS,
                'versionPattern' => '/iPhone OS ([\d_]+)/',
            ],
            [
                'pattern' => '/OS ([\d_]+) like Mac OS X/',
                'platform' => PlatformType::IOS,
                'versionPattern' => '/OS ([\d_]+) like Mac OS X/',
            ],

            // Android
            [
                'pattern' => '/Android ([\d.]+)/',
                'platform' => PlatformType::ANDROID,
                'versionPattern' => '/Android ([\d.]+)/',
            ],

            // Windows
            [
                'pattern' => '/Windows NT ([\d.]+)/',
                'platform' => PlatformType::WINDOWS,
                'versionPattern' => '/Windows NT ([\d.]+)/',
            ],

            // macOS (after iOS check)
            [
                'pattern' => '/Mac OS X ([\d_]+)/',
                'platform' => PlatformType::MACOS,
                'versionPattern' => '/Mac OS X ([\d_]+)/',
            ],
            [
                'pattern' => '/Macintosh.*OS X ([\d_]+)/',
                'platform' => PlatformType::MACOS,
                'versionPattern' => '/OS X ([\d_]+)/',
            ],

            // Chrome OS
            [
                'pattern' => '/CrOS \w+ ([\d.]+)/',
                'platform' => PlatformType::CHROME_OS,
                'versionPattern' => '/CrOS \w+ ([\d.]+)/',
            ],

            // Linux (generic, should be last)
            [
                'pattern' => '/Linux/',
                'platform' => PlatformType::LINUX,
                'versionPattern' => '//', // No version pattern for generic Linux
            ],
        ];
    }

    /**
     * Map Windows NT versions to readable names
     * @return array<string, string>
     */
    public static function getWindowsVersionMap(): array
    {
        return [
            '10.0' => '10/11',
            '6.3' => '8.1',
            '6.2' => '8',
            '6.1' => '7',
            '6.0' => 'Vista',
            '5.2' => 'XP 64-bit',
            '5.1' => 'XP',
            '5.0' => '2000',
        ];
    }

    /**
     * Convert Windows NT version to readable format
     */
    public static function formatWindowsVersion(string $version): string
    {
        $map = self::getWindowsVersionMap();

        return $map[$version] ?? $version;
    }

    /**
     * Convert iOS/macOS version from underscore to dot notation
     */
    public static function formatAppleVersion(string $version): string
    {
        return str_replace('_', '.', $version);
    }

    /**
     * Patterns that indicate mobile devices
     * @return string[]
     */
    public static function getMobilePatterns(): array
    {
        return [
            '/Mobile/',
            '/Android/',
            '/iPhone/',
            '/iPad/',
            '/iPod/',
            '/BlackBerry/',
            '/Windows Phone/',
            '/Opera Mini/',
            '/webOS/',
            '/Kindle/',
            '/Silk/',
            '/Mobile Safari/',
            '/SamsungBrowser/',
        ];
    }

    /**
     * Check if User-Agent indicates a mobile device
     */
    public static function isMobile(string $userAgent): bool
    {
        foreach (self::getMobilePatterns() as $pattern) {
            if (preg_match($pattern, $userAgent)) {
                return true;
            }
        }

        return false;
    }
}
