/**
 * Example harness — builds a request and sends it to a live compose backend.
 *
 * Usage:
 *   COMPOSER_BASE_URL=https://composer.li.quest yarn example <name>
 *
 * Pass LIFI_API_KEY to authenticate:
 *   LIFI_API_KEY=your-key COMPOSER_BASE_URL=https://composer.li.quest yarn example <name>
 *
 * Available examples:
 *   aave-borrow       — supply USDC to Aave v3, borrow against it with a health-factor guard
 *   aave-hf-explode   — read the Aave v3 health factor and assert it stays >= 1.0
 *   aave-repay        — repay an Aave v3 WETH debt by supplying WETH
 *   aave-repay-atoken — repay an Aave v3 USDC debt by burning proxy aTokens
 *   aave-emode        — switch the proxy's Aave v3 eMode category
 *   approve-deposit   — approve vault allowance then deposit USDC
 *   consolidate       — consolidate ERC-20 balances to USDC
 *   consolidate-eth   — consolidate ERC-20 balances to native ETH
 *   continuation-settle        — swap USDC to WETH on Base, escrow it for a deferred settlement
 *   continuation-bridge-settle — bridge Arbitrum USDC into a Base settlement escrow
 *   continuation-two-outcomes  — escrow Base USDC for a settlement that delivers DAI and aBasUSDC
 *   continuation-buy-with-change — swap WETH to USDC on Base, escrow it for a settlement that buys exact EURC for a payee and returns the USDC change
 *   continuation-exact-invoice      — escrow USDC on Base for an exact leg that pays a Request Network invoice
 *   continuation-exact-swap-invoice — swap WETH to USDC on Base, escrow it for the same exact invoice leg
 *   continuation-exact-intent-invoice — swap Arbitrum ETH to USDC, order exactly the invoice total on Base for the same exact leg, with no outcome
 *   deposit-proxy     — deposit tokens already on the proxy into Aave
 *   dust-sweep        — split USDC 80/20 and sweep leftover dust
 *   pay-invoice-min-amount — swap WETH to USDC on Base and pay an invoice, requiring its total with a minAmount guard
 *   swap              — swap WETH to USDC via LI.FI
 *   split-zap         — split USDC 60/40 and zap into Aave + Morpho vaults
 *   split-arithmetic  — split USDC 70/30 with arithmetic assertions
 *   zap               — zap USDC into Aave lending position
 *   zap-async         — zap USDC into an async Aave position (future output)
 *   morpho-borrow     — supply WETH collateral to Morpho Blue and borrow USDC
 *   morpho-repay      — open, repay and close a Morpho Blue WETH/USDC position
 *   fly-swap          — exit a Pendle PT position and swap ALL proceeds to WETH
 *   swap-zap          — swap WETH to USDC then zap into Aave
 *   swap-check        — swap WETH to USDC with balance check
 *   swap-recipient    — swap WETH + DAI to USDC and send to recipient
 *   swap-validate     — swap WETH to USDC with output validation bounds
 *   swap-allow-revert — swap WETH to USDC with allow-revert policy
 *   raw-call          — query a contract with raw calldata + arithmetic
 *   read-state        — read on-chain state via peek, staticCall, balanceOf
 *   redeem            — redeem ERC-4626 vault shares via core.call
 *   wrap-eth          — wrap native ETH into WETH via ValueCall
 *   transfer          — transfer full token balance to a recipient
 *   partial-transfer  — split off a fixed amount with core.splitAt, transfer it, swap the rest
 *   untyped-ref       — mix untypedOp with typed handles via raw.ref
 *   uniswap-v2-add    — split USDC, swap half to WETH, add both to the Uniswap V2 pair
 *   uniswap-v3-mint   — open a full-range Uniswap V3 USDC/WETH position
 *   route-exact       — wrap 0.1 native ETH into WETH via /compose/route
 *   route-all         — unwrap the whole WETH balance to ETH via /compose/route
 *   supported-chains  — list the chains this deployment supports, and guard on them
 *
 * Experimental examples use experimental ops and materialisers (see the
 * package README, "Experimental definitions"); a backend that does not enable
 * them rejects the request with `validation_error`:
 *   debt-rebalance    — switch an Aave v3 debt from USDC to EURC with one flashloan and an exact-output swap
 *   debt-migration    — migrate an Aave v3 WETH/USDC position to Morpho Blue with two flashloans
 *   debt-migration-swap — migrate an Aave v3 USDC debt into Morpho Blue WETH/EURC, swapping the EURC borrow to USDC
 *   debt-migration-swap-exact-out — the same migration with an exact-output swap
 *   flashloan-repay   — borrow USDC in a flashloan and repay principal and fee from two handles
 */
