// 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 {WETH10} from "../contracts/tokens/WETH10.sol";
import {CCIPWETH9Bridge} from "../contracts/ccip/CCIPWETH9Bridge.sol";
import {CCIPWETH10Bridge} from "../contracts/ccip/CCIPWETH10Bridge.sol";

/**
 * @title Deploy WETH9, WETH10, and CCIP Bridges
 * @notice Complete deployment script for WETH contracts with CCIP cross-chain support
 */
contract DeployWETHWithCCIP is Script {
    struct DeploymentConfig {
        address ccipRouter;
        address feeToken; // LINK token
        bool deployWETH9;
        bool deployWETH10;
        bool deployBridges;
    }
    
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        // Get configuration from environment
        DeploymentConfig memory config = DeploymentConfig({
            ccipRouter: vm.envAddress("CCIP_ROUTER"),
            feeToken: vm.envAddress("CCIP_FEE_TOKEN"),
            deployWETH9: vm.envBool("DEPLOY_WETH9"),
            deployWETH10: vm.envBool("DEPLOY_WETH10"),
            deployBridges: vm.envBool("DEPLOY_BRIDGES")
        });
        
        console.log("=== WETH with CCIP Deployment ===");
        console.log("Deployer:", deployer);
        console.log("CCIP Router:", config.ccipRouter);
        console.log("Fee Token (LINK):", config.feeToken);
        console.log("Deploy WETH9:", config.deployWETH9);
        console.log("Deploy WETH10:", config.deployWETH10);
        console.log("Deploy Bridges:", config.deployBridges);
        
        vm.startBroadcast(deployerPrivateKey);
        
        address weth9Address = address(0);
        address weth10Address = address(0);
        address weth9BridgeAddress = address(0);
        address weth10BridgeAddress = address(0);
        
        // Deploy WETH9
        if (config.deployWETH9) {
            console.log("\n--- Deploying WETH9 ---");
            WETH weth9 = new WETH();
            weth9Address = address(weth9);
            console.log("WETH9 deployed at:", weth9Address);
            console.log("WETH9 name:", weth9.name());
            console.log("WETH9 symbol:", weth9.symbol());
        }
        
        // Deploy WETH10
        if (config.deployWETH10) {
            console.log("\n--- Deploying WETH10 ---");
            WETH10 weth10 = new WETH10();
            weth10Address = address(weth10);
            console.log("WETH10 deployed at:", weth10Address);
            console.log("WETH10 name:", weth10.name());
            console.log("WETH10 symbol:", weth10.symbol());
            console.log("WETH10 decimals:", weth10.decimals());
        }
        
        // Deploy Bridges
        if (config.deployBridges) {
            // Use deployed addresses or environment variables
            address weth9 = config.deployWETH9 ? weth9Address : vm.envAddress("WETH9_ADDRESS");
            address weth10 = config.deployWETH10 ? weth10Address : vm.envAddress("WETH10_ADDRESS");
            
            require(weth9 != address(0), "WETH9 address required");
            require(weth10 != address(0), "WETH10 address required");
            
            // Deploy WETH9 Bridge
            console.log("\n--- Deploying CCIPWETH9Bridge ---");
            CCIPWETH9Bridge weth9Bridge = new CCIPWETH9Bridge(
                config.ccipRouter,
                weth9,
                config.feeToken
            );
            weth9BridgeAddress = address(weth9Bridge);
            console.log("CCIPWETH9Bridge deployed at:", weth9BridgeAddress);
            
            // Deploy WETH10 Bridge
            console.log("\n--- Deploying CCIPWETH10Bridge ---");
            CCIPWETH10Bridge weth10Bridge = new CCIPWETH10Bridge(
                config.ccipRouter,
                weth10,
                config.feeToken
            );
            weth10BridgeAddress = address(weth10Bridge);
            console.log("CCIPWETH10Bridge deployed at:", weth10BridgeAddress);
        }
        
        console.log("\n=== Deployment Summary ===");
        console.log("WETH9:", weth9Address);
        console.log("WETH10:", weth10Address);
        console.log("CCIPWETH9Bridge:", weth9BridgeAddress);
        console.log("CCIPWETH10Bridge:", weth10BridgeAddress);
        
        vm.stopBroadcast();
    }
}

