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

import {Script, console} from "forge-std/Script.sol";
import {CWAssetReserveVerifier} from "../contracts/bridge/integration/CWAssetReserveVerifier.sol";

interface ICWMultiTokenBridgeL1AdminV2 {
    function setReserveVerifier(address newVerifier) external;
}

/**
 * @title DeployCWAssetReserveVerifier
 * @notice Deploy a generic reserve verifier for stable, monetary-unit, or gas-native c* lanes.
 *
 * Env:
 *   PRIVATE_KEY (required)
 *   CW_L1_BRIDGE or CHAIN138_L1_BRIDGE (required)
 *   CW_ASSET_RESERVE_VAULT (optional)
 *   CW_ASSET_RESERVE_SYSTEM (optional)
 *   CW_ATTACH_VERIFIER_TO_L1=1 (optional, default 0)
 */
contract DeployCWAssetReserveVerifier is Script {
    function run() external {
        uint256 privateKey = vm.envUint("PRIVATE_KEY");

        address l1Bridge = vm.envOr("CW_L1_BRIDGE", address(0));
        if (l1Bridge == address(0)) {
            l1Bridge = vm.envOr("CHAIN138_L1_BRIDGE", address(0));
        }
        if (l1Bridge == address(0)) {
            l1Bridge = vm.envOr("CW_L1_BRIDGE_CHAIN138", address(0));
        }

        address assetReserveVault = vm.envOr("CW_ASSET_RESERVE_VAULT", address(0));
        address reserveSystem = vm.envOr("CW_ASSET_RESERVE_SYSTEM", address(0));
        bool attachVerifierToBridge = vm.envOr("CW_ATTACH_VERIFIER_TO_L1", uint256(0)) == 1;

        require(l1Bridge != address(0), "CW_L1_BRIDGE or CHAIN138_L1_BRIDGE required");

        vm.startBroadcast(privateKey);

        CWAssetReserveVerifier verifier = new CWAssetReserveVerifier(
            vm.addr(privateKey),
            l1Bridge,
            assetReserveVault,
            reserveSystem
        );

        if (attachVerifierToBridge) {
            ICWMultiTokenBridgeL1AdminV2(l1Bridge).setReserveVerifier(address(verifier));
        }

        vm.stopBroadcast();

        console.log("CWAssetReserveVerifier:", address(verifier));
        console.log("  L1 bridge:", l1Bridge);
        console.log("  attached to bridge:", attachVerifierToBridge);
        console.log("  asset reserve vault:", assetReserveVault);
        console.log("  reserve system:", reserveSystem);
    }
}
