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

package common

import (
	"context"
	"math/big"
	"time"

	"github.com/luxfi/ids"
	"github.com/luxfi/math/set"
	"github.com/luxfi/utxo/secp256k1fx"

	ethcommon "github.com/luxfi/geth/common"
)

const defaultPollFrequency = 100 * time.Millisecond

// Signature of the function that will be called after a transaction
// has been issued with the ID of the issued transaction.
type PostIssuanceFunc func(ids.ID)

// ConfirmationReceipt contains information about a confirmed transaction
type ConfirmationReceipt struct {
	ChainAlias           string
	TxID                 ids.ID
	IssuanceDuration     time.Duration
	ConfirmationDuration time.Duration
	TotalDuration        time.Duration
}

// ConfirmationHandler is called when a transaction is confirmed
type ConfirmationHandler func(ConfirmationReceipt)

type Option func(*Options)

type Options struct {
	ctx context.Context

	customAddressesSet bool
	customAddresses    set.Set[ids.ShortID]

	customEVMAddressesSet bool
	customEVMAddresses    set.Set[ethcommon.Address]

	baseFee *big.Int

	minIssuanceTimeSet bool
	minIssuanceTime    uint64

	allowStakeableLocked bool

	changeOwner *secp256k1fx.OutputOwners

	memo []byte

	assumeDecided bool

	pollFrequencySet bool
	pollFrequency    time.Duration

	postIssuanceFunc    PostIssuanceFunc
	confirmationHandler ConfirmationHandler
}

func NewOptions(ops []Option) *Options {
	o := &Options{}
	o.applyOptions(ops)
	return o
}

func UnionOptions(first, second []Option) []Option {
	firstLen := len(first)
	newOptions := make([]Option, firstLen+len(second))
	copy(newOptions, first)
	copy(newOptions[firstLen:], second)
	return newOptions
}

func (o *Options) applyOptions(ops []Option) {
	for _, op := range ops {
		op(o)
	}
}

func (o *Options) Context() context.Context {
	if o.ctx != nil {
		return o.ctx
	}
	return context.Background()
}

func (o *Options) Addresses(defaultAddresses set.Set[ids.ShortID]) set.Set[ids.ShortID] {
	if o.customAddressesSet {
		return o.customAddresses
	}
	return defaultAddresses
}

// EVMAddresses returns the configured set of 20-byte EVM-runtime
// account addresses (the format consumed by Lux C-Chain, regulated EVM L1,
// and every EVM-compatible chain). Naming reflects the data model
// that consumes the value (EVM account), not the hash primitive that
// derives it.
func (o *Options) EVMAddresses(defaultAddresses set.Set[ethcommon.Address]) set.Set[ethcommon.Address] {
	if o.customEVMAddressesSet {
		return o.customEVMAddresses
	}
	return defaultAddresses
}

func (o *Options) BaseFee(defaultBaseFee *big.Int) *big.Int {
	if o.baseFee != nil {
		return o.baseFee
	}
	return defaultBaseFee
}

func (o *Options) MinIssuanceTime() uint64 {
	if o.minIssuanceTimeSet {
		return o.minIssuanceTime
	}
	return uint64(time.Now().Unix())
}

func (o *Options) AllowStakeableLocked() bool {
	return o.allowStakeableLocked
}

func (o *Options) ChangeOwner(defaultOwner *secp256k1fx.OutputOwners) *secp256k1fx.OutputOwners {
	if o.changeOwner != nil {
		return o.changeOwner
	}
	return defaultOwner
}

func (o *Options) Memo() []byte {
	return o.memo
}

func (o *Options) AssumeDecided() bool {
	return o.assumeDecided
}

func (o *Options) PollFrequency() time.Duration {
	if o.pollFrequencySet {
		return o.pollFrequency
	}
	return defaultPollFrequency
}

func (o *Options) PostIssuanceFunc() PostIssuanceFunc {
	return o.postIssuanceFunc
}

func (o *Options) ConfirmationHandler() ConfirmationHandler {
	return o.confirmationHandler
}

func WithContext(ctx context.Context) Option {
	return func(o *Options) {
		o.ctx = ctx
	}
}

func WithCustomAddresses(addrs set.Set[ids.ShortID]) Option {
	return func(o *Options) {
		o.customAddressesSet = true
		o.customAddresses = addrs
	}
}

// WithCustomEVMAddresses sets the option's filter to a custom set of
// 20-byte EVM-runtime account addresses. The value IS "EVM-runtime
// account address" — that's the data model.
func WithCustomEVMAddresses(addrs set.Set[ethcommon.Address]) Option {
	return func(o *Options) {
		o.customEVMAddressesSet = true
		o.customEVMAddresses = addrs
	}
}

func WithBaseFee(baseFee *big.Int) Option {
	return func(o *Options) {
		o.baseFee = baseFee
	}
}

func WithMinIssuanceTime(minIssuanceTime uint64) Option {
	return func(o *Options) {
		o.minIssuanceTimeSet = true
		o.minIssuanceTime = minIssuanceTime
	}
}

func WithStakeableLocked() Option {
	return func(o *Options) {
		o.allowStakeableLocked = true
	}
}

func WithChangeOwner(changeOwner *secp256k1fx.OutputOwners) Option {
	return func(o *Options) {
		o.changeOwner = changeOwner
	}
}

func WithMemo(memo []byte) Option {
	return func(o *Options) {
		o.memo = memo
	}
}

func WithAssumeDecided() Option {
	return func(o *Options) {
		o.assumeDecided = true
	}
}

func WithPollFrequency(pollFrequency time.Duration) Option {
	return func(o *Options) {
		o.pollFrequencySet = true
		o.pollFrequency = pollFrequency
	}
}

func WithPostIssuanceFunc(f PostIssuanceFunc) Option {
	return func(o *Options) {
		o.postIssuanceFunc = f
	}
}

func WithConfirmationHandler(f ConfirmationHandler) Option {
	return func(o *Options) {
		o.confirmationHandler = f
	}
}
