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

import {Script, console} from "forge-std/Script.sol";
import {WETH} from "../contracts/tokens/WETH.sol";
import {CREATE2Factory} from "../contracts/utils/CREATE2Factory.sol";

/**
 * @title DeployWETHWithCREATE2
 * @notice Deploy WETH9 using CREATE2 for deterministic address
 * @dev This will create a NEW deterministic address, NOT the Ethereum Mainnet address
 *      (WETH9 on Ethereum Mainnet was deployed with CREATE, not CREATE2)
 */
contract DeployWETHWithCREATE2 is Script {
    // Ethereum Mainnet WETH9 address (for reference)
    address constant MAINNET_WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        console.log("Deploying WETH9 with CREATE2");
        console.log("Deployer:", deployer);
        console.log("Note: This will create a NEW address, not the Ethereum Mainnet address");
        console.log("Ethereum Mainnet WETH9:", MAINNET_WETH9);
        
        vm.startBroadcast(deployerPrivateKey);
        
        // First, deploy CREATE2Factory if not already deployed
        // For deterministic deployment, we can use a known CREATE2Factory address
        // or deploy it first
        
        // Get WETH bytecode
        bytes memory wethBytecode = type(WETH).creationCode;
        
        // Use a salt for deterministic address
        // Salt can be: keccak256("WETH9") or a specific value
        uint256 salt = uint256(keccak256("WETH9-ChainID-138"));
        
        // Deploy CREATE2Factory (or use existing)
        CREATE2Factory factory = new CREATE2Factory();
        console.log("CREATE2Factory deployed at:", address(factory));
        
        // Compute predicted address
        address predictedAddress = factory.computeAddress(wethBytecode, salt);
        console.log("Predicted WETH9 address:", predictedAddress);
        
        // Deploy using CREATE2
        address wethAddress = factory.deploy(wethBytecode, salt);
        console.log("WETH9 deployed at:", wethAddress);
        
        require(wethAddress == predictedAddress, "Address mismatch");
        
        // Verify it's the correct contract
        WETH weth = WETH(payable(wethAddress));
        console.log("WETH9 name:", weth.name());
        console.log("WETH9 symbol:", weth.symbol());
        console.log("WETH9 decimals:", weth.decimals());
        
        vm.stopBroadcast();
        
        console.log("\n=== Deployment Summary ===");
        console.log("WETH9 Address:", wethAddress);
        console.log("CREATE2Factory:", address(factory));
        console.log("Salt:", vm.toString(salt));
        console.log("\nNote: This address is different from Ethereum Mainnet");
        console.log("   Ethereum Mainnet WETH9:", MAINNET_WETH9);
        console.log("   (WETH9 on Mainnet was deployed with CREATE, not CREATE2)");
    }
}

