package storage

import (
	"errors"
	"os"
	"path/filepath"
	"strconv"
)

const snapshotFile = "state_snapshot.txt"

func (bs *BlockStore) SetSnapshotHeight(index uint64) error {
	file := filepath.Join(bs.Path, snapshotFile)
	return os.WriteFile(file, []byte(strconv.FormatUint(index, 10)), 0644)
}

func (bs *BlockStore) GetSnapshotHeight() (uint64, error) {
	file := filepath.Join(bs.Path, snapshotFile)
	data, err := os.ReadFile(file)
	if err != nil {
		return 0, errors.New("nenhum snapshot salvo")
	}
	return strconv.ParseUint(string(data), 10, 64)
}