// 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 IDODOIntegrationSwapExactIn {
    function swapExactIn(address pool, address tokenIn, uint256 amountIn, uint256 minAmountOut)
        external
        returns (uint256 amountOut);
}

/**
 * @title TwoHopDodoIntegrationUnwinder
 * @notice Unwind `tokenIn -> midToken` on `poolA`, then `midToken -> tokenOut` on `poolB`, both via the same DODO PMM integration.
 * @dev `data` = abi.encode(address poolA, address poolB, address midToken, uint256 minMidOut).
 *      Use for Mainnet when there is no Uniswap V3 route for cWUSDC/USDC but there *is* depth on
 *      cWUSDC/cWUSDT and a second PMM pool cWUSDT/USDC (sizes must fit the second pool).
 */
contract TwoHopDodoIntegrationUnwinder {
    using SafeERC20 for IERC20;

    address public immutable integration;

    error BadParams();

    constructor(address integration_) {
        if (integration_ == address(0)) revert BadParams();
        integration = integration_;
    }

    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 poolA, address poolB, address midToken, uint256 minMidOut) =
            abi.decode(data, (address, address, address, uint256));
        if (poolA == address(0) || poolB == address(0) || midToken == address(0)) revert BadParams();
        if (midToken == tokenIn || midToken == tokenOut) revert BadParams();

        IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
        IERC20(tokenIn).forceApprove(integration, amountIn);

        uint256 midOut = IDODOIntegrationSwapExactIn(integration).swapExactIn(poolA, tokenIn, amountIn, minMidOut);
        if (midOut == 0) revert BadParams();

        IERC20(midToken).forceApprove(integration, midOut);
        amountOut = IDODOIntegrationSwapExactIn(integration).swapExactIn(poolB, midToken, midOut, minAmountOut);

        IERC20(tokenOut).safeTransfer(msg.sender, amountOut);
    }
}
