<?php

namespace App\Support;

use WhichBrowser\Parser;

class DeviceInfo {
    public static function parse(?string $userAgent): array {
        if (!$userAgent) {
            return self::unknown();
        }

        $ua = new Parser($userAgent);

        $browser = $ua->browser->name ?? 'Unknown';
        $os = $ua->os->name ?? 'Unknown';

        return [
            'browser' => $browser,
            'browser_icon' => self::browserIcon($browser),
            'os' => $os,
            'os_icon' => self::osIcon($os),
        ];
    }

    private static function unknown(): array {
        return [
            'browser' => 'Unknown',
            'browser_icon' => 'mdi-web',
            'os' => 'Unknown',
            'os_icon' => 'mdi-laptop',
        ];
    }

    private static function browserIcon(string $browser): string {
        return match (true) {
            str_contains($browser, 'Chrome') => 'mdi-google-chrome',
            str_contains($browser, 'Safari') => 'mdi-apple-safari',
            str_contains($browser, 'Firefox') => 'mdi-firefox',
            str_contains($browser, 'Edge') => 'mdi-microsoft-edge',
            default => 'mdi-web',
        };
    }

    private static function osIcon(string $os): string {
        return match (true) {
            str_contains($os, 'Windows') => 'mdi-microsoft-windows',
            str_contains($os, 'Mac') => 'mdi-apple',
            str_contains($os, 'iOS') => 'mdi-apple-ios',
            str_contains($os, 'Android') => 'mdi-android',
            str_contains($os, 'Linux') => 'mdi-linux',
            default => 'mdi-laptop',
        };
    }
}
