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

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

/**
 * @title DeployWETHBridges
 * @notice Deployment script for WETH9 and WETH10 bridges to ChainID 138
 * @dev Deploys bridges with full interface including addDestination and getDestinationChains
 */
contract DeployWETHBridges is Script {
    CCIPWETH9Bridge public weth9Bridge;
    CCIPWETH10Bridge public weth10Bridge;

    // Configuration
    address public admin;
    address public ccipRouter;
    address public weth9;
    address public weth10;
    address public linkToken;

    // Mainnet configuration (checksummed addresses)
    uint64 public constant MAINNET_CHAIN_SELECTOR = 5009297550715157269;
    address public mainnetWeth9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address public mainnetWeth10 = 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F;

    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        admin = vm.addr(deployerPrivateKey);

        // Get addresses from environment or use defaults (checksummed).
        // Default router: working CCIP router (0x8078A...) with code; do not use 0x80226... (no code).
        ccipRouter = vm.envOr("CCIP_ROUTER_ADDRESS", address(0x8078A09637e47Fa5Ed34F626046Ea2094a5CDE5e));
        weth9 = vm.envOr("WETH9_ADDRESS", address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
        weth10 = vm.envOr("WETH10_ADDRESS", address(0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F));
        linkToken = vm.envOr("LINK_TOKEN_ADDRESS", address(0x514910771AF9Ca656af840dff83E8264EcF986CA));

        vm.startBroadcast(deployerPrivateKey);

        console.log("=== Deploying WETH Bridges to ChainID 138 ===");
        console.log("Admin:", admin);
        console.log("CCIP Router:", ccipRouter);
        console.log("WETH9:", weth9);
        console.log("WETH10:", weth10);
        console.log("LINK Token:", linkToken);

        // Deploy WETH9 Bridge (constructor: ccipRouter, weth9, feeToken)
        console.log("\n1. Deploying WETH9 Bridge...");
        weth9Bridge = new CCIPWETH9Bridge(ccipRouter, weth9, linkToken);
        console.log("WETH9 Bridge:", address(weth9Bridge));

        // Deploy WETH10 Bridge (constructor: ccipRouter, weth10, feeToken)
        console.log("\n2. Deploying WETH10 Bridge...");
        weth10Bridge = new CCIPWETH10Bridge(ccipRouter, weth10, linkToken);
        console.log("WETH10 Bridge:", address(weth10Bridge));

        // Configure Mainnet destination (addDestination: chainSelector, receiverBridge)
        address mainnetWeth9Receiver = vm.envOr("MAINNET_WETH9_BRIDGE_ADDRESS", address(0));
        address mainnetWeth10Receiver = vm.envOr("MAINNET_WETH10_BRIDGE_ADDRESS", address(0));
        if (mainnetWeth9Receiver != address(0)) {
            weth9Bridge.addDestination(MAINNET_CHAIN_SELECTOR, mainnetWeth9Receiver);
            console.log("WETH9 Bridge: Mainnet destination added");
        }
        if (mainnetWeth10Receiver != address(0)) {
            weth10Bridge.addDestination(MAINNET_CHAIN_SELECTOR, mainnetWeth10Receiver);
            console.log("WETH10 Bridge: Mainnet destination added");
        }

        // Verify destinations (optional when MAINNET_*_BRIDGE_ADDRESS not set)
        console.log("\n4. Verifying destinations...");
        uint64[] memory weth9Chains = weth9Bridge.getDestinationChains();
        uint64[] memory weth10Chains = weth10Bridge.getDestinationChains();
        if (mainnetWeth9Receiver != address(0)) {
            require(weth9Chains.length == 1, "WETH9 should have 1 destination");
            require(weth9Chains[0] == MAINNET_CHAIN_SELECTOR, "WETH9 destination should be Mainnet");
        }
        if (mainnetWeth10Receiver != address(0)) {
            require(weth10Chains.length == 1, "WETH10 should have 1 destination");
            require(weth10Chains[0] == MAINNET_CHAIN_SELECTOR, "WETH10 destination should be Mainnet");
        }

        vm.stopBroadcast();

        console.log("\n=== WETH Bridges Deployment Complete ===");
        console.log("Summary:");
        console.log("  WETH9 Bridge:", address(weth9Bridge));
        console.log("  WETH10 Bridge:", address(weth10Bridge));
        console.log("  Mainnet Chain Selector:", MAINNET_CHAIN_SELECTOR);
        console.log("\nNext Steps:");
        console.log("  1. Fund bridges with LINK tokens for CCIP fees");
        console.log("  2. Test bidirectional transfers");
        console.log("  3. Monitor bridge activity");
    }
}
