package searchsvc

import (
	"context"
	"errors"
	"fmt"

	objectV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
	objectSvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object"
	searchsvc "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/search"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/object/util"
	cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
	objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
)

func (s *Service) toPrm(req *objectV2.SearchRequest, stream objectSvc.SearchStream) (*searchsvc.Prm, error) {
	body := req.GetBody()

	cnrV2 := body.GetContainerID()
	if cnrV2 == nil {
		return nil, errors.New("missing container ID")
	}

	var id cid.ID

	err := id.ReadFromV2(*cnrV2)
	if err != nil {
		return nil, fmt.Errorf("invalid container ID: %w", err)
	}

	commonPrm, err := util.CommonPrmFromV2(req)
	if err != nil {
		return nil, err
	}

	p := new(searchsvc.Prm)
	p.SetCommonParameters(commonPrm)

	p.SetWriter(&streamWriter{
		stream: stream,
	})

	if !commonPrm.LocalOnly() {
		key, err := s.keyStorage.GetKey(nil)
		if err != nil {
			return nil, err
		}

		forwarder := &requestForwarder{
			Request: req,
			Key:     key,
		}

		p.SetRequestForwarder(groupAddressRequestForwarder(forwarder.forwardRequest))
	}

	p.WithContainerID(id)
	p.WithSearchFilters(objectSDK.NewSearchFiltersFromV2(body.GetFilters()))

	return p, nil
}

func groupAddressRequestForwarder(f func(context.Context, network.Address, client.MultiAddressClient, []byte) ([]oid.ID, error)) searchsvc.RequestForwarder {
	return func(ctx context.Context, info client.NodeInfo, c client.MultiAddressClient) ([]oid.ID, error) {
		var (
			firstErr error
			res      []oid.ID

			key = info.PublicKey()
		)

		info.AddressGroup().IterateAddresses(func(addr network.Address) (stop bool) {
			var err error

			defer func() {
				stop = err == nil

				if stop || firstErr == nil {
					firstErr = err
				}

				// would be nice to log otherwise
			}()

			res, err = f(ctx, addr, c, key)

			return
		})

		return res, firstErr
	}
}