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

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

/**
 * @title DeployWETH10WithCREATE2
 * @notice Deploy WETH10 using CREATE2 for deterministic address
 * @dev Attempts to match Ethereum Mainnet address if WETH10 was deployed with CREATE2
 *      If WETH10 on Mainnet was deployed with CREATE2, we can match the address
 *      by using the same bytecode, salt, and deployer address
 */
contract DeployWETH10WithCREATE2 is Script {
    // Ethereum Mainnet WETH10 address (for reference)
    address constant MAINNET_WETH10 = 0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9F;
    
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        console.log("Deploying WETH10 with CREATE2");
        console.log("Deployer:", deployer);
        console.log("Ethereum Mainnet WETH10:", MAINNET_WETH10);
        
        vm.startBroadcast(deployerPrivateKey);
        
        // Get WETH10 bytecode
        bytes memory weth10Bytecode = type(WETH10).creationCode;
        
        // Try to match Ethereum Mainnet address
        // If WETH10 was deployed with CREATE2, we need:
        // 1. Same bytecode (must match exactly)
        // 2. Same deployer address (or factory address)
        // 3. Same salt
        
        // Option 1: Use a known salt if WETH10 was deployed with CREATE2
        // Common salt: keccak256("WETH10") or a specific value
        uint256 salt = uint256(keccak256("WETH10-ChainID-138"));
        
        // Option 2: If we know the original salt from Ethereum Mainnet, use it
        // For now, we'll use a deterministic salt for this chain
        
        // Deploy CREATE2Factory (or use existing)
        CREATE2Factory factory = new CREATE2Factory();
        console.log("CREATE2Factory deployed at:", address(factory));
        
        // Compute predicted address
        address predictedAddress = factory.computeAddress(weth10Bytecode, salt);
        console.log("Predicted WETH10 address:", predictedAddress);
        
        // Check if this matches Mainnet address
        if (predictedAddress == MAINNET_WETH10) {
            console.log("Predicted address matches Ethereum Mainnet!");
        } else {
            console.log("Predicted address differs from Ethereum Mainnet");
            console.log("   This is expected if WETH10 on Mainnet was deployed with CREATE");
            console.log("   or if bytecode/salt/deployer differs");
        }
        
        // Deploy using CREATE2
        address weth10Address = factory.deploy(weth10Bytecode, salt);
        console.log("WETH10 deployed at:", weth10Address);
        
        require(weth10Address == predictedAddress, "Address mismatch");
        
        // Verify it's the correct contract
        WETH10 weth10 = WETH10(payable(weth10Address));
        console.log("WETH10 name:", weth10.name());
        console.log("WETH10 symbol:", weth10.symbol());
        console.log("WETH10 decimals:", weth10.decimals());
        
        vm.stopBroadcast();
        
        console.log("\n=== Deployment Summary ===");
        console.log("WETH10 Address:", weth10Address);
        console.log("CREATE2Factory:", address(factory));
        console.log("Salt:", vm.toString(salt));
        console.log("Ethereum Mainnet WETH10:", MAINNET_WETH10);
        
        if (weth10Address == MAINNET_WETH10) {
            console.log("\nSUCCESS: Address matches Ethereum Mainnet!");
        } else {
            console.log("\nAddress differs from Ethereum Mainnet");
            console.log("   This is a new deterministic address for ChainID 138");
        }
    }
}

