// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package version

import (
	"fmt"
	"runtime"
)

var (
	// MimVerMajor is the major version of Mim
	MimVerMajor = 0
	// MimVerMinor is the minor version of Mim
	MimVerMinor = 0
	// MimVerPatch is the patch version of Mim
	MimVerPatch = 1
	// MimVerName is alternative name of the version
	MimVerName = "Mim"
	// GitHash is the current git commit hash
	GitHash = "Unknown"
	// GitRef is the current git reference name (branch or tag)
	GitRef = "Unknown"
)

// MimVersion is the semver of Mim
type MimVersion struct {
	major int
	minor int
	patch int
	name  string
}

// NewMimVersion creates a MimVersion object
func NewMimVersion() *MimVersion {
	return &MimVersion{
		major: MimVerMajor,
		minor: MimVerMinor,
		patch: MimVerPatch,
		name:  MimVerName,
	}
}

// Name returns the alternative name of MimVersion
func (v *MimVersion) Name() string {
	return v.name
}

// SemVer returns MimVersion in semver format
func (v *MimVersion) SemVer() string {
	return fmt.Sprintf("%d.%d.%d", v.major, v.minor, v.patch)
}

// String converts MimVersion to a string
func (v *MimVersion) String() string {
	return fmt.Sprintf("%s %s\n%s", v.SemVer(), v.name, NewMimBuildInfo())
}

// MimBuild is the info of building environment
type MimBuild struct {
	GitHash   string `json:"gitHash"`
	GitRef    string `json:"gitRef"`
	GoVersion string `json:"goVersion"`
}

// NewMimBuildInfo creates a TiUPBuild object
func NewMimBuildInfo() *MimBuild {
	return &MimBuild{
		GitHash:   GitHash,
		GitRef:    GitRef,
		GoVersion: runtime.Version(),
	}
}
