package writecache

import (
	"context"
	"errors"
	"fmt"
	"os"
	"time"

	"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/blobstor/common"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/shard/mode"
	"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/trace"
)

type setModePrm struct {
	ignoreErrors bool
	shrink       bool
}

// SetMode sets write-cache mode of operation.
// When shard is put in read-only mode all objects in memory are flushed to disk
// and all background jobs are suspended.
func (c *cache) SetMode(ctx context.Context, m mode.Mode) error {
	ctx, span := tracing.StartSpanFromContext(ctx, "writecache.SetMode",
		trace.WithAttributes(
			attribute.String("mode", m.String()),
		))
	defer span.End()

	c.modeMtx.Lock()
	defer c.modeMtx.Unlock()

	err := c.setMode(ctx, m, setModePrm{ignoreErrors: true})
	if err == nil {
		c.metrics.SetMode(mode.ConvertToComponentModeDegraded(m))
	}
	return err
}

// setMode applies new mode. Must be called with cache.modeMtx lock taken.
func (c *cache) setMode(ctx context.Context, m mode.Mode, prm setModePrm) error {
	var err error
	turnOffMeta := m.NoMetabase()

	if turnOffMeta && !c.mode.NoMetabase() {
		err = c.flush(ctx, prm.ignoreErrors)
		if err != nil {
			return err
		}
	}

	if err := c.closeStorage(ctx, prm.shrink); err != nil {
		return err
	}

	// Suspend producers to ensure there are channel send operations in fly.
	// flushCh is populated by `flush` with `modeMtx` taken, thus waiting until it is empty
	// guarantees that there are no in-fly operations.
	for len(c.flushCh) != 0 {
		c.log.Info(ctx, logs.WritecacheWaitingForChannelsToFlush)
		time.Sleep(time.Second)
	}

	if turnOffMeta {
		c.mode = m
		return nil
	}

	if err = c.openStore(mode.ConvertToComponentModeDegraded(m)); err != nil {
		return err
	}

	c.mode = m
	return nil
}

func (c *cache) closeStorage(ctx context.Context, shrink bool) error {
	if c.fsTree == nil {
		return nil
	}
	if !shrink {
		if err := c.fsTree.Close(ctx); err != nil {
			return fmt.Errorf("can't close write-cache storage: %w", err)
		}
		return nil
	}

	empty := true
	_, err := c.fsTree.Iterate(ctx, common.IteratePrm{
		Handler: func(common.IterationElement) error {
			return errIterationCompleted
		},
	})
	if err != nil {
		if errors.Is(err, errIterationCompleted) {
			empty = false
		} else {
			return fmt.Errorf("failed to check write-cache items: %w", err)
		}
	}
	if err := c.fsTree.Close(ctx); err != nil {
		return fmt.Errorf("can't close write-cache storage: %w", err)
	}
	if empty {
		err := os.RemoveAll(c.path)
		if err != nil && !os.IsNotExist(err) {
			return fmt.Errorf("failed to remove write-cache files: %w", err)
		}
	} else {
		c.log.Info(ctx, logs.WritecacheShrinkSkippedNotEmpty)
	}
	return nil
}

// readOnly returns true if current mode is read-only.
// `c.modeMtx` must be taken.
func (c *cache) readOnly() bool {
	return c.mode.ReadOnly()
}

// noMetabase returns true if c is operating without the metabase.
// `c.modeMtx` must be taken.
func (c *cache) noMetabase() bool {
	return c.mode.NoMetabase()
}