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

import "forge-std/Script.sol";
import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../../contracts/bridge/UniversalCCIPBridge.sol";
import "../../contracts/bridge/BridgeOrchestrator.sol";

contract DeployBridges is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address registry = vm.envAddress("UNIVERSAL_ASSET_REGISTRY");
        address ccipRouter = vm.envAddress("CCIP_ROUTER");
        
        vm.startBroadcast(pk);
        
        // UniversalCCIPBridge behind proxy (UUPS upgradeable)
        UniversalCCIPBridge bridgeImpl = new UniversalCCIPBridge();
        bytes memory bridgeInitData = abi.encodeCall(UniversalCCIPBridge.initialize, (registry, ccipRouter, deployer));
        ERC1967Proxy bridgeProxy = new ERC1967Proxy(address(bridgeImpl), bridgeInitData);
        address bridgeAddr = address(bridgeProxy);
        
        // BridgeOrchestrator behind proxy (UUPS upgradeable)
        BridgeOrchestrator orchestratorImpl = new BridgeOrchestrator();
        bytes memory orchestratorInitData = abi.encodeCall(BridgeOrchestrator.initialize, (registry, bridgeAddr, deployer));
        ERC1967Proxy orchestratorProxy = new ERC1967Proxy(address(orchestratorImpl), orchestratorInitData);
        address orchestratorAddr = address(orchestratorProxy);
        
        vm.stopBroadcast();
        
        console.log("UNIVERSAL_CCIP_BRIDGE=", bridgeAddr);
        console.log("BRIDGE_ORCHESTRATOR=", orchestratorAddr);
    }
}
