// File: src/EsEMBRRewardsDistributor.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

import "./IEsEMBR.sol";
import "solmate/auth/Owned.sol";

contract EsEMBRRewardsDistributor is Owned {
    IEsEMBR public esEmbr;

    uint256 public emissionPerSecondEth; // Max esEMBR emissions per second for ETH stakers
    uint256 public emissionPerSecondEmbr; // Max esEMBR emissions per second for EMBR stakers

    uint256 private lastEmissionTimeEth;
    uint256 private lastEmissionTimeEmbr;

    constructor() Owned(msg.sender) { }

    modifier onlyEsEMBR() {
        require(msg.sender == address(esEmbr), "EsEMBRRewardsDistributor: Only esEMBR contract can call this function");
        _;
    }

    function setEsEMBR(address payable _esEmbr) external onlyOwner {
        esEmbr = IEsEMBR(_esEmbr);
    }

    function setEmissionPerSecondEth(uint256 amount) external onlyOwner {
        esEmbr.updateRewardsEthForAll();

        emissionPerSecondEth = amount;
        lastEmissionTimeEth = block.timestamp;
    }

    function setEmissionPerSecondEmbr(uint256 amount) external onlyOwner {
        esEmbr.updateRewardsEmbrForAll();

        emissionPerSecondEmbr = amount;
        lastEmissionTimeEmbr = block.timestamp;
    }

    function _clamp(uint256 value, uint256 max) internal pure returns(uint256) {
        return value > max ? max : value;
    }

    function distributeForEth() onlyEsEMBR external returns (uint256) {
        uint256 tokens_to_emit = pendingForEth();
        if (tokens_to_emit == 0) return 0;

        lastEmissionTimeEth = block.timestamp;

        return _clamp(tokens_to_emit, esEmbr.rewardsLeft());
    }

    function distributeForEmbr() onlyEsEMBR external returns (uint256) {
        uint256 tokens_to_emit = pendingForEmbr();
        if (tokens_to_emit == 0) return 0;

        lastEmissionTimeEmbr = block.timestamp;

        return _clamp(tokens_to_emit, esEmbr.rewardsLeft());
    }

    function pendingForEth() public view returns (uint256) {
        if (lastEmissionTimeEth == block.timestamp) return 0;

        return (block.timestamp - lastEmissionTimeEth) * emissionPerSecondEth;
    }

    function pendingForEmbr() public view returns (uint256) {
        if (lastEmissionTimeEmbr == block.timestamp) return 0;

        return (block.timestamp - lastEmissionTimeEmbr) * emissionPerSecondEmbr;
    }
}

// File: src/IEsEMBR.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

import "./IVester.sol";
import "./IERC20.sol";

interface IEsEMBR {
    function updateRewardsEthForAll() external;
    function updateRewardsEmbrForAll() external;
    function addVester(uint timeframe, IVester vester) external;
    function reward(address recipient, uint amount) external;

    function claimable(address _address) external view returns (uint256);
    function claim() external returns (uint256);

    function claimableRevShare(address _address) external view returns (uint256);
    function claimRevShare() external returns (uint256);
    function claimableEMBR(address addy, uint256[] calldata timeframes) external view returns (uint256);

    function batchCollectVested(uint[] calldata timeframes) external returns (uint);
    function collectVested(uint timeframe) external returns (uint);

    function rewardsLeft() external returns (uint);
}

// File: lib/solmate/src/auth/Owned.sol



// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Simple single owner authorization mixin.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/auth/Owned.sol)
abstract contract Owned {
    /*//////////////////////////////////////////////////////////////
                                 EVENTS
    //////////////////////////////////////////////////////////////*/

    event OwnershipTransferred(address indexed user, address indexed newOwner);

    /*//////////////////////////////////////////////////////////////
                            OWNERSHIP STORAGE
    //////////////////////////////////////////////////////////////*/

    address public owner;

    modifier onlyOwner() virtual {
        require(msg.sender == owner, "UNAUTHORIZED");

        _;
    }

    /*//////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(address _owner) {
        owner = _owner;

        emit OwnershipTransferred(address(0), _owner);
    }

    /*//////////////////////////////////////////////////////////////
                             OWNERSHIP LOGIC
    //////////////////////////////////////////////////////////////*/

    function transferOwnership(address newOwner) public virtual onlyOwner {
        owner = newOwner;

        emit OwnershipTransferred(msg.sender, newOwner);
    }
}

// File: src/IVester.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

interface IVester {
    function vest(address user, uint256 amount) external; // called by esembr contract only. This is where esembr contract will transfer the vested embr amount to the vester.
    function claimable(address user) external view returns (uint256 /* claimable amount */, uint256 /* entry time */);
    function claim(address user) external returns (uint256); // called by esembr contract only
    function vestingTime() external view returns (uint256);
    function vestingAmount(address) external view returns (uint256);
}

// File: src/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.19;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}