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

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

interface IDODOMultiHopSwapExactInLike {
    function swapExactIn(address pool, address tokenIn, uint256 amountIn, uint256 minAmountOut)
        external
        returns (uint256 amountOut);
}

interface ISwapRouterLikeMultiHop {
    struct ExactInputParams {
        bytes path;
        address recipient;
        uint256 amountIn;
        uint256 amountOutMinimum;
    }

    function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
}

contract DODOToUniswapV3MultiHopExternalUnwinder {
    using SafeERC20 for IERC20;

    address public immutable integration;
    address public immutable router;

    error BadParams();

    constructor(address integration_, address router_) {
        integration = integration_;
        router = router_;
    }

    function unwind(address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, bytes calldata data)
        external
        returns (uint256 amountOut)
    {
        if (tokenIn == address(0) || tokenOut == address(0) || tokenIn == tokenOut || amountIn == 0) revert BadParams();

        (address dodoPool, address intermediateToken, uint256 minIntermediateOut, bytes memory uniswapPath) =
            abi.decode(data, (address, address, uint256, bytes));
        if (dodoPool == address(0) || intermediateToken == address(0) || intermediateToken == tokenIn || intermediateToken == tokenOut) {
            revert BadParams();
        }
        if (uniswapPath.length < 43) revert BadParams();

        IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
        IERC20(tokenIn).forceApprove(integration, amountIn);
        uint256 intermediateOut =
            IDODOMultiHopSwapExactInLike(integration).swapExactIn(dodoPool, tokenIn, amountIn, minIntermediateOut);

        IERC20(intermediateToken).forceApprove(router, intermediateOut);
        amountOut = ISwapRouterLikeMultiHop(router).exactInput(
            ISwapRouterLikeMultiHop.ExactInputParams({
                path: uniswapPath,
                recipient: msg.sender,
                amountIn: intermediateOut,
                amountOutMinimum: minAmountOut
            })
        );
    }
}
