// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.26;

import "@src/executors/BalancerV2Executor.sol";
import {Test} from "../../lib/forge-std/src/Test.sol";
import {Constants} from "../Constants.sol";

contract BalancerV2ExecutorExposed is BalancerV2Executor {
    function decodeParams(bytes calldata data)
        external
        pure
        returns (
            IERC20 tokenIn,
            IERC20 tokenOut,
            bytes32 poolId,
            address receiver,
            bool needsApproval
        )
    {
        return _decodeData(data);
    }
}

contract BalancerV2ExecutorTest is
    BalancerV2ExecutorExposed,
    Test,
    Constants
{
    using SafeERC20 for IERC20;

    BalancerV2ExecutorExposed balancerV2Exposed;
    IERC20 WETH = IERC20(WETH_ADDR);
    IERC20 BAL = IERC20(BAL_ADDR);
    bytes32 constant WETH_BAL_POOL_ID =
        0x5c6ee304399dbdb9c8ef030ab642b10820db8f56000200000000000000000014;

    function setUp() public {
        uint256 forkBlock = 17323404;
        vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
        balancerV2Exposed = new BalancerV2ExecutorExposed();
    }

    function testDecodeParams() public view {
        bytes memory params = abi.encodePacked(
            WETH_ADDR, BAL_ADDR, WETH_BAL_POOL_ID, address(2), true
        );

        (
            IERC20 tokenIn,
            IERC20 tokenOut,
            bytes32 poolId,
            address receiver,
            bool needsApproval
        ) = balancerV2Exposed.decodeParams(params);

        assertEq(address(tokenIn), WETH_ADDR);
        assertEq(address(tokenOut), BAL_ADDR);
        assertEq(poolId, WETH_BAL_POOL_ID);
        assertEq(receiver, address(2));
        assertEq(needsApproval, true);
    }

    function testDecodeParamsInvalidDataLength() public {
        bytes memory invalidParams =
            abi.encodePacked(WETH_ADDR, BAL_ADDR, WETH_BAL_POOL_ID, address(2));

        vm.expectRevert(BalancerV2Executor__InvalidDataLength.selector);
        balancerV2Exposed.decodeParams(invalidParams);
    }

    function testSwap() public {
        uint256 amountIn = 10 ** 18;
        bytes memory protocolData =
            abi.encodePacked(WETH_ADDR, BAL_ADDR, WETH_BAL_POOL_ID, BOB, true);

        deal(WETH_ADDR, address(balancerV2Exposed), amountIn);
        uint256 balanceBefore = BAL.balanceOf(BOB);

        uint256 amountOut = balancerV2Exposed.swap(amountIn, protocolData);

        uint256 balanceAfter = BAL.balanceOf(BOB);
        assertGt(balanceAfter, balanceBefore);
        assertEq(balanceAfter - balanceBefore, amountOut);
    }

    function testDecodeIntegration() public view {
        // Generated by the SwapEncoder - test_encode_balancer_v2
        bytes memory protocolData =
            hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01";

        (
            IERC20 tokenIn,
            IERC20 tokenOut,
            bytes32 poolId,
            address receiver,
            bool needsApproval
        ) = balancerV2Exposed.decodeParams(protocolData);

        assertEq(address(tokenIn), WETH_ADDR);
        assertEq(address(tokenOut), BAL_ADDR);
        assertEq(poolId, WETH_BAL_POOL_ID);
        assertEq(receiver, BOB);
        assertEq(needsApproval, true);
    }

    function testSwapIntegration() public {
        // Generated by the SwapEncoder - test_encode_balancer_v2
        bytes memory protocolData =
            hex"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ba100000625a3754423978a60c9317c58a424e3d5c6ee304399dbdb9c8ef030ab642b10820db8f560002000000000000000000141d96f2f6bef1202e4ce1ff6dad0c2cb002861d3e01";

        uint256 amountIn = 10 ** 18;
        deal(WETH_ADDR, address(balancerV2Exposed), amountIn);
        uint256 balanceBefore = BAL.balanceOf(BOB);

        uint256 amountOut = balancerV2Exposed.swap(amountIn, protocolData);

        uint256 balanceAfter = BAL.balanceOf(BOB);
        assertGt(balanceAfter, balanceBefore);
        assertEq(balanceAfter - balanceBefore, amountOut);
    }
}
