package initialize

import (
	"fmt"
	"math/big"
	"strings"

	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
	"github.com/nspcc-dev/neo-go/pkg/core/native"
	"github.com/nspcc-dev/neo-go/pkg/core/transaction"
	"github.com/nspcc-dev/neo-go/pkg/io"
	"github.com/nspcc-dev/neo-go/pkg/rpcclient/actor"
	"github.com/nspcc-dev/neo-go/pkg/rpcclient/gas"
	"github.com/nspcc-dev/neo-go/pkg/rpcclient/neo"
	"github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17"
	"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
	"github.com/nspcc-dev/neo-go/pkg/util"
	"github.com/nspcc-dev/neo-go/pkg/vm/emit"
	"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
	"github.com/nspcc-dev/neo-go/pkg/wallet"
)

const (
	gasInitialTotalSupply = 30000000 * native.GASFactor
	// initialAlphabetGASAmount represents the amount of GAS given to each alphabet node.
	initialAlphabetGASAmount = 10_000 * native.GASFactor
	// initialProxyGASAmount represents the amount of GAS given to a proxy contract.
	initialProxyGASAmount = 50_000 * native.GASFactor
)

func initialCommitteeGASAmount(c *helper.InitializeContext) int64 {
	return (gasInitialTotalSupply - initialAlphabetGASAmount*int64(len(c.Wallets))) / 2
}

func transferFunds(c *helper.InitializeContext) error {
	ok, err := transferFundsFinished(c)
	if ok || err != nil {
		if err == nil {
			c.Command.Println("Stage 1: already performed.")
		}
		return err
	}

	var transfers []transferTarget
	for _, acc := range c.Accounts {
		to := acc.Contract.ScriptHash()
		transfers = append(transfers,
			transferTarget{
				Token:   gas.Hash,
				Address: to,
				Amount:  initialAlphabetGASAmount,
			},
		)
	}

	// It is convenient to have all funds at the committee account.
	transfers = append(transfers,
		transferTarget{
			Token:   gas.Hash,
			Address: c.CommitteeAcc.Contract.ScriptHash(),
			Amount:  initialCommitteeGASAmount(c),
		},
		transferTarget{
			Token:   neo.Hash,
			Address: c.CommitteeAcc.Contract.ScriptHash(),
			Amount:  native.NEOTotalSupply,
		},
	)

	tx, err := createNEP17MultiTransferTx(c.Client, c.ConsensusAcc, transfers)
	if err != nil {
		return fmt.Errorf("can't create transfer transaction: %w", err)
	}

	if err := c.MultiSignAndSend(tx, constants.ConsensusAccountName); err != nil {
		return fmt.Errorf("can't send transfer transaction: %w", err)
	}

	return c.AwaitTx()
}

// transferFundsFinished checks balances of accounts we transfer GAS to.
// The stage is considered finished if the balance is greater than the half of what we need to transfer.
func transferFundsFinished(c *helper.InitializeContext) (bool, error) {
	acc := c.Accounts[0]

	r := nep17.NewReader(c.ReadOnlyInvoker, gas.Hash)
	res, err := r.BalanceOf(acc.Contract.ScriptHash())
	if err != nil || res.Cmp(big.NewInt(initialAlphabetGASAmount/2)) != 1 {
		return false, err
	}

	res, err = r.BalanceOf(c.CommitteeAcc.ScriptHash())
	return res != nil && res.Cmp(big.NewInt(initialCommitteeGASAmount(c)/2)) == 1, err
}

func transferGASToProxy(c *helper.InitializeContext) error {
	proxyCs := c.GetContract(constants.ProxyContract)

	r := nep17.NewReader(c.ReadOnlyInvoker, gas.Hash)
	bal, err := r.BalanceOf(proxyCs.Hash)
	if err != nil || bal.Sign() > 0 {
		return err
	}

	tx, err := createNEP17MultiTransferTx(c.Client, c.CommitteeAcc, []transferTarget{{
		Token:   gas.Hash,
		Address: proxyCs.Hash,
		Amount:  initialProxyGASAmount,
	}})
	if err != nil {
		return err
	}

	if err := c.MultiSignAndSend(tx, constants.CommitteeAccountName); err != nil {
		return err
	}

	return c.AwaitTx()
}

type transferTarget struct {
	Token   util.Uint160
	Address util.Uint160
	Amount  int64
	Data    any
}

func createNEP17MultiTransferTx(c helper.Client, acc *wallet.Account, recipients []transferTarget) (*transaction.Transaction, error) {
	from := acc.Contract.ScriptHash()

	w := io.NewBufBinWriter()
	for i := range recipients {
		emit.AppCall(w.BinWriter, recipients[i].Token, "transfer", callflag.All,
			from, recipients[i].Address, recipients[i].Amount, recipients[i].Data)
		emit.Opcodes(w.BinWriter, opcode.ASSERT)
	}
	if w.Err != nil {
		return nil, fmt.Errorf("failed to create transfer script: %w", w.Err)
	}

	signers := []actor.SignerAccount{{
		Signer: transaction.Signer{
			Account: acc.Contract.ScriptHash(),
			Scopes:  transaction.CalledByEntry,
		},
		Account: acc,
	}}

	act, err := actor.New(c, signers)
	if err != nil {
		return nil, fmt.Errorf("can't create actor: %w", err)
	}
	tx, err := act.MakeRun(w.Bytes())
	if err != nil {
		sum := make(map[util.Uint160]int64)
		for _, recipient := range recipients {
			sum[recipient.Token] += recipient.Amount
		}
		detail := make([]string, 0, len(sum))
		for _, value := range sum {
			detail = append(detail, fmt.Sprintf("amount=%v", value))
		}
		err = fmt.Errorf("transfer failed: from=%s(%s) %s: %w", acc.Label, acc.Address, strings.Join(detail, " "), err)
	}
	return tx, err
}