<?php

namespace App\Service\Utils;

class SimplifyOgLiveFilenameService
{
    private const string PATTERN = '/^ogLive-([^-]+)-(.+)-([^-]+)-r([0-9]+)(?:\.([a-f0-9]+))?_([0-9]+)(?:\.iso)?$/';

    public function __invoke(string $filename): ?string
    {
        if (!preg_match(self::PATTERN, $filename, $matches)) {
            return null;
        }

        $distro = $matches[1];
        $kernelFull = $matches[2];
        $arch = $matches[3];
        $revision = $matches[4];
        $commit = $matches[5] ?? null;
        $date = $matches[6];


        $kernel = in_array($arch, ['amd64', 'i386']) ? $kernelFull : "$kernelFull-$arch";
        $arch = in_array($arch, ['amd64', 'i386']) ? $arch : 'i386';

        return 'ogLive-'.$kernelFull.'-'.$date;
    }
}