package aggregate

import (
	"math"

	"github.com/pingcap-inc/clinic/pkg/ngm/statements/api"
	"github.com/pingcap-inc/clinic/pkg/ngm/statements/load"
)

func AggMaxGetCommitTsTime() Aggregator {
	return &MaxGetCommitTsTime{math.MinInt64}
}

type MaxGetCommitTsTime struct {
	max int64
}

var _ Aggregator = (*MaxGetCommitTsTime)(nil)

func (a *MaxGetCommitTsTime) Add(record *load.StmtRecord) {
	if record.MaxGetCommitTsTime.Nanoseconds() > a.max {
		a.max = record.MaxGetCommitTsTime.Nanoseconds()
	}
}

func (a *MaxGetCommitTsTime) Merge(agg Aggregator) {
	if agg, ok := agg.(*MaxGetCommitTsTime); ok {
		if agg.max > a.max {
			a.max = agg.max
		}
	}
}

func (a *MaxGetCommitTsTime) Output(model *api.Model) {
	model.MaxGetCommitTsTime = int(a.max)
}
