package vpc

import (
	"context"

	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

type VPCReaderWriter interface {
	VPCReader
	VPCWriter
}

type VPCWriter interface {
	Create(ctx context.Context, vpc *v1alpha1.VPC) error
	Update(ctx context.Context, vpc *v1alpha1.VPC) error
	Delete(ctx context.Context, vpc *v1alpha1.VPC) error
	UpdateStatus(ctx context.Context, vpc *v1alpha1.VPC) error
}

type VPCReader interface {
	Get(ctx context.Context, key client.ObjectKey, vpc *v1alpha1.VPC) error
	List(ctx context.Context, list *v1alpha1.VPCList, opts ...client.ListOption) error
}

func NewVPCReaderWriter(cli client.Client) VPCReaderWriter {
	return &vpcReaderWriter{
		vpcReader: vpcReader{cli},
		vpcWriter: vpcWriter{cli},
	}
}

func NewVPCReader(cli client.Client) VPCReader {
	return &vpcReader{
		readCli: cli,
	}
}

func NewVPCWriter(cli client.Client) VPCWriter {
	return &vpcWriter{
		cli: cli,
	}
}

type vpcReaderWriter struct {
	vpcReader
	vpcWriter
}

type vpcWriter struct {
	cli client.Client
}

func (w *vpcWriter) Create(ctx context.Context, vpc *v1alpha1.VPC) error {
	return w.cli.Create(ctx, vpc)
}

func (w *vpcWriter) Update(ctx context.Context, vpc *v1alpha1.VPC) error {
	return w.cli.Update(ctx, vpc)
}

func (w *vpcWriter) Delete(ctx context.Context, vpc *v1alpha1.VPC) error {
	return w.cli.Delete(ctx, vpc)
}

func (w *vpcWriter) UpdateStatus(ctx context.Context, vpc *v1alpha1.VPC) error {
	return w.cli.Status().Update(ctx, vpc)
}

type vpcReader struct {
	readCli client.Reader
}

func (r *vpcReader) Get(ctx context.Context, key client.ObjectKey, vpc *v1alpha1.VPC) error {
	return r.readCli.Get(ctx, key, vpc)
}

func (r *vpcReader) List(ctx context.Context, list *v1alpha1.VPCList, opts ...client.ListOption) error {
	return r.readCli.List(ctx, list, opts...)
}
