// Copyright 2025 Sonic Operations Ltd
// This file is part of the Sonic Client
//
// Sonic is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sonic is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Sonic. If not, see <http://www.gnu.org/licenses/>.

package compactdb

import (
	"errors"
	"fmt"
	"math/big"
	"strconv"

	"github.com/0xsoniclabs/sonic/utils/dbutil"

	"github.com/Fantom-foundation/lachesis-base/kvdb"
	"github.com/Fantom-foundation/lachesis-base/kvdb/table"

	"github.com/0xsoniclabs/sonic/utils"
)

type contCompacter struct {
	kvdb.Store
	prev []byte
}

func (s *contCompacter) Compact(_ []byte, limit []byte) error {
	err := s.Store.Compact(s.prev, limit)
	if err != nil {
		return err
	}
	s.prev = limit
	return nil
}

func compact(db *contCompacter, prefix []byte, iters int) error {
	// use heuristic to locate tables
	nonEmptyPrefixes := make([]byte, 0, 256)
	for b := 0; b < 256; b++ {
		if !isEmptyDB(table.New(db, append(prefix, byte(b)))) {
			nonEmptyPrefixes = append(nonEmptyPrefixes, byte(b))
		}
	}
	if len(nonEmptyPrefixes) == 0 {
		return nil
	}
	if len(nonEmptyPrefixes) != 1 && len(nonEmptyPrefixes) < 50 {
		// compact each table individually
		for _, b := range nonEmptyPrefixes {
			if err := compact(db, append(prefix, b), iters); err != nil {
				return err
			}
		}
		return nil
	}

	// once a table is located, split the range into *iters* chunks for compaction
	prefixed := utils.NewTableOrSelf(db, prefix)
	first, _, diff := keysRange(prefixed)
	if diff.Cmp(big.NewInt(int64(iters+10000))) < 0 {
		// skip if too few keys and compact it along with next range
		return nil
	}
	firstBn := new(big.Int).SetBytes(first)
	for i := iters; i >= 1; i-- {
		until := addToKey(firstBn, new(big.Int).Div(diff, big.NewInt(int64(i))), len(first))
		if err := prefixed.Compact(nil, until); err != nil {
			return err
		}
	}
	return nil
}

func Compact(db kvdb.Store, loggingName string, sizePerIter uint64) error {
	loggedDB := &loggedCompacter{
		Store: db,
		name:  loggingName,
		quit:  make(chan struct{}),
	}
	loggedDB.StartLogging()
	defer loggedDB.StopLogging()

	// scale number of iterations based on total DB size and sizePerIter

	measurableStore, isMeasurable := db.(dbutil.MeasurableStore)
	if !isMeasurable {
		return fmt.Errorf("unable to read database used disk space - not a MeasurableStore")
	}

	diskSizeStr, err := measurableStore.UsedDiskSpace()
	if err != nil {
		return err
	}
	var nDiskSize int64
	if nDiskSize, err = strconv.ParseInt(diskSizeStr, 10, 64); err != nil || nDiskSize < 0 {
		return errors.New("bad syntax of disk size entry")
	}

	iters := uint64(nDiskSize) / sizePerIter
	if iters <= 1 {
		// short circuit if too few iterations
		return loggedDB.Compact(nil, nil)
	}

	compacter := &contCompacter{
		Store: loggedDB,
	}
	err = compact(compacter, []byte{}, int(iters))
	if err != nil {
		return err
	}
	return compacter.Compact(nil, nil)
}
