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

import {CheckpointExtensionBase} from "./CheckpointExtensionBase.sol";
import {CheckpointStorage} from "../storage/CheckpointStorage.sol";
import {CheckpointLeaf} from "../libraries/CheckpointLeaf.sol";
import {IChain138MainnetCheckpoint} from "../interfaces/IChain138MainnetCheckpoint.sol";

/// @notice Enforces hub minPaymentValueWei against payment leaves in extensionData.
contract MinPaymentValueExtension is CheckpointExtensionBase {
    IChain138MainnetCheckpoint public hub;

    function setHub(address hubAddress) external {
        hub = IChain138MainnetCheckpoint(hubAddress);
    }

    function HOOK_BEFORE_SUBMIT() external pure override returns (uint32) {
        return 1 << 0;
    }

    function HOOK_AFTER_SUBMIT() external pure override returns (uint32) {
        return 0;
    }

    function HOOK_ON_CCIP() external pure override returns (uint32) {
        return 0;
    }

    function beforeSubmit(CheckpointStorage.CheckpointHeader calldata header, bytes calldata data) external view {
        (, , uint256 minWei,,,) = hub.getConfig();
        if (minWei == 0 || data.length == 0) return;
        CheckpointLeaf.PaymentLeafV1[] memory leaves = abi.decode(data, (CheckpointLeaf.PaymentLeafV1[]));
        for (uint256 i = 0; i < leaves.length; i++) {
            require(leaves[i].value >= minWei, "below min");
        }
        header;
    }

    function afterSubmit(CheckpointStorage.CheckpointHeader calldata, bytes calldata) external pure override {}

    function onCCIPReceive(bytes calldata) external pure override {}
}
