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

import {Test, console} from "forge-std/Test.sol";
import "../../../../contracts/bridge/trustless/EnhancedSwapRouter.sol";
import "../../../../contracts/bridge/trustless/LiquidityPoolETH.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MockERC20 is ERC20 {
    constructor(string memory name, string memory symbol) ERC20(name, symbol) {
        _mint(msg.sender, 1000000 ether);
    }
}

contract LiquidityEngineIntegrationTest is Test {
    EnhancedSwapRouter public router;
    LiquidityPoolETH public liquidityPool;
    
    MockERC20 public weth;
    MockERC20 public usdt;
    MockERC20 public usdc;
    MockERC20 public dai;

    address public deployer = address(0xDE0001);

    // Mock protocol addresses
    address public uniswapV3Router = address(0x1111111111111111111111111111111111111111);
    address public curve3Pool = address(0x2222222222222222222222222222222222222222);
    address public dodoexRouter = address(0x3333333333333333333333333333333333333333);
    address public balancerVault = address(0x4444444444444444444444444444444444444444);
    address public oneInchRouter = address(0x5555555555555555555555555555555555555555);

    function setUp() public {
        vm.startPrank(deployer);

        weth = new MockERC20("Wrapped Ether", "WETH");
        usdt = new MockERC20("Tether USD", "USDT");
        usdc = new MockERC20("USD Coin", "USDC");
        dai = new MockERC20("Dai Stablecoin", "DAI");

        liquidityPool = new LiquidityPoolETH(address(weth), 5, 11000);

        router = new EnhancedSwapRouter(
            uniswapV3Router,
            curve3Pool,
            dodoexRouter,
            balancerVault,
            oneInchRouter,
            address(weth),
            address(usdt),
            address(usdc),
            address(dai)
        );

        router.grantRole(router.ROUTING_MANAGER_ROLE(), deployer);

        vm.stopPrank();
    }

    function testRoutingConfig_SmallSwap() public view {
        // Small swap should prefer Uniswap V3 and Dodoex
        // This is tested via the routing logic
        assertTrue(router.providerEnabled(EnhancedSwapRouter.SwapProvider.UniswapV3));
        assertTrue(router.providerEnabled(EnhancedSwapRouter.SwapProvider.Dodoex));
    }

    function testRoutingConfig_MediumSwap() public {
        EnhancedSwapRouter.SwapProvider[] memory providers = new EnhancedSwapRouter.SwapProvider[](3);
        providers[0] = EnhancedSwapRouter.SwapProvider.Dodoex;
        providers[1] = EnhancedSwapRouter.SwapProvider.Balancer;
        providers[2] = EnhancedSwapRouter.SwapProvider.UniswapV3;

        vm.prank(deployer);
        router.setRoutingConfig(1, providers); // Medium swaps

        assertTrue(true); // Config set successfully
    }

    function testRoutingConfig_LargeSwap() public {
        EnhancedSwapRouter.SwapProvider[] memory providers = new EnhancedSwapRouter.SwapProvider[](3);
        providers[0] = EnhancedSwapRouter.SwapProvider.Dodoex;
        providers[1] = EnhancedSwapRouter.SwapProvider.Curve;
        providers[2] = EnhancedSwapRouter.SwapProvider.Balancer;

        vm.prank(deployer);
        router.setRoutingConfig(2, providers); // Large swaps

        assertTrue(true); // Config set successfully
    }

    function testProviderToggle() public {
        vm.prank(deployer);
        router.setProviderEnabled(EnhancedSwapRouter.SwapProvider.UniswapV3, false);
        
        assertFalse(router.providerEnabled(EnhancedSwapRouter.SwapProvider.UniswapV3));

        vm.prank(deployer);
        router.setProviderEnabled(EnhancedSwapRouter.SwapProvider.UniswapV3, true);
        
        assertTrue(router.providerEnabled(EnhancedSwapRouter.SwapProvider.UniswapV3));
    }

    function testBalancerPoolIdConfiguration() public {
        bytes32 poolId = keccak256("weth-usdt-pool");

        vm.prank(deployer);
        router.setBalancerPoolId(address(weth), address(usdt), poolId);

        assertEq(router.balancerPoolIds(address(weth), address(usdt)), poolId);
    }

    function testGetQuotes_AllProviders() public view {
        // Test that getQuotes function exists and can be called
        (EnhancedSwapRouter.SwapProvider[] memory providers, uint256[] memory amounts) = 
            router.getQuotes(address(usdt), 1 ether);
        
        // Function should execute without revert
        // Actual quotes depend on protocol integration
        assertTrue(providers.length >= 0);
        assertTrue(amounts.length >= 0);
    }
}