import type {
  ComposeCompileRequest,
  ComposeRouteRequest,
} from '@lifi/compose-spec';

import { bigintReplacer } from '../client.js';
import { type ComposeSdk, createComposeSdk } from '../sdk.js';

import { buildAaveBorrow } from './aaveBorrow.js';
import { buildAaveHealthFactorExplode } from './aaveHealthFactorExplode.js';
import { buildAaveRepay } from './aaveRepay.js';
import { buildAaveRepayWithATokens } from './aaveRepayWithATokens.js';
import { buildAaveSetEMode } from './aaveSetEMode.js';
import { buildApproveAndDeposit } from './approveAndDeposit.js';
import { buildRedeemFromVault, buildWrapEth } from './callContract.js';
import { API_KEY, BASE_URL, OWNER, PROXY, RECIPIENT } from './config.js';
import { buildConsolidateToEth } from './consolidateToEth.js';
import { buildConsolidateStablesToUsdc } from './consolidateToUsdc.js';
import { buildContinuationBridgeSettleExample } from './continuationBridgeSettle.js';
import { buildContinuationBuyWithChangeExample } from './continuationBuyWithChange.js';
import {
  buildContinuationExactIntentInvoiceExample,
  buildContinuationExactInvoiceExample,
  buildContinuationExactSwapInvoiceExample,
  SAMPLE_INVOICE,
} from './continuationExactInvoice.js';
import { buildContinuationSettleExample } from './continuationSettle.js';
import { buildContinuationTwoOutcomesExample } from './continuationTwoOutcomes.js';
import { buildDepositFromProxy } from './depositFromProxy.js';
import { buildDustSweepExample } from './dustSweep.js';
import { buildAaveDebtRebalanceExactOut } from './experimental/aaveDebtRebalanceExactOut.js';
import { buildAaveToMorphoDebtMigration } from './experimental/aaveToMorphoDebtMigration.js';
import { buildAaveToMorphoDebtMigrationWithSwap } from './experimental/aaveToMorphoDebtMigrationWithSwap.js';
import { buildAaveToMorphoDebtMigrationWithSwapExactOut } from './experimental/aaveToMorphoDebtMigrationWithSwapExactOut.js';
import { buildTrivialFlashloanRepay } from './experimental/flashloanRepay.js';
import { buildFlySwapExample } from './flySwap.js';
import { buildLifiSwapExample } from './lifiSwap.js';
import { buildLifiZapExample } from './lifiZap.js';
import { buildLifiZapAsyncExample } from './lifiZapAsync.js';
import { buildMorphoBlueBorrow } from './morphoBlueBorrow.js';
import { buildMorphoBlueRepay } from './morphoBlueRepay.js';
import { buildPayInvoiceWithMinAmount } from './payInvoiceWithMinAmount.js';
import { buildRawCallWithArithmetic } from './rawCallWithArithmetic.js';
import { buildReadContractState } from './readContractState.js';
import {
  buildRouteAllExample,
  buildRouteExactExample,
} from './routeTokenPair.js';
import { buildSplitAndZapExample } from './splitAndZap.js';
import { buildSplitWithArithmetic } from './splitWithArithmetic.js';
import { runSupportedChainsExample } from './supportedChains.js';
import { buildSwapAndZapExample } from './swapAndZap.js';
import { buildSwapToRecipient } from './swapToRecipient.js';
import { buildSwapWithAllowRevertExample } from './swapWithAllowRevert.js';
import { buildSwapWithBalanceCheck } from './swapWithBalanceCheck.js';
import { buildSwapWithOutputValidation } from './swapWithOutputValidation.js';
import { buildPartialTransfer, buildTransferTokens } from './transferTokens.js';
import { buildUniswapV2AddLiquidity } from './uniswapV2AddLiquidity.js';
import { buildUniswapV3Mint } from './uniswapV3Mint.js';
import { buildUntypedOpWithTypedRef } from './untypedOpWithTypedRef.js';

const stringifyJson = (value: unknown) =>
  JSON.stringify(value, bigintReplacer, 2);

