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

import {Script, console} from "forge-std/Script.sol";
import {CCIPRouter} from "../contracts/ccip/CCIPRouter.sol";
import {CCIPWETH9Bridge} from "../contracts/ccip/CCIPWETH9Bridge.sol";

/**
 * Deploy both fixes for "Send ETH to mainnet" revert (0x9996b315):
 * 1. Router-side: New router + bridge that accept Chain 138 LINK as fee token.
 * 2. Bridge-side: New router + bridge that accept native ETH as fee token.
 *
 * Usage:
 *   PRIVATE_KEY=... forge script script/DeploySendEthToMainnetFixes.s.sol --rpc-url $RPC_URL_138 --broadcast
 * Optional: MAINNET_WETH9_BRIDGE_ADDRESS=0x... (receiver on mainnet; default from docs)
 */
contract DeploySendEthToMainnetFixes is Script {
    address constant CHAIN138_LINK = 0xb7721dD53A8c629d9f1Ba31a5819AFe250002b03;
    address constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    uint64 constant MAINNET_SELECTOR = 5009297550715157269;
    uint256 constant BASE_FEE = 1e15;
    uint256 constant DATA_FEE_PER_BYTE = 100_000_000;

    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address mainnetReceiver = vm.envOr(
            "MAINNET_WETH9_BRIDGE_ADDRESS",
            address(0x3304b747E565a97ec8AC220b0B6A1f6ffDB837e6)
        );

        vm.startBroadcast(deployerPrivateKey);

        // --- Fix 1: Router + Bridge that accept Chain 138 LINK as fee ---
        CCIPRouter routerLINK = new CCIPRouter(CHAIN138_LINK, BASE_FEE, DATA_FEE_PER_BYTE);
        routerLINK.addSupportedChain(MAINNET_SELECTOR);
        routerLINK.addSupportedToken(MAINNET_SELECTOR, WETH9);

        CCIPWETH9Bridge bridgeLINK = new CCIPWETH9Bridge(
            address(routerLINK),
            WETH9,
            CHAIN138_LINK
        );
        bridgeLINK.addDestination(MAINNET_SELECTOR, mainnetReceiver);

        console.log("Fix 1 (pay in LINK):");
        console.log("  Router LINK:", address(routerLINK));
        console.log("  Bridge LINK:", address(bridgeLINK));

        // --- Fix 2: Router + Bridge that accept native ETH as fee ---
        CCIPRouter routerNATIVE = new CCIPRouter(address(0), BASE_FEE, DATA_FEE_PER_BYTE);
        routerNATIVE.addSupportedChain(MAINNET_SELECTOR);
        routerNATIVE.addSupportedToken(MAINNET_SELECTOR, WETH9);

        CCIPWETH9Bridge bridgeNative = new CCIPWETH9Bridge(
            address(routerNATIVE),
            WETH9,
            address(0)
        );
        bridgeNative.addDestination(MAINNET_SELECTOR, mainnetReceiver);

        console.log("Fix 2 (pay in native ETH):");
        console.log("  Router NATIVE:", address(routerNATIVE));
        console.log("  Bridge NATIVE:", address(bridgeNative));

        vm.stopBroadcast();

        console.log("\n=== Set in .env ===");
        console.log("# Use LINK fee (Chain 138 LINK):");
        console.log("CCIPWETH9_BRIDGE_CHAIN138_LINK=", address(bridgeLINK));
        console.log("# Use native ETH fee:");
        console.log("CCIPWETH9_BRIDGE_CHAIN138_NATIVE=", address(bridgeNative));
    }
}
