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

import {Script} from "forge-std/Script.sol";
import {CheckpointHubConfig} from "../../contracts/mainnet-checkpoint/libraries/CheckpointHubConfig.sol";
import {BatchEmitterConfig} from "../../contracts/mainnet-checkpoint/libraries/BatchEmitterConfig.sol";

/// @notice Shared env → config parsing for deploy/configure scripts.
abstract contract CheckpointConfigScript is Script {
    function hubConfigFromEnv() internal view returns (CheckpointHubConfig.HubConfig memory cfg) {
        cfg = CheckpointHubConfig.mainnetDefaults();
        cfg.batchSize = uint16(vm.envOr("CHECKPOINT_BATCH_SIZE", uint256(cfg.batchSize)));
        cfg.maxBatchWaitSeconds = uint32(vm.envOr("CHECKPOINT_MAX_WAIT_SECONDS", uint256(cfg.maxBatchWaitSeconds)));
        cfg.minPaymentValueWei = vm.envOr("CHECKPOINT_MIN_PAYMENT_VALUE_WEI", uint256(0));
        cfg.requireValidatorSigs = vm.envOr("CHECKPOINT_REQUIRE_VALIDATOR_SIGS", cfg.requireValidatorSigs);
        cfg.allowCalldataOnlySubmit = vm.envOr("CHECKPOINT_ALLOW_CALLDATA_ONLY", cfg.allowCalldataOnlySubmit);
        cfg.allowCCIPIngress = vm.envOr("CHECKPOINT_ALLOW_CCIP_INGRESS", cfg.allowCCIPIngress);
        cfg.enforcePreviousBatchId = vm.envOr("CHECKPOINT_ENFORCE_PREVIOUS_BATCH_ID", cfg.enforcePreviousBatchId);
        cfg.ccipRouter = vm.envOr("CCIP_ROUTER_MAINNET", address(0));
        cfg.sourceChainSelector = uint64(vm.envOr("CCIP_CHAIN_SELECTOR_138", uint256(0)));
        cfg.batchEmitterOnSource = vm.envOr("CHAIN138_BATCH_EMITTER", address(0));
        cfg.legacyMirrorV1 = vm.envOr("TRANSACTION_MIRROR_MAINNET", address(0));
        cfg.legacyTetherV1 = vm.envOr("MAINNET_TETHER_ADDRESS", address(0));
        cfg.submitterAttestationSigner = vm.envOr("CHECKPOINT_ATTESTATION_SIGNER", address(0));
        CheckpointHubConfig.validate(cfg);
    }

    function emitterConfigFromEnv() internal view returns (BatchEmitterConfig.EmitterConfig memory cfg) {
        cfg.ccipRouter = vm.envAddress("CCIP_ROUTER_CHAIN138");
        cfg.linkToken = vm.envAddress("LINK_TOKEN_CHAIN138");
        cfg.mainnetChainSelector = uint64(vm.envUint("CCIP_CHAIN_SELECTOR_MAINNET"));
        cfg.mainnetCheckpoint = vm.envAddress("CHAIN138_MAINNET_CHECKPOINT_PROXY");
        BatchEmitterConfig.validate(cfg);
    }
}