const EXAMPLES: Record<string, () => ComposeCompileRequest> = {
  'aave-borrow': () =>
    buildAaveBorrow({
      owner: OWNER,
      recipient: RECIPIENT,
      collateralAmount: '1000000000',
      borrowAmount: '500000000',
    }).request,
  'aave-hf-explode': () =>
    buildAaveHealthFactorExplode({ signer: OWNER }).request,
  'aave-repay': () =>
    buildAaveRepay({
      owner: OWNER,
      collateralAmount: '1000000000',
      borrowAmount: '100000000000000000',
      repayAmount: '101000000000000000',
    }).request,
  'aave-repay-atoken': () =>
    buildAaveRepayWithATokens({
      owner: OWNER,
      collateralAmount: '1000000000',
      borrowAmount: '500000000',
    }).request,
  'aave-emode': () => buildAaveSetEMode({ owner: OWNER }).request,
  'approve-deposit': () =>
    buildApproveAndDeposit({ owner: OWNER, amount: '1000000000' }).request,
  consolidate: () =>
    buildConsolidateStablesToUsdc({
      owner: OWNER,
      usdtAmount: '1000000000',
      daiAmount: '1000000000000000000000',
      fraxAmount: '1000000000000000000000',
      lusdAmount: '1000000000000000000000',
    }).request,
  'consolidate-eth': () =>
    buildConsolidateToEth({
      owner: OWNER,
      wethAmount: '1000000000000000000',
      usdcAmount: '1000000000',
      usdtAmount: '1000000000',
      daiAmount: '1000000000000000000000',
    }).request,
  'continuation-settle': () =>
    buildContinuationSettleExample({ owner: OWNER }).request,
  'continuation-bridge-settle': () =>
    buildContinuationBridgeSettleExample({ owner: OWNER }).request,
  'continuation-two-outcomes': () =>
    buildContinuationTwoOutcomesExample({ owner: OWNER }).request,
  'continuation-buy-with-change': () =>
    buildContinuationBuyWithChangeExample({
      owner: OWNER,
      payee: RECIPIENT,
      wethAmount: '300000000000000000',
      eurcAmount: '300000000',
      minChangeAmount: '150000000',
    }).request,
  'continuation-exact-invoice': () =>
    buildContinuationExactInvoiceExample({
      owner: OWNER,
      invoice: SAMPLE_INVOICE,
      usdcAmount: '1050000000',
    }).request,
  'continuation-exact-swap-invoice': () =>
    buildContinuationExactSwapInvoiceExample({
      owner: OWNER,
      invoice: SAMPLE_INVOICE,
      wethAmount: '1000000000000000000',
    }).request,
  'continuation-exact-intent-invoice': () =>
    buildContinuationExactIntentInvoiceExample({
      owner: OWNER,
      invoice: SAMPLE_INVOICE,
      ethAmount: '500000000000000000',
    }).request,
  'deposit-proxy': () =>
    buildDepositFromProxy({
      owner: OWNER,
      proxyAddress: PROXY,
      expectedAmount: '1000000000',
    }).request,
  'dust-sweep': () => buildDustSweepExample().request,
  swap: () => buildLifiSwapExample().request,
  'split-zap': () => buildSplitAndZapExample().request,
  'split-arithmetic': () =>
    buildSplitWithArithmetic({ owner: OWNER, amount: '1000000000' }).request,
  zap: () => buildLifiZapExample().request,
  'zap-async': () =>
    buildLifiZapAsyncExample({ owner: OWNER, recipient: RECIPIENT }).request,
  'fly-swap': () => buildFlySwapExample().request,
  'morpho-borrow': () =>
    buildMorphoBlueBorrow({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      borrowAmount: '1200000000',
    }).request,
  'morpho-repay': () =>
    buildMorphoBlueRepay({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      borrowAmount: '500000000',
      repayAmount: '501000000',
    }).request,
  'pay-invoice-min-amount': () =>
    buildPayInvoiceWithMinAmount({
      owner: OWNER,
      invoice: SAMPLE_INVOICE,
      wethAmount: '600000000000000000',
    }).request,
  'swap-zap': () => buildSwapAndZapExample().request,
  'swap-check': () =>
    buildSwapWithBalanceCheck({ owner: OWNER, amount: '1000000000000000000' })
      .request,
  'swap-recipient': () =>
    buildSwapToRecipient({
      owner: OWNER,
      recipient: RECIPIENT,
      wethAmount: '1000000000000000000',
      daiAmount: '500000000000000000000',
    }).request,
  'swap-validate': () =>
    buildSwapWithOutputValidation({
      owner: OWNER,
      amount: '1000000000000000000',
      expectedOut: '3000000000',
    }).request,
  'swap-allow-revert': () => buildSwapWithAllowRevertExample().request,
  'raw-call': () => buildRawCallWithArithmetic({ owner: OWNER }).request,
  'read-state': () => buildReadContractState({ owner: OWNER }).request,
  redeem: () =>
    buildRedeemFromVault({ owner: OWNER, amount: '1000000000000000000' })
      .request,
  'wrap-eth': () =>
    buildWrapEth({ owner: OWNER, amount: '1000000000000000000' }).request,
  transfer: () =>
    buildTransferTokens({
      owner: OWNER,
      recipient: RECIPIENT,
      amount: '1000000000',
    }).request,
  'partial-transfer': () =>
    buildPartialTransfer({
      owner: OWNER,
      recipient: RECIPIENT,
      amount: '1000000000',
    }).request,
  'untyped-ref': () => buildUntypedOpWithTypedRef({ owner: OWNER }).request,
  'uniswap-v2-add': () =>
    buildUniswapV2AddLiquidity({ owner: OWNER, usdcAmount: '1000000000' })
      .request,
  'uniswap-v3-mint': () =>
    buildUniswapV3Mint({
      owner: OWNER,
      usdcAmount: '1000000000',
      wethAmount: '400000000000000000',
    }).request,
  'debt-rebalance': () =>
    buildAaveDebtRebalanceExactOut({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      debtAmount: '1000000000',
      newDebtBorrowAmount: '965000000',
    }).request,
  'debt-migration': () =>
    buildAaveToMorphoDebtMigration({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      debtAmount: '1000000000',
    }).request,
  'debt-migration-swap': () =>
    buildAaveToMorphoDebtMigrationWithSwap({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      debtAmount: '1000000000',
      morphoBorrowAmount: '1100000000',
    }).request,
  'debt-migration-swap-exact-out': () =>
    buildAaveToMorphoDebtMigrationWithSwapExactOut({
      owner: OWNER,
      collateralAmount: '1000000000000000000',
      debtAmount: '1000000000',
      morphoBorrowAmount: '975000000',
    }).request,
  'flashloan-repay': () =>
    buildTrivialFlashloanRepay({
      owner: OWNER,
      amount: '10000000000',
    }).request,
};

