// ─── Chain identifiers ───────────────────────────────────────────────────────

export type Chain = 'ETH' | 'BSC' | 'BTC' | 'TRX' | 'SOL'
export type FeeTier = 'slow' | 'normal' | 'fast'

// ─── Per-chain token lists (excluding native) ────────────────────────────────

export type EthToken = 'USDT' | 'USDC' | 'DAI' | 'WBTC' | 'LINK' | 'UNI'
export type BscToken = 'USDT' | 'USDC' | 'BUSD' | 'WBNB' | 'DOGE'
export type TrxToken = 'USDT' | 'USDC'
// SOL has 14 SPL tokens in the registry
export type SolToken =
  | 'USDC' | 'USDT' | 'RAY' | 'BONK' | 'JTO' | 'PYTH'
  | 'WIF' | 'JUP' | 'ORCA' | 'MNGO' | 'MSOL' | 'STSOL'
  | 'SAMO' | 'ATLAS'

export const CHAIN_TOKENS = {
  ETH: ['USDT', 'USDC', 'DAI', 'WBTC', 'LINK', 'UNI'] as const satisfies readonly EthToken[],
  BSC: ['USDT', 'USDC', 'BUSD', 'WBNB', 'DOGE'] as const satisfies readonly BscToken[],
  BTC: [] as const,
  TRX: ['USDT', 'USDC'] as const satisfies readonly TrxToken[],
  SOL: ['USDC', 'USDT', 'RAY', 'BONK', 'JTO', 'PYTH', 'WIF', 'JUP', 'ORCA', 'MNGO', 'MSOL', 'STSOL', 'SAMO', 'ATLAS'] as const satisfies readonly SolToken[],
} satisfies Record<Chain, readonly string[]>

// ─── Per-chain UI / behaviour config ────────────────────────────────────────

export interface ChainConfig {
  label: string
  nativeSymbol: string
  /** Whether the chain supports token-level sends (ERC20 / BEP20 / TRC20 / SPL) */
  hasToken: boolean
  /** Whether the API accepts a feeTier parameter */
  hasFeeTier: boolean
  /** Placeholder for the recipient address input */
  addressPlaceholder: string
  /** Smallest amount unit name (for tooltip / docs) */
  unit: string
  /** Accent color used for the chain dot in dropdowns */
  color: string
  /** Decimal places for the native coin (used to convert human input → base units) */
  nativeDecimals: number
}

export const CHAIN_CONFIG: Record<Chain, ChainConfig> = {
  ETH: {
    label: 'Ethereum',
    nativeSymbol: 'ETH',
    hasToken: true,
    hasFeeTier: true,
    addressPlaceholder: '0x…',
    unit: 'wei',
    color: '#627EEA',
    nativeDecimals: 18,
  },
  BSC: {
    label: 'BSC Chain',
    nativeSymbol: 'BSC',
    hasToken: true,
    hasFeeTier: false,
    addressPlaceholder: '0x…',
    unit: 'wei',
    color: '#F3BA2F',
    nativeDecimals: 18,
  },
  BTC: {
    label: 'Bitcoin',
    nativeSymbol: 'BTC',
    hasToken: false,
    hasFeeTier: true,
    addressPlaceholder: 'bc1q…',
    unit: 'satoshi',
    color: '#F7931A',
    nativeDecimals: 8,
  },
  TRX: {
    label: 'Tron',
    nativeSymbol: 'TRX',
    hasToken: true,
    hasFeeTier: false,
    addressPlaceholder: 'T…',
    unit: 'sun',
    color: '#FF060A',
    nativeDecimals: 6,
  },
  SOL: {
    label: 'Solana',
    nativeSymbol: 'SOL',
    hasToken: true,
    hasFeeTier: false,
    addressPlaceholder: 'Fg3R…',
    unit: 'lamport',
    color: '#9945FF',
    nativeDecimals: 9,
  },
}

// ─── Wallet ticker → chain mapping (for TokenTable integration) ──────────────
// ARB is an ERC-20 on Ethereum, not a separate chain.

export const TICKER_TO_CHAIN: Record<string, Chain> = {
  BTC: 'BTC',
  ETH: 'ETH',
  SOL: 'SOL',
  TRX: 'TRX',
  BSC: 'BSC',
}

// ─── API request / response types ───────────────────────────────────────────

export interface SendEthRequest {
  to: string
  amount: string           // raw wei / token base units (string to avoid JS precision loss)
  token?: EthToken
  feeTier?: FeeTier
}

export interface SendBscRequest {
  to: string
  amount: string           // raw wei / 18-dec base units (DOGE = 8 dec)
  token?: BscToken
  // ⚠️ USDT on BSC = 18 dec, not 6 like on ETH/TRX. DOGE = 8 dec.
}

export interface SendBtcRequest {
  to: string
  amount: string           // satoshi (8 dec). Min dust = 294 sat. Floor fee = 2 sat/vB.
  feeTier?: FeeTier        // slow = ~144 blocks, normal = ~6, fast = ~1
}

export interface SendTrxRequest {
  to: string
  amount: string           // sun (6 dec for TRX; TRC-20 USDT also 6 dec)
  token?: TrxToken
  // ⚠️ TRC-20 transfer burns ~15–30 TRX Energy when no frozen Energy available.
}

export interface SendSolRequest {
  to: string               // base58 public key
  amount: string           // lamports (9 dec for SOL)
  token?: SolToken
}

// Discriminated union — pick the right one based on `chain`
export type SendRequest =
  | ({ chain: 'ETH' } & SendEthRequest)
  | ({ chain: 'BSC' } & SendBscRequest)
  | ({ chain: 'BTC' } & SendBtcRequest)
  | ({ chain: 'TRX' } & SendTrxRequest)
  | ({ chain: 'SOL' } & SendSolRequest)

export interface SendResponse {
  data: {
    txid: string
    chain: Chain
  }
}
