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

package poa

import (
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	consensusconfig "github.com/luxfi/consensus/config"
	"github.com/luxfi/node/config"
	"github.com/luxfi/node/nets"
)

func TestPOAConsensusParameters(t *testing.T) {
	require := require.New(t)

	// Test that POA consensus parameters are correctly set
	params := nets.GetPOAConsensusParameters()

	// POA mode should have K=1, Alpha=1, Beta=1 for single-node operation
	require.Equal(1, params.K, "POA mode should have K=1")
	require.Equal(1, params.AlphaPreference, "POA mode should have AlphaPreference=1")
	require.Equal(1, params.AlphaConfidence, "POA mode should have AlphaConfidence=1")
	require.Equal(uint32(1), params.Beta, "POA mode should have Beta=1")
	require.Equal(1, params.ConcurrentPolls, "POA mode should have ConcurrentPolls=1")
}

func TestPOANetConfig(t *testing.T) {
	require := require.New(t)

	// Test net config with POA enabled
	cfg := nets.Config{
		POAEnabled:        true,
		POASingleNodeMode: true,
		POAMinBlockTime:   1 * time.Second,
		ConsensusParameters: consensusconfig.Parameters{
			K:                     1,
			Alpha:                 0.69, // Must be between 0.68 and 1.0
			AlphaPreference:       1,
			AlphaConfidence:       1,
			Beta:                  uint32(1),
			ConcurrentPolls:       1,
			OptimalProcessing:     10,
			MaxOutstandingItems:   256,
			MaxItemProcessingTime: 30 * time.Second,
		},
	}

	// Verify configuration is valid
	err := cfg.Valid()
	require.NoError(err, "POA net config should be valid")
}

func TestPOAConfigKeys(t *testing.T) {
	require := require.New(t)

	// Test that POA configuration keys are defined
	require.Equal("poa-mode-enabled", config.POAModeEnabledKey)
	require.Equal("poa-single-node-mode", config.POASingleNodeModeKey)
	require.Equal("poa-min-block-time", config.POAMinBlockTimeKey)
	require.Equal("poa-authorized-nodes", config.POAAuthorizedNodesKey)
}
