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

import {Script, console} from "forge-std/Script.sol";
import "../../../contracts/bridge/trustless/EnhancedSwapRouter.sol";

/**
 * @title DeployEnhancedSwapRouter
 * @notice Deployment script for EnhancedSwapRouter with multi-protocol support
 * @dev Deploys EnhancedSwapRouter with Uniswap V3, Curve, Dodoex, Balancer, and 1inch
 */
contract DeployEnhancedSwapRouter is Script {
    // Ethereum Mainnet addresses
    address constant UNISWAP_V3_ROUTER = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45;
    address constant CURVE_3POOL = 0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7;
    address constant DODOEX_ROUTER = 0xa356867fDCEa8e71AEaF87805808803806231FdC;
    address constant BALANCER_VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8;
    address constant ONEINCH_ROUTER = 0x1111111254EEB25477B68fb85Ed929f73A960582;
    address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
    address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
    address constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F;

    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        console.log("=== EnhancedSwapRouter Deployment ===");
        console.log("Deployer:", deployer);
        console.log("Chain ID:", block.chainid);
        
        require(block.chainid == 1, "DeployEnhancedSwapRouter: Ethereum Mainnet only");

        vm.startBroadcast(deployerPrivateKey);

        console.log("\n--- Deploying EnhancedSwapRouter ---");
        console.log("Uniswap V3 Router:", UNISWAP_V3_ROUTER);
        console.log("Curve 3Pool:", CURVE_3POOL);
        console.log("Dodoex Router:", DODOEX_ROUTER);
        console.log("Balancer Vault:", BALANCER_VAULT);
        console.log("1inch Router:", ONEINCH_ROUTER);
        console.log("WETH:", WETH);
        console.log("USDT:", USDT);
        console.log("USDC:", USDC);
        console.log("DAI:", DAI);

        EnhancedSwapRouter router = new EnhancedSwapRouter(
            UNISWAP_V3_ROUTER,
            CURVE_3POOL,
            DODOEX_ROUTER,
            BALANCER_VAULT,
            ONEINCH_ROUTER,
            WETH,
            USDT,
            USDC,
            DAI
        );

        console.log("\nEnhancedSwapRouter deployed at:", address(router));

        // Grant ROUTING_MANAGER_ROLE to deployer so setRoutingConfig can be called
        router.grantRole(router.ROUTING_MANAGER_ROLE(), deployer);

        // Configure default routing
        _configureDefaultRouting(router, deployer);

        vm.stopBroadcast();

        console.log("\n=== Deployment Summary ===");
        console.log("EnhancedSwapRouter:", address(router));
        console.log("\n=== Export to .env ===");
        console.log("export ENHANCED_SWAP_ROUTER=", vm.toString(address(router)));
    }

    function _configureDefaultRouting(EnhancedSwapRouter router, address deployer) internal {
        console.log("\n--- Configuring Default Routing ---");
        
        // Small swaps (< $10k): Uniswap V3, Dodoex
        EnhancedSwapRouter.SwapProvider[] memory smallProviders = new EnhancedSwapRouter.SwapProvider[](2);
        smallProviders[0] = EnhancedSwapRouter.SwapProvider.UniswapV3;
        smallProviders[1] = EnhancedSwapRouter.SwapProvider.Dodoex;
        router.setRoutingConfig(0, smallProviders);
        console.log("Small swap routing configured");

        // Medium swaps ($10k-$100k): Dodoex, Balancer, Uniswap V3
        EnhancedSwapRouter.SwapProvider[] memory mediumProviders = new EnhancedSwapRouter.SwapProvider[](3);
        mediumProviders[0] = EnhancedSwapRouter.SwapProvider.Dodoex;
        mediumProviders[1] = EnhancedSwapRouter.SwapProvider.Balancer;
        mediumProviders[2] = EnhancedSwapRouter.SwapProvider.UniswapV3;
        router.setRoutingConfig(1, mediumProviders);
        console.log("Medium swap routing configured");

        // Large swaps (> $100k): Dodoex, Curve, Balancer
        EnhancedSwapRouter.SwapProvider[] memory largeProviders = new EnhancedSwapRouter.SwapProvider[](3);
        largeProviders[0] = EnhancedSwapRouter.SwapProvider.Dodoex;
        largeProviders[1] = EnhancedSwapRouter.SwapProvider.Curve;
        largeProviders[2] = EnhancedSwapRouter.SwapProvider.Balancer;
        router.setRoutingConfig(2, largeProviders);
        console.log("Large swap routing configured");

        // Note: Balancer pool IDs need to be configured separately
        // after identifying the actual pool addresses
        console.log("\nWARNING: Remember to configure Balancer pool IDs after deployment");
    }
}

