package aggregate

import (
	"math"

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

func AggMaxCommitTime() Aggregator {
	return &MaxCommitTime{math.MinInt64}
}

type MaxCommitTime struct {
	max int64
}

var _ Aggregator = (*MaxCommitTime)(nil)

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

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

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