package consensus

import (
	"dejo_node/internal/staking"
	"log"
)

const SlashAmount = 50 // tokens penalizados

// ApplySlash penaliza validadores que não votaram corretamente.
func ApplySlash(precommits map[string]string, blockHash string, stakingStore *staking.StakingStore, validatorSet *ValidatorSet) {
	for _, val := range validatorSet.Validators {
		vote, voted := precommits[val.Address]
		if !voted || vote != blockHash {
			// Slashing
			info, exists := stakingStore.GetStakeInfo(val.Address)
			if exists && info.Amount >= SlashAmount {
				newInfo := staking.StakeInfo{
					Amount:   info.Amount - SlashAmount,
					Duration: uint64(info.Duration),
				}
				stakingStore.UpdateStakeInfo(val.Address, newInfo)
				log.Printf("⚡ SLASH: Validador %s penalizado em %d tokens\n", val.Address, SlashAmount)
			}
		}
	}
	_ = stakingStore.SaveToDisk("data/staking.db")
}