// wci26/live-abis.jsx - minimal ABI fragments needed by the dashboard/indexer seam.

const WCI_COUNTRY_DATA_OUTPUT = {
  type: 'tuple',
  components: [
    { name: 'token', type: 'address' },
    { name: 'lpTokenId', type: 'uint256' },
    { name: 'totalFeesWETH', type: 'uint256' },
    { name: 'totalVolumeWETH', type: 'uint256' },
    { name: 'active', type: 'bool' },
  ],
};

const eventInput = (name, type, indexed = false) => ({ name, type, indexed });
const fnInput = (name, type) => ({ name, type });
const fnOutput = (name, type) => ({ name, type });
const viewFn = (name, inputs, outputs) => ({ type: 'function', stateMutability: 'view', name, inputs, outputs });
const eventAbi = (name, inputs) => ({ type: 'event', name, inputs });

const WCI_FACTORY_ABI = [
  viewFn('getTotalBuybackPressure', [], [fnOutput('total', 'uint256')]),
  viewFn('pendingWETH', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint128')]),
  viewFn('pendingWETHForEliminated', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint128')]),
  viewFn('getCountry', [fnInput('iso', 'bytes2')], [WCI_COUNTRY_DATA_OUTPUT]),
  viewFn('getRemainingAllocations', [fnInput('isoCode', 'bytes2')], [
    fnOutput('reserveLeft', 'uint256'),
    fnOutput('airdropLeft', 'uint256'),
    fnOutput('treasuryLeft', 'uint256'),
    fnOutput('rewardsLeft', 'uint256'),
  ]),
  viewFn('getMarketCap', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint256')]),
  viewFn('getLeaderboard', [], [{ name: 'sorted', type: 'tuple[]', components: WCI_COUNTRY_DATA_OUTPUT.components }]),
  viewFn('getLeaderboardByFees', [], [{ name: 'sorted', type: 'tuple[]', components: WCI_COUNTRY_DATA_OUTPUT.components }]),
  viewFn('countryCount', [], [fnOutput('', 'uint256')]),
  viewFn('initialized', [], [fnOutput('', 'bool')]),
  viewFn('countryTokenImpl', [], [fnOutput('', 'address')]),
  viewFn('buybackExecutor', [], [fnOutput('', 'address')]),
  viewFn('WETH', [], [fnOutput('', 'address')]),
  viewFn('POSITION_MANAGER', [], [fnOutput('', 'address')]),
  viewFn('FEE_TIER', [], [fnOutput('', 'uint24')]),
  viewFn('weeklyChampion', [], [fnOutput('', 'bytes2')]),
  viewFn('lastChampionCrownedAt', [], [fnOutput('', 'uint256')]),
  viewFn('distributedReserve', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint256')]),
  viewFn('distributedAirdrop', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint256')]),
  viewFn('distributedTreasury', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint256')]),
  viewFn('distributedRewards', [fnInput('isoCode', 'bytes2')], [fnOutput('', 'uint256')]),
  { type: 'function', stateMutability: 'nonpayable', name: 'maybeCollectAndBuyback', inputs: [fnInput('isoCode', 'bytes2')], outputs: [] },
  { type: 'function', stateMutability: 'nonpayable', name: 'collectAllAndBuyback', inputs: [], outputs: [] },
  { type: 'function', stateMutability: 'nonpayable', name: 'sweepFactoryWETH', inputs: [], outputs: [] },
  eventAbi('TokenDeployed', [eventInput('iso', 'bytes2', true), eventInput('token', 'address')]),
  eventAbi('PoolCreated', [eventInput('iso', 'bytes2', true), eventInput('lpTokenId', 'uint256'), eventInput('pool', 'address')]),
  eventAbi('BuybackReady', [eventInput('iso', 'bytes2', true), eventInput('wethOwed', 'uint128')]),
  eventAbi('EliminatedFeesReady', [eventInput('iso', 'bytes2', true), eventInput('wethOwed', 'uint128')]),
  eventAbi('FeesCollected', [eventInput('iso', 'bytes2', true), eventInput('wethAmount', 'uint256')]),
  eventAbi('VolumeRecorded', [
    eventInput('iso', 'bytes2', true),
    eventInput('wethVolume', 'uint256'),
    eventInput('totalVolumeWETH', 'uint256'),
  ]),
  eventAbi('CountryTokenFeesBurned', [eventInput('iso', 'bytes2', true), eventInput('amount', 'uint256')]),
  eventAbi('BuybackTriggered', [eventInput('wethAmount', 'uint256')]),
  eventAbi('BuybackFailed', [eventInput('wethAmount', 'uint256')]),
  eventAbi('BuybackDeferred', [eventInput('wethAmount', 'uint256')]),
  eventAbi('CountryEliminated', [eventInput('iso', 'bytes2', true)]),
  eventAbi('CountryReinstated', [eventInput('iso', 'bytes2', true)]),
  eventAbi('ReserveDistributed', [
    eventInput('iso', 'bytes2', true),
    eventInput('to', 'address', true),
    eventInput('amount', 'uint256'),
    eventInput('totalDistributed', 'uint256'),
  ]),
  eventAbi('AirdropDistributed', [
    eventInput('iso', 'bytes2', true),
    eventInput('to', 'address', true),
    eventInput('amount', 'uint256'),
    eventInput('totalDistributed', 'uint256'),
  ]),
  eventAbi('TreasuryDistributed', [
    eventInput('iso', 'bytes2', true),
    eventInput('to', 'address', true),
    eventInput('amount', 'uint256'),
    eventInput('totalDistributed', 'uint256'),
  ]),
  eventAbi('RewardsDistributed', [
    eventInput('iso', 'bytes2', true),
    eventInput('to', 'address', true),
    eventInput('amount', 'uint256'),
    eventInput('totalDistributed', 'uint256'),
  ]),
  eventAbi('GroupStageResult', [
    eventInput('matchId', 'uint256', true),
    eventInput('winner', 'bytes2', true),
    eventInput('loser', 'bytes2', true),
    eventInput('timestamp', 'uint256'),
  ]),
  eventAbi('WeeklyChampionCrowned', [
    eventInput('iso', 'bytes2', true),
    eventInput('token', 'address'),
    eventInput('totalFeesWETH', 'uint256'),
    eventInput('timestamp', 'uint256'),
  ]),
];

const WCI_BUYBACK_ABI = [
  viewFn('pendingBuyback', [], [fnOutput('', 'uint256')]),
  viewFn('totalETHBought', [], [fnOutput('', 'uint256')]),
  viewFn('buybackCount', [], [fnOutput('', 'uint256')]),
  viewFn('disabledAt', [], [fnOutput('', 'uint256')]),
  viewFn('v2WethPair', [], [fnOutput('', 'address')]),
  viewFn('authorizedSender', [], [fnOutput('', 'address')]),
  viewFn('WCI26', [], [fnOutput('', 'address')]),
  viewFn('WETH', [], [fnOutput('', 'address')]),
  viewFn('V2_ROUTER', [], [fnOutput('', 'address')]),
  viewFn('wci26FeeBps', [], [fnOutput('', 'uint256')]),
  { type: 'function', stateMutability: 'nonpayable', name: 'executeBuyback', inputs: [], outputs: [] },
  eventAbi('BuybackExecuted', [eventInput('ethSwapped', 'uint256'), eventInput('remaining', 'uint256')]),
  eventAbi('BuybackDeferred', [eventInput('ethAmount', 'uint256')]),
  eventAbi('BuybackDisabled', [eventInput('disabledAt', 'uint256')]),
  eventAbi('V2PairSet', [eventInput('pair', 'address')]),
];

const WCI_COUNTRY_TOKEN_ABI = [
  viewFn('name', [], [fnOutput('', 'string')]),
  viewFn('symbol', [], [fnOutput('', 'string')]),
  viewFn('decimals', [], [fnOutput('', 'uint8')]),
  viewFn('totalSupply', [], [fnOutput('', 'uint256')]),
  viewFn('factory', [], [fnOutput('', 'address')]),
  viewFn('getPool', [], [fnOutput('', 'address')]),
  viewFn('tradingActive', [], [fnOutput('', 'bool')]),
  viewFn('limitsActive', [], [fnOutput('', 'bool')]),
  viewFn('maxWallet', [], [fnOutput('', 'uint256')]),
  viewFn('isoCode', [], [fnOutput('', 'bytes2')]),
  viewFn('balanceOf', [fnInput('account', 'address')], [fnOutput('', 'uint256')]),
  eventAbi('Initialized', [
    eventInput('factory', 'address', true),
    eventInput('isoCode', 'bytes2', true),
    eventInput('maxWallet', 'uint256'),
  ]),
  eventAbi('PoolCached', [eventInput('pool', 'address', true), eventInput('launchBlock', 'uint256')]),
  eventAbi('Transfer', [
    eventInput('from', 'address', true),
    eventInput('to', 'address', true),
    eventInput('value', 'uint256'),
  ]),
];

const WCI_UNISWAP_V3_POOL_ABI = [
  viewFn('token0', [], [fnOutput('', 'address')]),
  viewFn('token1', [], [fnOutput('', 'address')]),
  viewFn('fee', [], [fnOutput('', 'uint24')]),
  viewFn('liquidity', [], [fnOutput('', 'uint128')]),
  viewFn('slot0', [], [
    fnOutput('sqrtPriceX96', 'uint160'),
    fnOutput('tick', 'int24'),
    fnOutput('observationIndex', 'uint16'),
    fnOutput('observationCardinality', 'uint16'),
    fnOutput('observationCardinalityNext', 'uint16'),
    fnOutput('feeProtocol', 'uint8'),
    fnOutput('unlocked', 'bool'),
  ]),
  eventAbi('Swap', [
    eventInput('sender', 'address', true),
    eventInput('recipient', 'address', true),
    eventInput('amount0', 'int256'),
    eventInput('amount1', 'int256'),
    eventInput('sqrtPriceX96', 'uint160'),
    eventInput('liquidity', 'uint128'),
    eventInput('tick', 'int24'),
  ]),
  eventAbi('Mint', [
    eventInput('sender', 'address'),
    eventInput('owner', 'address', true),
    eventInput('tickLower', 'int24', true),
    eventInput('tickUpper', 'int24', true),
    eventInput('amount', 'uint128'),
    eventInput('amount0', 'uint256'),
    eventInput('amount1', 'uint256'),
  ]),
  eventAbi('Burn', [
    eventInput('owner', 'address', true),
    eventInput('tickLower', 'int24', true),
    eventInput('tickUpper', 'int24', true),
    eventInput('amount', 'uint128'),
    eventInput('amount0', 'uint256'),
    eventInput('amount1', 'uint256'),
  ]),
  eventAbi('Collect', [
    eventInput('owner', 'address', true),
    eventInput('recipient', 'address'),
    eventInput('tickLower', 'int24', true),
    eventInput('tickUpper', 'int24', true),
    eventInput('amount0', 'uint128'),
    eventInput('amount1', 'uint128'),
  ]),
];

const WCI_LIVE_ABIS = {
  factory: WCI_FACTORY_ABI,
  buyback: WCI_BUYBACK_ABI,
  countryToken: WCI_COUNTRY_TOKEN_ABI,
  uniswapV3Pool: WCI_UNISWAP_V3_POOL_ABI,
};

Object.assign(window, {
  WCI_LIVE_ABIS,
});
