import {
  type ComposeCompileRequest,
  flashloanMaxFee,
  type Flow,
} from '@lifi/compose-spec';

import {
  createComposeSdk,
  type FlowBuilder,
  type InputSchema,
  materialisers,
  resources,
} from '../../index.js';
import type { Address } from '../../types.js';
import { AAVE_SAME_BLOCK_SKEW_WEI } from '../aaveRounding.js';
import { API_KEY, BASE_URL } from '../config.js';

// Base mainnet token addresses.
const BASE_WETH = '0x4200000000000000000000000000000000000006';
const BASE_USDC = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913';
// Circle EURC on Base (6 decimals).
const BASE_EURC = '0x60a3E35Cc302bFA44Cb288Bc5a4F316Fdb1adb42';

// Token decimals for the Paraswap BUY (raw-address requests require them).
const EURC_DECIMALS = 6;
const USDC_DECIMALS = 6;

// Aave V3 on Base (pool + aWETH receipt + variable-debt USDC).
const AAVE_V3_POOL_BASE = '0xA238Dd80C259a72e81d7e4664a9801593F98d1c5';
const A_BAS_WETH = '0xD4a0e0b9149BCee3C920d2E00b5dE09138fd8bb7';
const VDEBT_BAS_USDC = '0x59dca05b6c26dbd64b5381374aAaC5CD05644C28';

// Morpho Blue WETH/EURC market on Base. Real listed market — borrow APY
// ~1.02% at the time of writing vs ~5.30% on the WETH/USDC market, which is
// what makes this migration economically interesting. Market ID:
// 0xa9b5142fa687a24c275faf731f13b52faa9873252bb4e1cb6077aa1f412edb0b
// (= keccak256(MarketParams) of the values below).
const ADAPTIVE_CURVE_IRM_BASE = '0x46415998764C29aB2a25CbeA6254146D50D22687';
const WETH_EURC_ORACLE = '0xE1bb8E5b4930eC9FeC7f7943FCF6227649F14B37';
const WETH_EURC_LLTV = '860000000000000000';

const MORPHO_WETH_EURC_MARKET = {
  loanToken: BASE_EURC,
  collateralToken: BASE_WETH,
  oracle: WETH_EURC_ORACLE,
  irm: ADAPTIVE_CURVE_IRM_BASE,
  lltv: WETH_EURC_LLTV,
};

// Source-cap slippage on the EURC → USDC BUY, in basis points. This bounds how
// much extra EURC Augustus may pull above the quoted source before refunding.
// `morphoBorrowAmount` must exceed the resulting `maxAmountIn`.
const DEFAULT_MAX_SLIPPAGE_BPS = 100; // 1%

/**
 * Forward the proxy's USDC above the leg's `reserve` to the signer. The live
 * USDC balance holds the loan proceeds, the BUY output and the repay's
 * leftover; the reserve must stay in the proxy for the runtime to repay Aave.
 * The split keeps `min(balance, reserve)` as its head and pays out only the
 * tail.
 *
 * A request-level `sweepTo` cannot do this: `outputs` reports the unbound
 * `borrowed` and `residual` ports in full, but the sweep withholds the
 * reserve, so it moves less than they report and compose rejects the flow.
 */
const payOutUsdcAboveReserve = <T extends InputSchema>(
  builder: FlowBuilder<T>,
  reserve: `${bigint}`,
): void => {
  const usdcBalance = builder.core.balanceOf('read-usdc-balance', {
    bind: {},
    config: { token: BASE_USDC },
  });
  const { tail: usdcProceeds } = builder.core.splitAt(
    'exclude-flashloan-reserve',
    {
      bind: { source: usdcBalance.balance },
      config: { amount: reserve },
    },
  );
  builder.core.transfer('payout-borrow-proceeds', {
    bind: {
      amount: usdcProceeds,
      recipient: builder.context.sender,
    },
    config: {},
  });
};

