// 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 DODOIntegrationExternalUnwinder
 * @notice Unwinds base -> quote through a DODO PMM integration.
 * @dev `data` must be `abi.encode(address pool)` selecting the registered pool to use.
 */
contract DODOIntegrationExternalUnwinder {
    using SafeERC20 for IERC20;

    address public immutable integration;

    error BadParams();

    constructor(address integration_) {
        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();
        if (data.length != 32) revert BadParams();

        address pool = abi.decode(data, (address));
        if (pool == address(0)) revert BadParams();

        IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
        IERC20(tokenIn).forceApprove(integration, amountIn);
        amountOut = IDODOIntegrationSwapExactIn(integration).swapExactIn(pool, tokenIn, amountIn, minAmountOut);
        IERC20(tokenOut).safeTransfer(msg.sender, amountOut);
    }
}
