package cli

import (
	"context"
	"fmt"
	"sort"

	"github.com/pkg/errors"

	"github.com/kopia/kopia/repo"
)

var (
	blockIndexListCommand           = indexCommands.Command("list", "List content indexes").Alias("ls").Default()
	blockIndexListSummary           = blockIndexListCommand.Flag("summary", "Display index blob summary").Bool()
	blockIndexListIncludeSuperseded = blockIndexListCommand.Flag("superseded", "Include inactive index files superseded by compaction").Bool()
	blockIndexListSort              = blockIndexListCommand.Flag("sort", "Index blob sort order").Default("time").Enum("time", "size", "name")
)

func runListBlockIndexesAction(ctx context.Context, rep repo.DirectRepository) error {
	var jl jsonList

	jl.begin()
	defer jl.end()

	blks, err := rep.IndexBlobReader().IndexBlobs(ctx, *blockIndexListIncludeSuperseded)
	if err != nil {
		return errors.Wrap(err, "error listing index blobs")
	}

	switch *blockIndexListSort {
	case "time":
		sort.Slice(blks, func(i, j int) bool {
			return blks[i].Timestamp.Before(blks[j].Timestamp)
		})
	case "size":
		sort.Slice(blks, func(i, j int) bool {
			return blks[i].Length < blks[j].Length
		})
	case "name":
		sort.Slice(blks, func(i, j int) bool {
			return blks[i].BlobID < blks[j].BlobID
		})
	}

	for _, b := range blks {
		if jsonOutput {
			jl.emit(b)
		} else {
			fmt.Printf("%-40v %10v %v %v\n", b.BlobID, b.Length, formatTimestampPrecise(b.Timestamp), b.Superseded)
		}
	}

	if *blockIndexListSummary && !jsonOutput {
		fmt.Printf("total %v indexes\n", len(blks))
	}

	return nil
}

func init() {
	registerJSONOutputFlags(blockIndexListCommand)
	blockIndexListCommand.Action(directRepositoryReadAction(runListBlockIndexesAction))
}