export interface AaveToMorphoDebtMigrationWithSwapExactOutInput {
  readonly owner: Address;
  /** WETH collateral the user posts (in smallest units, 18 decimals). */
  readonly collateralAmount: `${bigint}`;
  /** USDC principal borrowed from Aave (6 decimals). */
  readonly debtAmount: `${bigint}`;
  /**
   * EURC to borrow from Morpho (6 decimals). Sized as a *generous upper bound*
   * — large enough that the exact-output BUY can always produce
   * `debtAmount + maxFee` USDC even after `maxSlippageBps` of source slippage
   * (i.e. `morphoBorrowAmount > maxAmountIn`). The BUY pulls only what it needs;
   * the unspent EURC is repaid straight back into the freshly-minted Morpho
   * debt, so the *net* migrated debt is the original Aave debt (in EURC terms)
   * plus only the slippage/fee actually consumed — NOT the full upper bound.
   * Note: EUR/USD is not 1:1 — size off a live BUY quote, e.g.
   *   morphoBorrowAmount ≈ ceil((debtAmount + maxFee) * eurcPerUsdc * (1 + slippage))
   */
  readonly morphoBorrowAmount: `${bigint}`;
  /** Optional source-cap slippage on the BUY, in bps (default 100 = 1%). */
  readonly maxSlippageBps?: number;
  /** Optional integrator label for analytics / fee capture. */
  readonly partner?: string;
  /** Optional fee recipient; required to actually receive a partner fee. */
  readonly partnerAddress?: Address;
  /** Optional fixed partner fee in bps (e.g. '50' = 0.5%); needs partnerAddress. */
  readonly partnerFeeBps?: string;
}

/**
 * Atomically migrate an Aave V3 USDC borrow on Base into the Morpho Blue
 * WETH/EURC market — same WETH collateral, different stablecoin debt — using a
 * native **exact-output** (Paraswap/Velora BUY) swap instead of the exact-input
 * `lifi.swap`.
 *
 * Contrast with `aaveToMorphoDebtMigrationWithSwap.ts` (the exact-input
 * variant): there the author must over-size the Morpho borrow so a slippage-
 * adjusted *minimum* output still clears the fixed flashloan need, and the
 * buffer lands on the borrow leg as lasting debt. Here `paraswap.buy` produces
 * *exactly* `debtAmount + maxFee` USDC, pulls only the EURC it needs, and the
 * EURC it did not sell (Augustus's refund plus the headroom above the swap cap)
 * stays on the proxy and is surfaced as `unspentIn` — which we route straight
 * into a capped `morphoBlue.repay` to unwind the over-borrow. Net migrated
 * debt = original debt + only the consumed slippage/fee, not a standing buffer.
 *
 * Phase 1 — provision the source position on Aave V3:
 * - User direct-deposits WETH.
 * - `lifi.zap` supplies the WETH to Aave (WETH → aBasWETH routing edge).
 * - `aave.borrow` mints USDC debt; the borrowed USDC stays on the proxy
 *   until the payout at the end of the flow.
 *
 * Phase 2 — migrate to Morpho using two concurrent flashloans:
 * - Flashloan #1 (Aave V3, USDC) bridges the Aave debt payoff.
 * - Flashloan #2 (Balancer V2, WETH) bridges the Morpho collateral supply.
 * - `aave.repay(mode: 'max')` clears the Aave USDC debt with flashloan #1.
 * - `lifi.zap` withdraws the freed WETH from Aave (aBasWETH → WETH).
 * - `morphoBlue.supplyCollateral` deposits flashloan #2's WETH into Morpho.
 * - `morphoBlue.borrow` mints a *capped* `morphoBorrowAmount` of EURC.
 * - `paraswap.buy` buys *exactly* `debtAmount + maxFee` USDC with the
 *   borrowed EURC. `amountOut` is the USDC leg's reserve (principal plus the
 *   fee cap); `unspentIn` is every borrowed EURC the swap did not sell,
 *   including the headroom above the swap cap.
 * - Two `lifi.flashloanRepay` ops earmark each leg's funds: the USDC leg's from
 *   `buy.amountOut` (`amountOut` is the whole reserve), the WETH leg's from
 *   the Aave withdrawal (Balancer V2 charges no fee). They move nothing: the
 *   funds stay in the proxy and the flashloan runtime repays both lenders
 *   after the program.
 * - `morphoBlue.repay(mode: 'exact')` repays the proxy's live EURC balance (the
 *   same EURC `buy.unspentIn` names) straight back into the freshly-minted
 *   Morpho debt, unwinding the borrow buffer.
 * - The payout reads the proxy's USDC balance, splits the USDC leg's reserve
 *   off it with `core.splitAt`, and transfers the rest to the signer: the
 *   loan proceeds and the `mode: 'max'` repay's leftover.
 *
 * After execution the signer holds a Morpho Blue position with
 * `collateralAmount` WETH collateral and EURC debt equal to the original Aave
 * debt plus only the consumed slippage/fee; the Aave position is closed.
 */
