<?php

declare(strict_types=1);

namespace App\Tests\Service;

use App\Service\UserAgentParser;
use PHPUnit\Framework\TestCase;

/**
 * @internal
 */
final class UserAgentParserTest extends TestCase
{
    private UserAgentParser $parser;

    protected function setUp(): void
    {
        $this->parser = new UserAgentParser();
    }

    public function testNullAndEmptyReturnNull(): void
    {
        self::assertNull($this->parser->parse(null));
        self::assertNull($this->parser->parse(''));
        self::assertNull($this->parser->parse('   '));
    }

    public function testChromeOnWindows(): void
    {
        $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
        self::assertSame('Ordinateur · Windows · Chrome', $this->parser->parse($ua));
    }

    public function testEdgeBeatsChrome(): void
    {
        $ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0';
        self::assertSame('Ordinateur · Windows · Edge', $this->parser->parse($ua));
    }

    public function testSafariOnIphoneIsMobileIos(): void
    {
        $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
        self::assertSame('Mobile · iOS · Safari', $this->parser->parse($ua));
    }

    public function testChromeOnAndroid(): void
    {
        $ua = 'Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36';
        self::assertSame('Mobile · Android · Chrome', $this->parser->parse($ua));
    }

    public function testFirefoxOnLinux(): void
    {
        $ua = 'Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0';
        self::assertSame('Ordinateur · Linux · Firefox', $this->parser->parse($ua));
    }

    public function testSafariOnMac(): void
    {
        $ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15';
        self::assertSame('Ordinateur · macOS · Safari', $this->parser->parse($ua));
    }

    public function testIpadIsTablet(): void
    {
        $ua = 'Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1';
        self::assertSame('Tablette · iOS · Safari', $this->parser->parse($ua));
    }

    public function testUnknownUaFallsBack(): void
    {
        self::assertSame('Ordinateur · Autre · Autre', $this->parser->parse('SomeRandomBot/1.0'));
    }
}
