package aggregate

import (
	"math"

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

func AggMaxCopWaitTime() Aggregator {
	return &MaxCopWaitTime{math.MinInt64}
}

type MaxCopWaitTime struct {
	max int64
}

var _ Aggregator = (*MaxCopWaitTime)(nil)

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

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

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