// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {DODOToUniswapV3MultiHopExternalUnwinder} from "../src/DODOToUniswapV3MultiHopExternalUnwinder.sol";

contract DODOToUniswapV3MultiHopExternalUnwinderMainnetForkTest is Test {
    address constant DODO_PMM_INTEGRATION_MAINNET = 0xa9F284eD010f4F7d7F8F201742b49b9f58e29b84;
    address constant UNISWAP_V3_ROUTER = 0xE592427A0AEce92De3Edee1F18E0157C05861564;
    address constant POOL_CWUSDC_USDT = 0xCC0fd27A40775c9AfcD2BBd3f7c902b0192c247A;
    address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
    address constant CWUSDC = 0x2de5F116bFcE3d0f922d9C8351e0c5Fc24b9284a;

    DODOToUniswapV3MultiHopExternalUnwinder internal unwinder;

    function setUp() public {
        string memory rpcUrl = vm.envString("ETHEREUM_MAINNET_RPC");
        vm.createSelectFork(rpcUrl);
        unwinder = new DODOToUniswapV3MultiHopExternalUnwinder(DODO_PMM_INTEGRATION_MAINNET, UNISWAP_V3_ROUTER);
    }

    function testFork_cWUSDCToUSDC_multihopViaUSDT_works() public {
        uint256 amountIn = 1_000_000;
        deal(CWUSDC, address(this), amountIn);
        IERC20(CWUSDC).approve(address(unwinder), amountIn);

        bytes memory path = abi.encodePacked(USDT, uint24(100), USDC);
        bytes memory data = abi.encode(POOL_CWUSDC_USDT, USDT, uint256(1), path);

        uint256 before = IERC20(USDC).balanceOf(address(this));
        uint256 amountOut = unwinder.unwind(CWUSDC, USDC, amountIn, 1, data);
        uint256 afterBal = IERC20(USDC).balanceOf(address(this));

        assertGt(amountOut, 0, "amountOut > 0");
        assertEq(afterBal - before, amountOut, "USDC received");
    }
}
