package utils

import (
	"context"
	"fmt"
	"time"

	"github.com/tidbcloud/tiinf/monitor/tilog"
	"go.uber.org/zap"
)

type BenchmarkInfo struct {
	Name   string
	Fields []zap.Field
	Start  time.Time
	Finish time.Time
}

func NewBenchmarkInfo(name string, fields ...zap.Field) *BenchmarkInfo {
	b := &BenchmarkInfo{
		Name:   name,
		Fields: fields,
	}

	return b
}

func (b *BenchmarkInfo) Begin(ctx context.Context) *BenchmarkInfo {
	b.Start = time.Now()
	tilog.Info(ctx, b.Name+" start", b.Fields...)
	return b
}

func (b *BenchmarkInfo) Event(event string) string {
	return fmt.Sprintf("[%s] on(%s)", b.Name, event)
}

func (b *BenchmarkInfo) OnExit(ctx context.Context) {
	b.Finish = time.Now()
	duration := b.Finish.Sub(b.Start)
	tilog.Info(ctx, b.Name+" end", zap.Duration("cost", duration))
}

func Begin(ctx context.Context, name string, fields ...zap.Field) *BenchmarkInfo {
	bm := &BenchmarkInfo{
		Name:   name,
		Fields: fields,
	}
	bm.Start = time.Now()
	tilog.Info(ctx, bm.Name+" begin ", fields...)
	return bm

}
