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

package xvm

import (
	"github.com/luxfi/node/upgrade"
	"fmt"
	"math"
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/luxfi/vm"
	"github.com/luxfi/constants"
	"github.com/luxfi/crypto/secp256k1"
	"github.com/luxfi/ids"
	
	lux "github.com/luxfi/utxo"
	"github.com/luxfi/node/vms/xvm/txs"
	"github.com/luxfi/utxo/secp256k1fx"
)

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

	// secp256k1fx is now included by default, so we only need the custom Fx
	env := setup(t, &envConfig{
		fork: upgrade.Default,
		additionalFxs: []interface{}{
			&vm.Fx{
				ID: ids.GenerateTestID(),
				Fx: &FxTest{
					InitializeF: func(vmIntf interface{}) error {
						// The VM passed here is actually a txs.fxVM which implements secp256k1fx.VM.
						// Post-ZAP, fx Initialize is a no-op (no codec registration chain).
						if _, ok := vmIntf.(secp256k1fx.VM); !ok {
							return fmt.Errorf("unexpected VM type: %T", vmIntf)
						}
						return nil
					},
				},
			},
		},
	})
	defer env.testLock.Unlock()

	utxo := &lux.UTXO{
		UTXOID: lux.UTXOID{
			TxID:        ids.Empty,
			OutputIndex: 1,
		},
		Asset: lux.Asset{ID: ids.Empty},
		Out:   &lux.TestState{},
	}
	utxoID := utxo.InputID()

	tx := &txs.Tx{Unsigned: &txs.BaseTx{BaseTx: lux.BaseTx{
		NetworkID:    constants.UnitTestID,
		BlockchainID: env.consensusRuntime.XChainID,
		Ins: []*lux.TransferableInput{{
			UTXOID: lux.UTXOID{
				TxID:        ids.Empty,
				OutputIndex: 0,
			},
			Asset: lux.Asset{ID: assetID},
			In: &secp256k1fx.TransferInput{
				Amt: 20 * constants.KiloLux,
				Input: secp256k1fx.Input{
					SigIndices: []uint32{
						0,
					},
				},
			},
		}},
	}}}
	require.NoError(tx.SignSECP256K1Fx(env.vm.parser.Codec(), [][]*secp256k1.PrivateKey{{keys[0]}}))

	txID := tx.ID()

	env.vm.state.AddUTXO(utxo)
	env.vm.state.AddTx(tx)

	resultUTXO, err := env.vm.state.GetUTXO(utxoID)
	require.NoError(err)
	resultTx, err := env.vm.state.GetTx(txID)
	require.NoError(err)

	require.Equal(uint32(1), resultUTXO.OutputIndex)
	require.Equal(tx.ID(), resultTx.ID())
}

func TestFundingNoAddresses(t *testing.T) {
	// secp256k1fx is now included by default, so we only need the custom Fx
	env := setup(t, &envConfig{
		fork: upgrade.Default,
		additionalFxs: []interface{}{
			&vm.Fx{
				ID: ids.GenerateTestID(),
				Fx: &FxTest{
					InitializeF: func(vmIntf interface{}) error {
						// The VM passed here is actually a txs.fxVM which implements secp256k1fx.VM.
						// Post-ZAP, fx Initialize is a no-op (no codec registration chain).
						if _, ok := vmIntf.(secp256k1fx.VM); !ok {
							return fmt.Errorf("unexpected VM type: %T", vmIntf)
						}
						return nil
					},
				},
			},
		},
	})
	defer env.testLock.Unlock()

	utxo := &lux.UTXO{
		UTXOID: lux.UTXOID{
			TxID:        ids.Empty,
			OutputIndex: 1,
		},
		Asset: lux.Asset{ID: ids.Empty},
		Out:   &lux.TestState{},
	}

	env.vm.state.AddUTXO(utxo)
	env.vm.state.DeleteUTXO(utxo.InputID())
}

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

	// secp256k1fx is now included by default, so we only need the custom Fx
	env := setup(t, &envConfig{
		fork: upgrade.Default,
		additionalFxs: []interface{}{
			&vm.Fx{
				ID: ids.GenerateTestID(),
				Fx: &FxTest{
					InitializeF: func(vmIntf interface{}) error {
						// Post-ZAP, fx Initialize is a no-op (no codec registration chain).
						_ = vmIntf.(secp256k1fx.VM)
						return nil
					},
				},
			},
		},
	})
	defer env.testLock.Unlock()

	utxo := &lux.UTXO{
		UTXOID: lux.UTXOID{
			TxID:        ids.Empty,
			OutputIndex: 1,
		},
		Asset: lux.Asset{ID: ids.Empty},
		Out: &lux.TestAddressable{
			Addrs: [][]byte{{0}},
		},
	}

	env.vm.state.AddUTXO(utxo)
	require.NoError(env.vm.state.Commit())

	utxos, err := env.vm.state.UTXOIDs([]byte{0}, ids.Empty, math.MaxInt32)
	require.NoError(err)
	require.Len(utxos, 1)
	require.Equal(utxo.InputID(), utxos[0])

	env.vm.state.DeleteUTXO(utxo.InputID())
	require.NoError(env.vm.state.Commit())

	utxos, err = env.vm.state.UTXOIDs([]byte{0}, ids.Empty, math.MaxInt32)
	require.NoError(err)
	require.Empty(utxos)
}
