package aggregate

import (
	"math"

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

func AggSummaryBeginTime() Aggregator {
	return &SummaryBeginTime{math.MaxInt64}
}

type SummaryBeginTime struct {
	begin int64
}

var _ Aggregator = (*SummaryBeginTime)(nil)

func (a *SummaryBeginTime) Add(record *load.StmtRecord) {
	if record.Begin < a.begin {
		a.begin = record.Begin
	}
}

func (a *SummaryBeginTime) Merge(agg Aggregator) {
	if agg, ok := agg.(*SummaryBeginTime); ok {
		if agg.begin < a.begin {
			a.begin = agg.begin
		}
	}
}

func (a *SummaryBeginTime) Output(model *api.Model) {
	model.BeginTime = int(a.begin)
}
