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

import {Script, console} from "forge-std/Script.sol";
import "../../../contracts/bridge/trustless/EnhancedSwapRouterV2.sol";
import "../../../contracts/bridge/trustless/IntentBridgeCoordinatorV2.sol";
import "../../../contracts/bridge/trustless/RouteTypesV2.sol";
import "../../../contracts/bridge/trustless/adapters/DodoRouteExecutorAdapter.sol";
import "../../../contracts/bridge/trustless/adapters/DodoV3RouteExecutorAdapter.sol";
import "../../../contracts/bridge/trustless/adapters/UniswapV3RouteExecutorAdapter.sol";
import "../../../contracts/bridge/trustless/adapters/BalancerRouteExecutorAdapter.sol";
import "../../../contracts/bridge/trustless/adapters/CurveRouteExecutorAdapter.sol";
import "../../../contracts/bridge/trustless/adapters/OneInchRouteExecutorAdapter.sol";
import "../../../contracts/liquidity/providers/DODOPMMProvider.sol";

contract DeployAllMainnetEnhancedSwapRouterV2 is Script {
    uint256 constant ALL_MAINNET_CHAIN_ID = 651940;

    address constant ALL_WETH = 0x798F6762BB40d6801A593459d08F890603D3979C;
    address constant ALL_USDT = 0x66D8Efa0AF63B0e84eb1Dd72bf00f00cd1e2234e;
    address constant ALL_AUSDC = 0xa95EeD79f84E6A0151eaEb9d441F9Ffd50e8e881;
    address constant ALL_AUSDT = 0x015B1897Ed5279930bC2Be46F661894d219292A6;
    address constant ALL_WALL = 0x2da2b8f961F161ab6320acB3377e2e844a3C3ce4;

    address constant ALL_DODO_PMM_INTEGRATION = 0x8528E268F3b8C94208d09D131ACa3Ea93Bad57c7;
    address constant ALL_DODO_POOL_WALL_AUSDC = 0x7b81Dad382BBB57e91a80389bA48e41Abd10794F;
    address constant ALL_DODO_POOL_WALL_USDT = 0x261D7e1447EE88398B2b5a274D49454F5B86800E;

    function run() external {
        require(block.chainid == ALL_MAINNET_CHAIN_ID, "DeployAllMainnetEnhancedSwapRouterV2: ALL Mainnet only");

        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        address dodoProvider = vm.envOr("ALL_MAINNET_DODO_PMM_PROVIDER_ADDRESS", address(0));

        console.log("=== ALL Mainnet EnhancedSwapRouterV2 Deployment ===");
        console.log("Deployer:", deployer);
        console.log("DODO PMM integration:", ALL_DODO_PMM_INTEGRATION);
        console.log("Existing DODO PMM provider:", dodoProvider);

        vm.startBroadcast(deployerPrivateKey);

        if (dodoProvider == address(0)) {
            DODOPMMProvider provider = new DODOPMMProvider(ALL_DODO_PMM_INTEGRATION, deployer);
            dodoProvider = address(provider);
            console.log("DODOPMMProvider deployed at:", dodoProvider);
        }

        _registerDodoPair(dodoProvider, ALL_WALL, ALL_AUSDC, ALL_DODO_POOL_WALL_AUSDC);
        _registerDodoPair(dodoProvider, ALL_WALL, ALL_USDT, ALL_DODO_POOL_WALL_USDT);

        DodoRouteExecutorAdapter dodoAdapter = new DodoRouteExecutorAdapter();
        DodoV3RouteExecutorAdapter dodoV3Adapter = new DodoV3RouteExecutorAdapter();
        UniswapV3RouteExecutorAdapter uniswapAdapter = new UniswapV3RouteExecutorAdapter();
        BalancerRouteExecutorAdapter balancerAdapter = new BalancerRouteExecutorAdapter();
        CurveRouteExecutorAdapter curveAdapter = new CurveRouteExecutorAdapter();
        OneInchRouteExecutorAdapter oneInchAdapter = new OneInchRouteExecutorAdapter();

        EnhancedSwapRouterV2 router = new EnhancedSwapRouterV2(
            ALL_WETH,
            ALL_USDT,
            ALL_AUSDC,
            ALL_AUSDT
        );
        IntentBridgeCoordinatorV2 coordinator = new IntentBridgeCoordinatorV2(address(router));

        router.setProviderAdapter(RouteTypesV2.Provider.Dodo, address(dodoAdapter));
        router.setProviderAdapter(RouteTypesV2.Provider.DodoV3, address(dodoV3Adapter));
        router.setProviderAdapter(RouteTypesV2.Provider.UniswapV3, address(uniswapAdapter));
        router.setProviderAdapter(RouteTypesV2.Provider.Balancer, address(balancerAdapter));
        router.setProviderAdapter(RouteTypesV2.Provider.Curve, address(curveAdapter));
        router.setProviderAdapter(RouteTypesV2.Provider.OneInch, address(oneInchAdapter));

        _setDodoPair(router, ALL_WALL, ALL_AUSDC, dodoProvider, ALL_DODO_POOL_WALL_AUSDC);
        _setDodoPair(router, ALL_WALL, ALL_USDT, dodoProvider, ALL_DODO_POOL_WALL_USDT);

        router.setProviderEnabled(RouteTypesV2.Provider.DodoV3, false);
        router.setProviderEnabled(RouteTypesV2.Provider.UniswapV3, false);
        router.setProviderEnabled(RouteTypesV2.Provider.Balancer, false);
        router.setProviderEnabled(RouteTypesV2.Provider.Curve, false);
        router.setProviderEnabled(RouteTypesV2.Provider.OneInch, false);
        router.setProviderEnabled(RouteTypesV2.Provider.Partner, false);

        vm.stopBroadcast();

        console.log("EnhancedSwapRouterV2:", address(router));
        console.log("IntentBridgeCoordinatorV2:", address(coordinator));
        console.log("DODOPMMProvider:", dodoProvider);
        console.log("DodoRouteExecutorAdapter:", address(dodoAdapter));
        console.log("DodoV3RouteExecutorAdapter:", address(dodoV3Adapter));
        console.log("UniswapV3RouteExecutorAdapter:", address(uniswapAdapter));
        console.log("BalancerRouteExecutorAdapter:", address(balancerAdapter));
        console.log("CurveRouteExecutorAdapter:", address(curveAdapter));
        console.log("OneInchRouteExecutorAdapter:", address(oneInchAdapter));
    }

    function _registerDodoPair(
        address provider,
        address tokenA,
        address tokenB,
        address pool
    ) internal {
        DODOPMMProvider(provider).registerPool(tokenA, tokenB, pool);
        DODOPMMProvider(provider).registerPool(tokenB, tokenA, pool);
    }

    function _setDodoPair(
        EnhancedSwapRouterV2 router,
        address tokenA,
        address tokenB,
        address target,
        address pool
    ) internal {
        bytes memory providerData = abi.encode(pool);
        router.setProviderRoute(tokenA, tokenB, RouteTypesV2.Provider.Dodo, target, providerData, true);
        router.setProviderRoute(tokenB, tokenA, RouteTypesV2.Provider.Dodo, target, providerData, true);
    }
}
