// Copyright 2016 The go-ethereum Authors
// (original work)
// Copyright 2024 The Erigon Authors
// (modifications)
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.

package logger

import (
	"bytes"
	"encoding/json"
	"math/big"
	"testing"

	"github.com/holiman/uint256"

	"github.com/erigontech/erigon/common"
	"github.com/erigontech/erigon/execution/chain"
	"github.com/erigontech/erigon/execution/protocol/mdgas"
	"github.com/erigontech/erigon/execution/protocol/params"
	"github.com/erigontech/erigon/execution/state"
	"github.com/erigontech/erigon/execution/tracing"
	"github.com/erigontech/erigon/execution/types/accounts"
	"github.com/erigontech/erigon/execution/vm"
	"github.com/erigontech/erigon/execution/vm/evmtypes"
)

type dummyContractRef struct {
	calledForEach bool
}

func (dummyContractRef) ReturnGas(*big.Int)          {}
func (dummyContractRef) Address() common.Address     { return accounts.ZeroAddress.Value() }
func (dummyContractRef) Value() *big.Int             { return new(big.Int) }
func (dummyContractRef) SetCode(common.Hash, []byte) {}
func (d *dummyContractRef) ForEachStorage(callback func(key, value common.Hash) bool) {
	d.calledForEach = true
}
func (d *dummyContractRef) SubBalance(amount *big.Int) {}
func (d *dummyContractRef) AddBalance(amount *big.Int) {}
func (d *dummyContractRef) SetBalance(*big.Int)        {}
func (d *dummyContractRef) SetNonce(uint64)            {}
func (d *dummyContractRef) Balance() *big.Int          { return new(big.Int) }

func TestStoreCapture(t *testing.T) {
	//c := vm.NewJumpDestCache(128)
	ibs := state.New(state.NewNoopReader())
	defer ibs.Release(false)
	ibs.AddRefund(1337)

	var (
		logger   = NewStructLogger(nil)
		evm      = vm.NewEVM(evmtypes.BlockContext{}, evmtypes.TxContext{}, ibs, chain.AllProtocolChanges, vm.Config{Tracer: logger.Hooks()})
		contract = *vm.NewContract(accounts.ZeroAddress, accounts.ZeroAddress, accounts.ZeroAddress, uint256.Int{})
	)
	contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x0, byte(vm.SSTORE)}
	var index common.Hash
	logger.OnTxStart(evm.GetVMContext(), nil, accounts.ZeroAddress)
	_, _, _, err := evm.Run(contract, mdgas.MdGas{Regular: 200_000, State: params.StateGasPerStorageSet}, []byte{}, false)
	if err != nil {
		t.Fatal(err)
	}
	if len(logger.storage[contract.Address()]) == 0 {
		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(),
			len(logger.storage[contract.Address()]))
	}
	exp := common.BigToHash(big.NewInt(1))
	if logger.storage[contract.Address()][index] != exp {
		t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
	}
}

func TestJSONLoggerOnSystemCallStartSetsEnv(t *testing.T) {
	var buf bytes.Buffer
	logger := NewJSONLogger(nil, &buf)
	logger.OnSystemCallStartV2(&tracing.VMContext{IntraBlockState: &mockIBS{}})

	scope := &mockOpContext{}
	logger.OnOpcode(0, byte(vm.STOP), 100, 0, scope, nil, 0, nil)

	var entry map[string]json.RawMessage
	if err := json.Unmarshal(bytes.TrimSpace(buf.Bytes()), &entry); err != nil {
		t.Fatalf("failed to decode json logger output: %v", err)
	}
	if _, ok := entry["refund"]; !ok {
		t.Fatal("expected json logger to emit opcode output after system call start")
	}
}

//func TestStoreCapture(t *testing.T) {
//	c := vm.NewJumpDestCache()
//	var (
//		logger   = NewStructLogger(nil)
//		env      = vm.NewEVM(evmtypes.BlockContext{}, evmtypes.TxContext{}, &dummyStatedb{}, chain.AllProtocolChanges, vm.Config{Tracer: logger.Hooks()})
//		mem      = vm.NewMemory()
//		stack    = vm.New()
//		contract = vm.NewContract(&dummyContractRef{}, accounts.ZeroAddress, new(uint256.Int), 0, c)
//	)
//	stack.push(uint256.NewInt(1))
//	stack.push(uint256.NewInt(0))
//	var index common.Hash
//	logger.OnTxStart(env.GetVMContext(), nil, accounts.ZeroAddress)
//	logger.OnOpcode(0, byte(vm.SSTORE), 0, 0, &vm.ScopeContext{
//		Memory:   mem,
//		Stack:    stack,
//		Contract: contract,
//	}, nil, 0, nil)
//
//	if len(logger.storage[contract.Address()]) == 0 {
//		t.Fatalf("expected exactly 1 changed value on address %x, got %d", contract.Address(), len(logger.storage[contract.Address()]))
//	}
//	exp := common.BigToHash(big.NewInt(1))
//	if logger.storage[contract.Address()][index] != exp {
//		t.Errorf("expected %x, got %x", exp, logger.storage[contract.Address()][index])
//	}
//}
