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

import {Script, console2} from "forge-std/Script.sol";
import {CCIPRouter} from "../contracts/ccip/CCIPRouter.sol";
import {CWMultiTokenBridgeL2} from "../contracts/bridge/CWMultiTokenBridgeL2.sol";

/// @notice Deploy event-only CCIPRouter on Ethereum mainnet for cW burnAndSend → Chain 138 relay.
/// @dev Wire CW_BRIDGE_MAINNET.sendRouter to this router (supports CHAIN138_SELECTOR).
contract DeployMainnetCwReverseRouter is Script {
    uint64 constant CHAIN138_SELECTOR = 16015286601757825753;
    uint256 constant BASE_FEE = 1e15;
    uint256 constant DATA_FEE_PER_BYTE = 100_000_000;

    function run() external {
        uint256 deployerKey = vm.envUint("PRIVATE_KEY");
        address link = vm.envOr("MAINNET_LINK_TOKEN", address(0x514910771AF9Ca656af840dff83E8264EcF986CA));
        address l2 = vm.envOr("CW_BRIDGE_MAINNET", address(0x2bF74583206A49Be07E0E8A94197C12987AbD7B5));
        bool wire = vm.envOr("WIRE_L2_SEND_ROUTER", true);

        vm.startBroadcast(deployerKey);

        CCIPRouter router = new CCIPRouter(link, BASE_FEE, DATA_FEE_PER_BYTE);
        router.addSupportedChain(CHAIN138_SELECTOR);

        console2.log("MainnetCwReverseRouter:", address(router));
        console2.log("CHAIN138_SELECTOR:", CHAIN138_SELECTOR);

        if (wire) {
            CWMultiTokenBridgeL2 bridge = CWMultiTokenBridgeL2(payable(l2));
            bridge.setSendRouter(address(router));
            console2.log("CW_BRIDGE_MAINNET sendRouter updated:", l2);
        }

        vm.stopBroadcast();
    }
}
