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

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

/// @notice Deploy CREATE2Factory when missing on target chain (one-time CREATE).
contract DeployCREATE2FactoryIfNeeded is Script {
    function run() external {
        address existing = vm.envOr("CREATE2_FACTORY_ADDRESS", address(0));
        if (existing != address(0) && existing.code.length > 0) {
            console2.log("CREATE2Factory already live:", existing);
            return;
        }

        vm.startBroadcast(vm.envUint("PRIVATE_KEY"));
        CREATE2Factory factory = new CREATE2Factory();
        vm.stopBroadcast();
        console2.log("CREATE2Factory deployed:", address(factory));
    }
}
