package operator

import (
	"context"

	"helmet/pkg/mlbctl"
)

func (oper *Operator) ListForwarders(ctx context.Context, params *mlbctl.ListForwardersParams) (*mlbctl.ListForwardersResult, error) {
	var err error

	res := &mlbctl.ListForwardersResult{
		Forwarders: make([]*mlbctl.Forwarder, 0),
	}
	for _, forw := range oper.proxy.Forwarders {
		oForw := &mlbctl.Forwarder{
			Lport:        forw.Lport,
			Dport:        forw.Dport,
			Destinations: make([]*mlbctl.Destination, 0),
		}
		for _, dest := range forw.Dests {
			oDest := &mlbctl.Destination{
				Address: dest.Address,
			}
			oForw.Destinations = append(oForw.Destinations, oDest)
		}
		res.Forwarders = append(res.Forwarders, oForw)
	}
	return res, err
}

func (oper *Operator) CreateForwarder(ctx context.Context, params *mlbctl.CreateForwarderParams) (*mlbctl.CreateForwarderResult, error) {
	var err error
	res := &mlbctl.CreateForwarderResult{}
	err = oper.proxy.AddForwarder(ctx, params.Lport, params.Dport, params.Destinations...)
	return res, err
}

func (oper *Operator) DeleteForwarder(ctx context.Context, params *mlbctl.DeleteForwarderParams) (*mlbctl.DeleteForwarderResult, error) {
	var err error
	res := &mlbctl.DeleteForwarderResult{}
	err = oper.proxy.DeleteForwarder(ctx, params.Lport)
	return res, err
}
