package bridge

import (
	"context"
	"fmt"
)

// Provider interface for bridge providers
type Provider interface {
	GetQuote(ctx context.Context, req *BridgeRequest) (*BridgeQuote, error)
	Name() string
	SupportsRoute(fromChain, toChain int) bool
}

// BridgeRequest represents a bridge request
type BridgeRequest struct {
	FromChain int
	ToChain   int
	FromToken string
	ToToken   string
	Amount    string
	Recipient string
}

// BridgeQuote represents a bridge quote
type BridgeQuote struct {
	Provider      string
	FromChain     int
	ToChain       int
	FromAmount    string
	ToAmount      string
	Fee           string
	EstimatedTime string
	Route         []BridgeStep
}

// BridgeStep represents a step in bridge route
type BridgeStep struct {
	Provider string
	From     string
	To       string
	Type     string // "bridge" or "swap"
}

// Aggregator aggregates quotes from multiple bridge providers
type Aggregator struct {
	providers []Provider
}

// NewAggregator creates a new bridge aggregator with all providers
func NewAggregator() *Aggregator {
	return &Aggregator{
		providers: []Provider{
			NewLiFiProvider(),      // Li.Fi: 40+ chains, swap+bridge aggregation
			NewSocketProvider(),    // Socket/Bungee: 40+ chains
			NewSquidProvider(),     // Squid: Axelar-based, 50+ chains
			NewSymbiosisProvider(), // Symbiosis: 30+ chains
			NewRelayProvider(),     // Relay.link: EVM chains
			NewStargateProvider(),  // Stargate: LayerZero
			NewCCIPProvider(),      // Chainlink CCIP (138 <-> 1)
			NewHopProvider(),       // Hop Protocol (ETH <-> L2)
		},
	}
}

// GetBestQuote gets the best quote from all providers
func (a *Aggregator) GetBestQuote(ctx context.Context, req *BridgeRequest) (*BridgeQuote, error) {
	var bestQuote *BridgeQuote
	var bestAmount string

	for _, provider := range a.providers {
		if !provider.SupportsRoute(req.FromChain, req.ToChain) {
			continue
		}

		quote, err := provider.GetQuote(ctx, req)
		if err != nil {
			continue
		}

		if bestQuote == nil || quote.ToAmount > bestAmount {
			bestQuote = quote
			bestAmount = quote.ToAmount
		}
	}

	if bestQuote == nil {
		return nil, fmt.Errorf("no bridge quotes available")
	}

	return bestQuote, nil
}

// CCIPProvider is implemented in ccip_provider.go
// HopProvider is implemented in hop_provider.go
