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

import {Script, console} from "forge-std/Script.sol";
import {Chain138MainnetCheckpoint} from "../../contracts/mainnet-checkpoint/Chain138MainnetCheckpoint.sol";
import {ExtensionIds} from "../../contracts/mainnet-checkpoint/libraries/ExtensionIds.sol";
import {ICheckpointExtension} from "../../contracts/mainnet-checkpoint/interfaces/ICheckpointExtension.sol";

/// @notice Register deployed extensions on the checkpoint hub (env: EXT_* addresses).
contract RegisterCheckpointExtensions is Script {
    function run() external {
        uint256 pk = vm.envUint("PRIVATE_KEY");
        address proxy = vm.envAddress("CHAIN138_MAINNET_CHECKPOINT_PROXY");
        Chain138MainnetCheckpoint hub = Chain138MainnetCheckpoint(proxy);

        vm.startBroadcast(pk);
        _register(hub, ExtensionIds.METRICS, vm.envAddress("EXT_METRICS"));
        _register(hub, ExtensionIds.CONTENT_URI, vm.envAddress("EXT_ATTESTATION_URI"));
        _register(hub, ExtensionIds.RATE_LIMIT, vm.envAddress("EXT_RATE_LIMIT"));
        _register(hub, ExtensionIds.VALIDATOR_SIG, vm.envAddress("EXT_VALIDATOR_SIG"));
        _register(hub, ExtensionIds.TOKEN_FILTER, vm.envAddress("EXT_TOKEN_FILTER"));
        _register(hub, ExtensionIds.CW_LINK, vm.envAddress("EXT_CW_LINK"));
        _register(hub, ExtensionIds.GOV_TIMELOCK, vm.envAddress("EXT_TIMELOCK"));
        _register(hub, ExtensionIds.MIRROR_DETAIL, vm.envAddress("EXT_MIRROR_DETAIL"));
        _register(hub, ExtensionIds.ZK_STATE_ROOT, vm.envAddress("EXT_ZK_STATE_ROOT"));
        _register(hub, ExtensionIds.PAYMASTER_HINT, vm.envAddress("EXT_PAYMASTER_HINT"));
        _register(hub, ExtensionIds.L2_ORACLE, vm.envAddress("EXT_L2_ORACLE"));
        _register(hub, ExtensionIds.BLOCK_ORACLE, vm.envAddress("EXT_BLOCK_ORACLE"));
        _register(hub, ExtensionIds.MIN_PAYMENT, vm.envAddress("EXT_MIN_PAYMENT"));
        vm.stopBroadcast();
    }

    function _register(Chain138MainnetCheckpoint hub, bytes32 id, address module) internal {
        if (module == address(0)) return;
        uint32 hooks = ICheckpointExtension(module).HOOK_BEFORE_SUBMIT()
            | ICheckpointExtension(module).HOOK_AFTER_SUBMIT()
            | ICheckpointExtension(module).HOOK_ON_CCIP()
            | ICheckpointExtension(module).HOOK_VERIFY_LEAF();
        hub.registerExtension(id, module, hooks);
        console.log("Registered", vm.toString(id), module);
    }
}
