/**
 * Bridge config: CCIP (WETH9) and Trustless bridge addresses, chain selectors, ABIs.
 * Chain 138 addresses from env or defaults; mainnet Trustless from env when deployed.
 */

// ---------------------------------------------------------------------------
// CCIP WETH9 bridge (Chain 138)
// ---------------------------------------------------------------------------

export const CONTRACTS = {
  WETH9: (import.meta.env.VITE_WETH9_CHAIN138 || '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2') as `0x${string}`,
  WETH9_BRIDGE: (import.meta.env.VITE_CCIPWETH9_BRIDGE_CHAIN138 || '0xcacfd227A040002e49e2e01626363071324f820a') as `0x${string}`,
  LINK_TOKEN: (import.meta.env.VITE_LINK_TOKEN_CHAIN138 || '0xb7721dD53A8c629d9f1Ba31a5819AFe250002b03') as `0x${string}`,
} as const;

export const CHAIN_SELECTORS = {
  ETHEREUM_MAINNET: '5009297550715157269',
} as const;

export interface CCIPDestination {
  selector: string;
  name: string;
  chainId: number;
}

export const CCIP_DESTINATIONS: CCIPDestination[] = [
  { selector: CHAIN_SELECTORS.ETHEREUM_MAINNET, name: 'Ethereum Mainnet', chainId: 1 },
];

export const BRIDGE_QUOTE_URL = import.meta.env.VITE_BRIDGE_QUOTE_URL || '';

// ---------------------------------------------------------------------------
// Trustless bridge (Lockbox 138 ↔ Mainnet)
// ---------------------------------------------------------------------------

const zero = '0x0000000000000000000000000000000000000000' as const;

export const TRUSTLESS = {
  mainnet: {
    INBOX_ETH: (import.meta.env.VITE_INBOX_ETH_MAINNET || zero) as `0x${string}`,
    LIQUIDITY_POOL: (import.meta.env.VITE_LIQUIDITY_POOL_MAINNET || zero) as `0x${string}`,
    BRIDGE_SWAP_COORDINATOR: (import.meta.env.VITE_BRIDGE_SWAP_COORDINATOR_MAINNET || zero) as `0x${string}`,
    DUAL_ROUTER_BRIDGE_SWAP_COORDINATOR: (import.meta.env.VITE_DUAL_ROUTER_BRIDGE_SWAP_COORDINATOR_MAINNET || zero) as `0x${string}`,
    CHALLENGE_MANAGER: (import.meta.env.VITE_CHALLENGE_MANAGER_MAINNET || zero) as `0x${string}`,
    USDT: (import.meta.env.VITE_USDT_MAINNET || '0xdAC17F958D2ee523a2206206994597C13D831ec7') as `0x${string}`,
    USDC: (import.meta.env.VITE_USDC_MAINNET || '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48') as `0x${string}`,
    DAI: (import.meta.env.VITE_DAI_MAINNET || '0x6B175474E89094C44Da98b954Eedeac495271d0F') as `0x${string}`,
  },
  chain138: {
    LOCKBOX_138: (import.meta.env.VITE_LOCKBOX_138 || '0xFce6f50B312B3D936Ea9693C5C9531CF92a3324c') as `0x${string}`,
    WETH: (import.meta.env.VITE_WETH_CHAIN138 || '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2') as `0x${string}`,
    CUSDT: (import.meta.env.VITE_CUSDT_CHAIN138 || zero) as `0x${string}`,
    CUSDC: (import.meta.env.VITE_CUSDC_CHAIN138 || zero) as `0x${string}`,
  },
  /** Testnet 2138 — set addresses after deploy; used when VITE_TRUSTLESS_L2_CHAIN_ID=2138 */
  chain2138: {
    LOCKBOX_2138: (import.meta.env.VITE_LOCKBOX_2138 || zero) as `0x${string}`,
    WETH: (import.meta.env.VITE_WETH_CHAIN2138 || zero) as `0x${string}`,
    CUSDT: (import.meta.env.VITE_CUSDT_CHAIN2138 || zero) as `0x${string}`,
    CUSDC: (import.meta.env.VITE_CUSDC_CHAIN2138 || zero) as `0x${string}`,
  },
} as const;

// ---------------------------------------------------------------------------
// ABIs (minimal for used functions)
// ---------------------------------------------------------------------------

export const ERC20_ABI = [
  { inputs: [{ name: 'owner', type: 'address' }, { name: 'spender', type: 'address' }], name: 'allowance', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'account', type: 'address' }], name: 'balanceOf', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'spender', type: 'address' }, { name: 'amount', type: 'uint256' }], name: 'approve', outputs: [{ type: 'bool' }], stateMutability: 'nonpayable', type: 'function' },
  { inputs: [{ name: 'to', type: 'address' }, { name: 'amount', type: 'uint256' }], name: 'transfer', outputs: [{ type: 'bool' }], stateMutability: 'nonpayable', type: 'function' },
] as const;

export const WETH9_ABI = [
  ...ERC20_ABI,
  { inputs: [], name: 'deposit', outputs: [], stateMutability: 'payable', type: 'function' },
  { inputs: [{ name: 'wad', type: 'uint256' }], name: 'withdraw', outputs: [], stateMutability: 'nonpayable', type: 'function' },
] as const;

export const BRIDGE_ABI = [
  { inputs: [{ name: 'destinationChainSelector', type: 'uint64' }, { name: 'amount', type: 'uint256' }], name: 'calculateFee', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'destinationChainSelector', type: 'uint64' }, { name: 'recipient', type: 'address' }, { name: 'amount', type: 'uint256' }], name: 'sendCrossChain', outputs: [], stateMutability: 'payable', type: 'function' },
] as const;

export const LOCKBOX_138_ABI = [
  { inputs: [{ name: 'account', type: 'address' }], name: 'getNonce', outputs: [{ type: 'uint256' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'recipient', type: 'address' }, { name: 'nonce', type: 'bytes32' }], name: 'depositNative', outputs: [], stateMutability: 'payable', type: 'function' },
  { inputs: [{ name: 'token', type: 'address' }, { name: 'amount', type: 'uint256' }, { name: 'recipient', type: 'address' }, { name: 'nonce', type: 'bytes32' }], name: 'depositERC20', outputs: [], stateMutability: 'nonpayable', type: 'function' },
] as const;

export const DUAL_ROUTER_COORDINATOR_ABI = [
  { inputs: [{ name: 'depositId', type: 'uint256' }], name: 'canSwap', outputs: [{ type: 'bool' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'depositId', type: 'uint256' }, { name: 'recipient', type: 'address' }, { name: 'outputAsset', type: 'uint8' }, { name: 'stablecoin', type: 'address' }, { name: 'amountOutMin', type: 'uint256' }, { name: 'routeData', type: 'bytes' }, { name: 'useEnhancedRouter', type: 'bool' }], name: 'bridgeAndSwap', outputs: [], stateMutability: 'payable', type: 'function' },
] as const;

export const CHALLENGE_MANAGER_ABI = [
  { inputs: [{ name: 'depositId', type: 'uint256' }], name: 'canFinalize', outputs: [{ type: 'bool' }], stateMutability: 'view', type: 'function' },
  { inputs: [{ name: 'depositId', type: 'uint256' }], name: 'finalizeClaim', outputs: [], stateMutability: 'nonpayable', type: 'function' },
] as const;