// `/compose/route` examples build a `ComposeRouteRequest` and go to
// `sdk.route`, not `sdk.client.compile`, so they live in their own registry.
const ROUTE_EXAMPLES: Record<string, () => ComposeRouteRequest> = {
  'route-exact': buildRouteExactExample,
  'route-all': buildRouteAllExample,
};

// Query examples read from the API instead of building a request to compile,
// so they take the sdk and print their own output.
const QUERY_EXAMPLES: Record<string, (sdk: ComposeSdk) => Promise<void>> = {
  'supported-chains': runSupportedChainsExample,
};

const run = async () => {
  const name = process.argv[2];

  const queryExample = name ? QUERY_EXAMPLES[name] : undefined;
  const routeBuilder = name ? ROUTE_EXAMPLES[name] : undefined;
  const builder = name ? EXAMPLES[name] : undefined;

  if (!builder && !routeBuilder && !queryExample) {
    const valid = [
      ...Object.keys(EXAMPLES),
      ...Object.keys(ROUTE_EXAMPLES),
      ...Object.keys(QUERY_EXAMPLES),
    ]
      .sort()
      .join(', ');
    console.error(
      `Error: ${
        name ? `unknown example "${name}"` : 'no example specified'
      }\n` +
        `Valid examples: ${valid}\n` +
        `Usage: COMPOSER_BASE_URL=https://composer.li.quest yarn example <name>`,
    );
    process.exit(1);
  }
  console.log(`Running example: ${name}`);

  const sdk = createComposeSdk({ baseUrl: BASE_URL, apiKey: API_KEY });

  // Query examples do their own I/O and printing - there is no request to
  // show and nothing to compile.
  if (queryExample) {
    console.log(`\n--- Querying ${BASE_URL} ---`);
    await queryExample(sdk);
    return;
  }

  const request = routeBuilder ? routeBuilder() : builder!();

  console.log('--- Request ---');
  console.log(stringifyJson(request));

  console.log(`\n--- Compiling against ${BASE_URL} ---`);
  const result = routeBuilder
    ? await sdk.route(request as ComposeRouteRequest)
    : await sdk.client.compile(request as ComposeCompileRequest);

  console.log('\n--- Result ---');
  console.log(stringifyJson(result));
};

run().catch((err: unknown) => {
  console.error(err);
  process.exit(1);
});
