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

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

/**
 * @title DeployDualRouterBridgeSwapCoordinator
 * @notice Deploys DualRouterBridgeSwapCoordinator on Ethereum Mainnet and wires LP + EnhancedSwapRouter
 * @dev Env: PRIVATE_KEY, ETHEREUM_MAINNET_RPC; INBOX_ETH_MAINNET (or INBOX_ETH), LIQUIDITY_POOL_ETH_MAINNET (or LIQUIDITY_POOL),
 *      SWAP_ROUTER_MAINNET (or SWAP_ROUTER), ENHANCED_SWAP_ROUTER, CHALLENGE_MANAGER_MAINNET (or CHALLENGE_MANAGER).
 */
contract DeployDualRouterBridgeSwapCoordinator is Script {
    function run() external {
        require(block.chainid == 1, "DeployDualRouterBridgeSwapCoordinator: Ethereum Mainnet only");

        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);

        address inbox = vm.envOr("INBOX_ETH_MAINNET", vm.envOr("INBOX_ETH", address(0)));
        address liquidityPool = vm.envOr("LIQUIDITY_POOL_ETH_MAINNET", vm.envOr("LIQUIDITY_POOL", address(0)));
        address swapRouter = vm.envOr("SWAP_ROUTER_MAINNET", vm.envOr("SWAP_ROUTER", address(0)));
        address enhancedSwapRouter = vm.envOr("ENHANCED_SWAP_ROUTER", address(0));
        address challengeManager = vm.envOr("CHALLENGE_MANAGER_MAINNET", vm.envOr("CHALLENGE_MANAGER", address(0)));

        require(inbox != address(0), "Set INBOX_ETH_MAINNET or INBOX_ETH");
        require(liquidityPool != address(0), "Set LIQUIDITY_POOL_ETH_MAINNET or LIQUIDITY_POOL");
        require(swapRouter != address(0), "Set SWAP_ROUTER_MAINNET or SWAP_ROUTER");
        require(enhancedSwapRouter != address(0), "Set ENHANCED_SWAP_ROUTER");
        require(challengeManager != address(0), "Set CHALLENGE_MANAGER_MAINNET or CHALLENGE_MANAGER");

        console.log("=== Deploy DualRouterBridgeSwapCoordinator (Ethereum Mainnet) ===");
        console.log("Inbox:", inbox);
        console.log("LiquidityPool:", liquidityPool);
        console.log("SwapRouter:", swapRouter);
        console.log("EnhancedSwapRouter:", enhancedSwapRouter);
        console.log("ChallengeManager:", challengeManager);

        vm.startBroadcast(deployerPrivateKey);

        DualRouterBridgeSwapCoordinator coordinator = new DualRouterBridgeSwapCoordinator(
            inbox,
            liquidityPool,
            swapRouter,
            enhancedSwapRouter,
            challengeManager
        );

        console.log("DualRouterBridgeSwapCoordinator deployed at:", address(coordinator));

        // Authorize the new coordinator to release from LiquidityPoolETH
        LiquidityPoolETH(payable(liquidityPool)).authorizeRelease(address(coordinator));
        console.log("LiquidityPoolETH.authorizeRelease(coordinator) done");

        // Grant COORDINATOR_ROLE on EnhancedSwapRouter to the new coordinator
        bytes32 COORDINATOR_ROLE = keccak256("COORDINATOR_ROLE");
        EnhancedSwapRouter(payable(enhancedSwapRouter)).grantRole(COORDINATOR_ROLE, address(coordinator));
        console.log("EnhancedSwapRouter.grantRole(COORDINATOR_ROLE, coordinator) done");

        vm.stopBroadcast();

        console.log("\n=== Export to .env ===");
        console.log("DUAL_ROUTER_BRIDGE_SWAP_COORDINATOR=", vm.toString(address(coordinator)));
    }
}
