package shard

import (
	"context"
	"fmt"

	meta "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/metabase"
	"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
	cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

// Lock marks objects as locked with another object. All objects from the
// specified container.
//
// Allows locking regular objects only (otherwise returns apistatus.LockNonRegularObject).
//
// Locked list should be unique. Panics if it is empty.
func (s *Shard) Lock(ctx context.Context, idCnr cid.ID, locker oid.ID, locked []oid.ID) error {
	ctx, span := tracing.StartSpanFromContext(ctx, "Shard.Lock",
		trace.WithAttributes(
			attribute.String("shard_id", s.ID().String()),
			attribute.String("container_id", idCnr.EncodeToString()),
			attribute.String("locker", locker.EncodeToString()),
			attribute.Int("locked_count", len(locked)),
		))
	defer span.End()

	s.m.RLock()
	defer s.m.RUnlock()

	m := s.info.Mode
	if m.ReadOnly() {
		return ErrReadOnlyMode
	} else if m.NoMetabase() {
		return ErrDegradedMode
	}

	err := s.metaBase.Lock(ctx, idCnr, locker, locked)
	if err != nil {
		return fmt.Errorf("metabase lock: %w", err)
	}

	return nil
}

// IsLocked checks object locking relation of the provided object. Not found object is
// considered as not locked. Requires healthy metabase, returns ErrDegradedMode otherwise.
func (s *Shard) IsLocked(ctx context.Context, addr oid.Address) (bool, error) {
	ctx, span := tracing.StartSpanFromContext(ctx, "Shard.IsLocked",
		trace.WithAttributes(
			attribute.String("shard_id", s.ID().String()),
			attribute.String("address", addr.EncodeToString()),
		))
	defer span.End()

	m := s.GetMode()
	if m.NoMetabase() {
		return false, ErrDegradedMode
	}

	var prm meta.IsLockedPrm
	prm.SetAddress(addr)

	res, err := s.metaBase.IsLocked(ctx, prm)
	if err != nil {
		return false, err
	}

	return res.Locked(), nil
}