export const buildAaveToMorphoDebtMigrationWithSwapExactOut = ({
  owner,
  collateralAmount,
  debtAmount,
  morphoBorrowAmount,
  maxSlippageBps = DEFAULT_MAX_SLIPPAGE_BPS,
  // The rest is the optional partner-fee fields. Spreading them forwards only
  // the fields the caller set, so no fee policy is baked in by default.
  ...partnerFee
}: AaveToMorphoDebtMigrationWithSwapExactOutInput): {
  flow: Flow;
  request: ComposeCompileRequest;
} => {
  const sdk = createComposeSdk({ baseUrl: BASE_URL, apiKey: API_KEY });

  // The USDC the BUY must produce: the flashloan principal plus the leg's
  // `maxFee`, the default fee cap (5 bps of the principal, rounded up, since
  // Aave rounds its premium half-up). This is the leg's reserve: the proxy
  // keeps it through the program and the runtime repays Aave from it. The
  // request states the same `maxFee` on the leg, so client and server agree
  // on the cap.
  const maxFee = flashloanMaxFee({
    providerKind: 'aave-v3',
    amount: debtAmount,
  });
  const exactUsdcOut = (BigInt(debtAmount) + maxFee).toString() as `${bigint}`;

  const builder = sdk.flow(8453, {
    name: 'aave-to-morpho-debt-migration-with-swap-exact-out',
    inputs: {
      initialCollateral: resources.erc20(BASE_WETH, 8453),
      debtFlashloan: resources.erc20(BASE_USDC, 8453),
      collateralFlashloan: resources.erc20(BASE_WETH, 8453),
    },
  });

  // --- Phase 1: provision the Aave V3 borrow position ----------------------

  // WETH → aBasWETH via the aave routing edge family (Aave V3 supply).
  const supplyToAave = builder.lifi.zap('supply-to-aave', {
    bind: { amountIn: builder.inputs.initialCollateral },
    config: { resourceOut: resources.erc20(A_BAS_WETH, 8453) },
  });

  // Borrow USDC against the freshly supplied collateral. The `borrowed`
  // resource is left unbound: it stays on the proxy as the user's loan
  // proceeds, and `payout-borrow-proceeds` forwards it to the signer.
  builder.aave.borrow('borrow-from-aave', {
    bind: {},
    config: {
      pool: AAVE_V3_POOL_BASE,
      asset: BASE_USDC,
      variableDebtToken: VDEBT_BAS_USDC,
      amount: debtAmount,
    },
  });

  // --- Phase 2: migrate to Morpho with two concurrent flashloans -----------

  // Settle the Aave USDC debt with flashloan #1. mode: 'max' makes Aave's Pool
  // clamp paybackAmount to the outstanding debt exactly, so any unspent
  // flashloan principal stays on the proxy and reaches the payout below.
  builder.aave.repay('repay-aave', {
    bind: {
      assetIn: builder.inputs.debtFlashloan,
      onBehalfOf: builder.context.executionAddress,
    },
    config: { pool: AAVE_V3_POOL_BASE, mode: 'max' },
  });

  // Withdraw the freed WETH from Aave (aBasWETH → WETH) via routing.
  const withdrawFromAave = builder.lifi.zap('withdraw-from-aave', {
    bind: { amountIn: supplyToAave.amountOut },
    config: { resourceOut: resources.erc20(BASE_WETH, 8453) },
  });

  // Open the Morpho Blue position with flashloan #2's WETH.
  builder.morphoBlue.supplyCollateral('supply-to-morpho', {
    bind: { assetIn: builder.inputs.collateralFlashloan },
    config: { marketParams: MORPHO_WETH_EURC_MARKET, mode: 'exact' },
  });

  // Borrow a capped (generously over-sized) EURC amount at the destination's
  // cheaper APR. The over-borrow is intentional and is unwound below.
  const morphoBorrow = builder.morphoBlue.borrow('borrow-from-morpho', {
    bind: {},
    config: {
      marketParams: MORPHO_WETH_EURC_MARKET,
      amount: morphoBorrowAmount,
    },
  });

  // Exact-output BUY: produce EXACTLY `exactUsdcOut` USDC from the borrowed
  // EURC, pulling only the EURC needed and refunding the rest to the proxy
  // (surfaced as `unspentIn`). `amountOut` is the USDC leg's whole reserve.
  const buyUsdc = builder.paraswap.buy('buy-usdc-exact', {
    bind: { amountIn: morphoBorrow.borrowed },
    config: {
      resourceOut: resources.erc20(BASE_USDC, 8453),
      exactAmountOut: exactUsdcOut,
      srcDecimals: EURC_DECIMALS,
      destDecimals: USDC_DECIMALS,
      maxSlippageBps,
      ...partnerFee,
    },
  });

  // Earmark the exact BUY output for the USDC flashloan leg. One repay node:
  // `amountOut` is the leg's whole reserve.
  builder.experimental.lifi.flashloanRepay('repay-debt-flashloan', {
    bind: { funds: buyUsdc.amountOut },
  });

  // Earmark the Aave withdrawal for the WETH flashloan leg. Balancer V2
  // charges no fee, so the withdrawn principal alone covers the leg.
  builder.experimental.lifi.flashloanRepay('repay-collateral-flashloan', {
    bind: { funds: withdrawFromAave.amountOut },
  });

  // Unwind the over-borrow: repay the proxy's FULL residual EURC back into the
  // freshly-minted Morpho debt, so the migrated debt equals exactly the EURC the
  // swap consumed. The live EURC balance equals `buy.unspentIn`
  // (morphoBorrowAmount − sold): Augustus's refund below the cap plus the
  // headroom we over-borrowed above it (morphoBorrowAmount − cap), which
  // Augustus never pulled. Repaying it leaves zero idle EURC. mode: 'exact'
  // pulls precisely this balance; mode: 'max' would repay the FULL Morpho debt
  // in shares mode (reading Position.borrowShares) and pull far more EURC than
  // the residual, reverting with "transfer amount exceeds balance".
  const eurcResidual = builder.core.balanceOf('read-eurc-residual', {
    bind: {},
    config: { token: BASE_EURC },
  });
  builder.morphoBlue.repay('repay-morpho-remainder', {
    bind: {
      assetIn: eurcResidual.balance,
      onBehalfOf: builder.context.executionAddress,
    },
    config: { marketParams: MORPHO_WETH_EURC_MARKET, mode: 'exact' },
  });

  // Pay out the USDC above the leg's reserve: the loan proceeds and the
  // repay's leftover.
  payOutUsdcAboveReserve(builder, exactUsdcOut);

  const flow = builder.build();

  const assumptions: Record<string, bigint> = {
    'buy-usdc-exact.amountIn': BigInt(morphoBorrowAmount),
  };

  const request = sdk.request(flow, {
    signer: owner,
    inputs: {
      // Supply Aave's worst-case rounding skew over the flashloaned
      // `collateralAmount`, so the Aave withdrawal (which rounds down by up to
      // that skew) still covers the Balancer collateral-flashloan principal at
      // settlement. The flashloan, and the migrated Morpho collateral, stay at
      // the full `collateralAmount`; the few extra wei stay on the proxy.
      initialCollateral: materialisers.directDeposit({
        amount: (
          BigInt(collateralAmount) + AAVE_SAME_BLOCK_SKEW_WEI
        ).toString() as `${bigint}`,
      }),
      debtFlashloan: materialisers.experimental.flashloan({
        providerKind: 'aave-v3',
        amount: debtAmount,
        maxFee,
      }),
      collateralFlashloan: materialisers.experimental.flashloan({
        providerKind: 'balancer-v2',
        amount: collateralAmount,
      }),
    },
    assumptions,
  });

  return { flow, request };
};
