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

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

/**
 * @title DeployBridgeVaultDeterministic
 * @notice Deploy BridgeVault138 via CREATE2 with fixed salt for chains 1, 137, 56 (Ethereum, Polygon, BSC).
 * @dev Use identical ADMIN, POLICY_MANAGER, COMPLIANCE_REGISTRY on all three chains so the vault address is the same.
 * Env: PRIVATE_KEY, ADMIN, POLICY_MANAGER, COMPLIANCE_REGISTRY.
 * See docs/runbooks/MULTI_CHAIN_EXECUTION_DETERMINISTIC_DEPLOYMENT.md
 */
contract DeployBridgeVaultDeterministic is Script {
    uint256 constant SALT_BRIDGE_VAULT = uint256(keccak256("BridgeVault"));

    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address deployer = vm.addr(pk);
        address admin = vm.envOr("ADMIN", deployer);
        address policyManager = vm.envAddress("POLICY_MANAGER");
        address complianceRegistry = vm.envAddress("COMPLIANCE_REGISTRY");

        vm.startBroadcast(pk);

        CREATE2Factory factory = new CREATE2Factory();
        bytes memory bytecode = abi.encodePacked(
            type(BridgeVault138).creationCode,
            abi.encode(admin, policyManager, complianceRegistry)
        );
        address vaultAddr = factory.deploy(bytecode, SALT_BRIDGE_VAULT);
        console.log("BridgeVault138:", vaultAddr);
        console.log("CREATE2Factory:", address(factory));

        vm.stopBroadcast();
    }
}
