package getsvc

import (
	"context"
	"errors"

	"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
	"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
	apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
	objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
	"go.uber.org/zap"
)

func (r *request) executeLocal(ctx context.Context) {
	ctx, span := tracing.StartSpanFromContext(ctx, "getService.executeLocal")
	defer func() {
		span.End()
	}()

	var err error

	r.collectedObject, err = r.get(ctx)

	var errSplitInfo *objectSDK.SplitInfoError
	var errRemoved *apistatus.ObjectAlreadyRemoved
	var errOutOfRange *apistatus.ObjectOutOfRange

	switch {
	default:
		r.status = statusUndefined
		r.err = err

		r.log.Debug(logs.GetLocalGetFailed, zap.Error(err))
	case err == nil:
		r.status = statusOK
		r.err = nil
		r.writeCollectedObject(ctx)
	case errors.As(err, &errRemoved):
		r.status = statusINHUMED
		r.err = errRemoved
	case errors.As(err, &errSplitInfo):
		r.status = statusVIRTUAL
		mergeSplitInfo(r.splitInfo(), errSplitInfo.SplitInfo())
		r.err = objectSDK.NewSplitInfoError(r.infoSplit)
	case errors.As(err, &errOutOfRange):
		r.status = statusOutOfRange
		r.err = errOutOfRange
	}
}

func (r *request) get(ctx context.Context) (*objectSDK.Object, error) {
	if r.headOnly() {
		return r.localStorage.Head(ctx, r.address(), r.isRaw())
	}
	if rng := r.ctxRange(); rng != nil {
		return r.localStorage.Range(ctx, r.address(), rng)
	}
	return r.localStorage.Get(ctx, r.address())
}