// Copyright 2025 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package version

import (
	"fmt"
	"regexp"
)

var (
	// Following vars are injected through the build flags (see Makefile)
	GitCommit string
	GitBranch string
	GitTag    string
)

// see https://calver.org
const (
	Major                    = 3             // Major version component of the current release
	Minor                    = 5             // Minor version component of the current release
	Micro                    = 2             // Patch version component of the current release
	Modifier                 = ""            // Modifier component of the current release
	DefaultSnapshotGitBranch = "release/3.4" // Branch of erigontech/erigon-snapshot to use in OtterSync.
	VersionKeyCreated        = "ErigonVersionCreated"
	VersionKeyFinished       = "ErigonVersionFinished"
	ClientName               = "erigon"
	ClientCode               = "EG"
)

// VersionNoMeta holds the textual version string excluding the metadata.
var VersionNoMeta = func() string {
	return fmt.Sprintf("%d.%d.%d", Major, Minor, Micro)
}()

// VersionWithMeta holds the textual version string including the metadata.
var VersionWithMeta = func() string {
	v := VersionNoMeta
	if Modifier != "" {
		v += "-" + Modifier
	}
	return v
}()

func VersionWithCommit(gitCommit string) string {
	vsn := VersionWithMeta
	if len(gitCommit) >= 8 {
		vsn += "-" + gitCommit[:8]
	}
	return vsn
}

// advertisedVersionPattern keeps shell-metacharacter and control content out of
// the advertised version: GitTag is untrusted build-time metadata (and may be
// passed via env for builds without .git), so it is honored only when version-shaped.
var advertisedVersionPattern = regexp.MustCompile(`^v?[0-9][0-9A-Za-z.+-]*$`)

// NodeVersion is the human-facing version advertised to peers and over APIs.
// A build from a release tag reports that tag (e.g. v3.5.0); any other build
// reports the in-development version plus the short commit for provenance
// (e.g. 3.5.0-dev-a53e9545).
func NodeVersion() string {
	if advertisedVersionPattern.MatchString(GitTag) {
		return GitTag
	}
	return VersionWithCommit(GitCommit)
}
