namespace PSLauncher.Core.Installations;

public readonly record struct SemVer(int Major, int Minor, int Patch) : IComparable<SemVer>
{
    public static SemVer Parse(string s)
    {
        var parts = s.Split('.');
        if (parts.Length != 3
            || !int.TryParse(parts[0], out var maj)
            || !int.TryParse(parts[1], out var min)
            || !int.TryParse(parts[2], out var pat))
        {
            return new SemVer(0, 0, 0);
        }
        return new SemVer(maj, min, pat);
    }

    public int CompareTo(SemVer other)
    {
        var c = Major.CompareTo(other.Major);
        if (c != 0) return c;
        c = Minor.CompareTo(other.Minor);
        if (c != 0) return c;
        return Patch.CompareTo(other.Patch);
    }

    public override string ToString() => $"{Major}.{Minor}.{Patch}";
}
