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

import {Script, console} from "forge-std/Script.sol";
import {CCIPRelayRouter} from "../contracts/relay/CCIPRelayRouter.sol";
import {CCIPRelayBridge} from "../contracts/relay/CCIPRelayBridge.sol";

/**
 * @title Deploy CCIP Relay Infrastructure
 * @notice Deploys relay router and bridge on destination chain (Ethereum Mainnet)
 */
contract DeployCCIPRelay is Script {
    function run() external {
        uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(deployerPrivateKey);
        
        // Get configuration
        address weth9 = vm.envAddress("WETH9_MAINNET"); // Ethereum Mainnet WETH9
        address relayer = vm.envAddress("RELAYER_ADDRESS"); // Address that will run relay service
        
        console.log("Deploying CCIP Relay Infrastructure:");
        console.log("  Deployer:", deployer);
        console.log("  WETH9:", weth9);
        console.log("  Relayer:", relayer);
        
        vm.startBroadcast(deployerPrivateKey);
        
        // Deploy Relay Router
        CCIPRelayRouter relayRouter = new CCIPRelayRouter();
        console.log("CCIPRelayRouter deployed at:", address(relayRouter));
        
        // Deploy Relay Bridge
        CCIPRelayBridge relayBridge = new CCIPRelayBridge(weth9, address(relayRouter));
        console.log("CCIPRelayBridge deployed at:", address(relayBridge));
        
        // Authorize bridge in router
        relayRouter.authorizeBridge(address(relayBridge));
        console.log("Bridge authorized in router");
        
        // Grant relayer role
        if (relayer != address(0)) {
            relayRouter.grantRelayerRole(relayer);
            console.log("Relayer role granted to:", relayer);
        }
        
        vm.stopBroadcast();
        
        console.log("\n=== Deployment Summary ===");
        console.log("CCIPRelayRouter:", address(relayRouter));
        console.log("CCIPRelayBridge:", address(relayBridge));
        console.log("Relayer:", relayer);
        console.log("\nNext steps:");
        console.log("1. Update .env with relay router and bridge addresses");
        console.log("2. Start the relay service");
    }
}

