// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package state

import (
	"github.com/luxfi/database"
	"github.com/luxfi/ids"
)

type delegatorMetadata struct {
	PotentialReward uint64 `serialize:"true"`
	StakerStartTime uint64 `serialize:"true"`

	txID ids.ID
}

func parseDelegatorMetadata(bytes []byte, metadata *delegatorMetadata) error {
	var err error
	switch len(bytes) {
	case database.Uint64Size:
		// only potential reward was stored
		metadata.PotentialReward, err = database.ParseUInt64(bytes)
	default:
		_, err = MetadataCodec.Unmarshal(bytes, metadata)
	}
	return err
}

func writeDelegatorMetadata(db database.KeyValueWriter, metadata *delegatorMetadata, codecVersion uint16) error {
	// The "0" codec is skipped for [delegatorMetadata]. This is to ensure the
	// [validatorMetadata] codec version is the same as the [delegatorMetadata]
	// codec version.
	//
	if codecVersion == 0 {
		return database.PutUInt64(db, metadata.txID[:], metadata.PotentialReward)
	}
	metadataBytes, err := MetadataCodec.Marshal(codecVersion, metadata)
	if err != nil {
		return err
	}
	return db.Put(metadata.txID[:], metadataBytes)
}
