package vo

import (
	"fmt"
	"strings"
)

// ChainType is a value object representing a supported blockchain network.
type ChainType string

const (
	ChainTypeEthereum ChainType = "ethereum"
	ChainTypeBSC      ChainType = "bsc"
	ChainTypePolygon  ChainType = "polygon"
	ChainTypeSolana   ChainType = "solana"
	ChainTypeTron     ChainType = "tron"
	ChainTypeGenex    ChainType = "genex"
)

// supportedChains enumerates all recognized chain types.
var supportedChains = map[ChainType]bool{
	ChainTypeEthereum: true,
	ChainTypeBSC:      true,
	ChainTypePolygon:  true,
	ChainTypeSolana:   true,
	ChainTypeTron:     true,
	ChainTypeGenex:    true,
}

// NewChainType creates a ChainType after validation.
// The input is normalised to lowercase.
func NewChainType(raw string) (ChainType, error) {
	ct := ChainType(strings.ToLower(strings.TrimSpace(raw)))
	if ct == "" {
		return "", fmt.Errorf("chain type must not be empty")
	}
	if !supportedChains[ct] {
		return "", fmt.Errorf("unsupported chain type: %s", ct)
	}
	return ct, nil
}

// String returns the string representation.
func (c ChainType) String() string {
	return string(c)
}

// IsValid reports whether the ChainType is one of the supported chains.
func (c ChainType) IsValid() bool {
	return supportedChains[c]
}
