package manage

import (
	"context"
	"crypto/ecdsa"
	"fmt"
	"strings"

	"github.com/exchain/go-exchain/op-deployer/pkg/deployer"
	"github.com/exchain/go-exchain/op-deployer/pkg/deployer/artifacts"
	"github.com/exchain/go-exchain/op-deployer/pkg/deployer/bootstrap"
	"github.com/exchain/go-exchain/op-deployer/pkg/deployer/broadcaster"
	"github.com/exchain/go-exchain/op-deployer/pkg/deployer/opcm"
	"github.com/exchain/go-exchain/op-deployer/pkg/env"
	op_service "github.com/exchain/go-exchain/op-service"
	opcrypto "github.com/exchain/go-exchain/op-service/crypto"
	"github.com/exchain/go-exchain/op-service/ctxinterrupt"
	oplog "github.com/exchain/go-exchain/op-service/log"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
	"github.com/ethereum/go-ethereum/ethclient"
	"github.com/ethereum/go-ethereum/log"
	"github.com/ethereum/go-ethereum/rpc"
	"github.com/urfave/cli/v2"
)

type DependenciesConfig struct {
	L1RPCUrl         string
	PrivateKey       string
	Logger           log.Logger
	ArtifactsLocator *artifacts.Locator
	ChainID          common.Hash
	SystemConfig     common.Address
	Remove           bool

	privateKeyECDSA *ecdsa.PrivateKey
}

func (c *DependenciesConfig) Check() error {
	if c.L1RPCUrl == "" {
		return fmt.Errorf("l1RPCUrl must be specified")
	}

	if c.PrivateKey == "" {
		return fmt.Errorf("private key must be specified")
	}

	privECDSA, err := crypto.HexToECDSA(strings.TrimPrefix(c.PrivateKey, "0x"))
	if err != nil {
		return fmt.Errorf("failed to parse private key: %w", err)
	}
	c.privateKeyECDSA = privECDSA

	if c.Logger == nil {
		return fmt.Errorf("logger must be specified")
	}

	if c.ArtifactsLocator == nil {
		return fmt.Errorf("artifacts locator must be specified")
	}

	if c.ChainID == (common.Hash{}) {
		return fmt.Errorf("chain ID must be specified")
	}

	if c.SystemConfig == (common.Address{}) {
		return fmt.Errorf("system config must be specified")
	}

	return nil
}

func DependenciesCLI(cliCtx *cli.Context) error {
	logCfg := oplog.ReadCLIConfig(cliCtx)
	l := oplog.NewLogger(oplog.AppOut(cliCtx), logCfg)
	oplog.SetGlobalLogHandler(l.Handler())

	l1RPCUrl := cliCtx.String(deployer.L1RPCURLFlagName)
	privateKey := cliCtx.String(deployer.PrivateKeyFlagName)
	artifactsURLStr := cliCtx.String(bootstrap.ArtifactsLocatorFlagName)
	artifactsLocator := new(artifacts.Locator)
	if err := artifactsLocator.UnmarshalText([]byte(artifactsURLStr)); err != nil {
		return fmt.Errorf("failed to parse artifacts URL: %w", err)
	}

	chainID, err := op_service.Parse256BitChainID(cliCtx.String(ChainIDFlagName))
	if err != nil {
		return fmt.Errorf("failed to parse chain ID: %w", err)
	}
	systemConfig := common.HexToAddress(cliCtx.String(SystemConfigFlagName))

	remove := cliCtx.Bool(RemoveFlagName)

	cfg := DependenciesConfig{
		L1RPCUrl:         l1RPCUrl,
		PrivateKey:       privateKey,
		Logger:           l,
		ArtifactsLocator: artifactsLocator,
		ChainID:          chainID,
		SystemConfig:     systemConfig,
		Remove:           remove,
	}

	ctx := ctxinterrupt.WithCancelOnInterrupt(cliCtx.Context)

	return Dependencies(ctx, cfg)
}

func Dependencies(ctx context.Context, cfg DependenciesConfig) error {
	if err := cfg.Check(); err != nil {
		return err
	}

	lgr := cfg.Logger
	progressor := func(curr, total int64) {
		lgr.Info("artifacts download progress", "current", curr, "total", total)
	}

	artifactsFS, cleanup, err := artifacts.Download(ctx, cfg.ArtifactsLocator, progressor)
	if err != nil {
		return fmt.Errorf("failed to download artifacts: %w", err)
	}
	defer func() {
		if err := cleanup(); err != nil {
			lgr.Warn("failed to clean up artifacts", "err", err)
		}
	}()

	l1RPC, err := rpc.Dial(cfg.L1RPCUrl)
	if err != nil {
		return fmt.Errorf("failed to connect to L1 RPC: %w", err)
	}

	l1Client := ethclient.NewClient(l1RPC)

	chainID, err := l1Client.ChainID(ctx)
	if err != nil {
		return fmt.Errorf("failed to get chain ID: %w", err)
	}

	signer := opcrypto.SignerFnFromBind(opcrypto.PrivateKeySignerFn(cfg.privateKeyECDSA, chainID))
	chainDeployer := crypto.PubkeyToAddress(cfg.privateKeyECDSA.PublicKey)

	bcaster, err := broadcaster.NewKeyedBroadcaster(broadcaster.KeyedBroadcasterOpts{
		Logger:  lgr,
		ChainID: chainID,
		Client:  l1Client,
		Signer:  signer,
		From:    chainDeployer,
	})
	if err != nil {
		return fmt.Errorf("failed to create broadcaster: %w", err)
	}

	host, err := env.DefaultForkedScriptHost(
		ctx,
		bcaster,
		lgr,
		chainDeployer,
		artifactsFS,
		l1RPC,
	)
	if err != nil {
		return fmt.Errorf("failed to create script host: %w", err)
	}

	input := opcm.ManageDependenciesInput{
		ChainId:      cfg.ChainID.Big(),
		SystemConfig: cfg.SystemConfig,
		Remove:       cfg.Remove,
	}

	msg := "adding dependencies"
	if cfg.Remove {
		msg = "removing dependencies"
	}
	lgr.Info(msg)

	if err := opcm.ManageDependencies(host, input); err != nil {
		return fmt.Errorf("error running manage dependencies script: %w", err)
	}

	if _, err := bcaster.Broadcast(ctx); err != nil {
		return fmt.Errorf("failed to broadcast transactions: %w", err)
	}

	lgr.Info("success")
	return nil
}
