package clientv2

import (
	"context"

	scalerpb "github.com/tidbcloud/aws-shared-provider/pkg/tidbworker/proto/scaler"
)

type gcV2Client interface {
	// RegisterGCV2 notifies the manager that a round of GC at safePoint has just been completed.
	// If no more heartbeats are received, a new round of GC should be scheduled after gcLifeTime.
	RegisterGCV2(ctx context.Context, safePoint uint64, gcLifeTime int64) error
	// RecycleGCV2 notifies the manager that all GC tasks at or before safePoint have been completed.
	RecycleGCV2(ctx context.Context, safePoint uint64) error
	// UpdateGCLifeTime notifies the manager that the user's GCLifeTime setting has been updated.
	UpdateGCLifeTime(ctx context.Context, gcLifeTime int64) error
}

var _ gcV2Client = &client{}

func (c *client) RegisterGCV2(ctx context.Context, safePoint uint64, gcLifeTime int64) error {
	req := &scalerpb.RegisterGCV2Request{
		Header:     requestHeader(c),
		SafePoint:  safePoint,
		GcLifeTime: gcLifeTime,
	}
	resp, err := c.grpcClient.RegisterGCV2(ctx, req)
	return getError("RegisterGCV2", err, resp)
}

func (c *client) RecycleGCV2(ctx context.Context, safePoint uint64) error {
	req := &scalerpb.RecycleGCV2Request{
		Header:    requestHeader(c),
		SafePoint: safePoint,
	}
	resp, err := c.grpcClient.RecycleGCV2(ctx, req)
	return getError("RecycleGCV2", err, resp)
}

func (c *client) UpdateGCLifeTime(ctx context.Context, gcLifeTime int64) error {
	req := &scalerpb.UpdateGCLifeTimeRequest{
		Header:     requestHeader(c),
		GcLifeTime: gcLifeTime,
	}
	resp, err := c.grpcClient.UpdateGCLifeTime(ctx, req)
	return getError("UpdateGCLifeTime", err, resp)
}
