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

/**
 * @title MonetaryFormulas
 * @notice Implementation of mandatory monetary formulas
 * @dev All formulas MUST be applied exactly as specified without modification
 * 
 * Formulas:
 * - Money Supply: M = C + D and M = MB × m
 * - Money Velocity: V = PQ / M
 * - Money Multiplier: m = 1 / r and m = (1 + c) / (r + c)
 */
library MonetaryFormulas {
    /**
     * @notice Calculate money supply: M = C + D
     * @dev M = Money Supply, C = Currency, D = Deposits
     * @param currency Currency component (C)
     * @param deposits Deposits component (D)
     * @return moneySupply Money supply (M)
     */
    function calculateMoneySupply(uint256 currency, uint256 deposits) internal pure returns (uint256 moneySupply) {
        return currency + deposits;
    }
    
    /**
     * @notice Calculate money supply: M = MB × m
     * @dev M = Money Supply, MB = Monetary Base, m = Money Multiplier
     * @param monetaryBase Monetary base (MB)
     * @param moneyMultiplier Money multiplier (m)
     * @return moneySupply Money supply (M)
     */
    function calculateMoneySupplyFromMultiplier(uint256 monetaryBase, uint256 moneyMultiplier) internal pure returns (uint256 moneySupply) {
        return (monetaryBase * moneyMultiplier) / 1e18; // Assuming multiplier in 18 decimals
    }
    
    /**
     * @notice Calculate money velocity: V = PQ / M
     * @dev V = Velocity, P = Price Level, Q = Quantity of Goods, M = Money Supply
     * @param priceLevel Price level (P)
     * @param quantityOfGoods Quantity of goods (Q)
     * @param moneySupply Money supply (M)
     * @return velocity Money velocity (V)
     */
    function calculateMoneyVelocity(uint256 priceLevel, uint256 quantityOfGoods, uint256 moneySupply) internal pure returns (uint256 velocity) {
        if (moneySupply == 0) {
            return 0;
        }
        return (priceLevel * quantityOfGoods) / moneySupply;
    }
    
    /**
     * @notice Calculate simple money multiplier: m = 1 / r
     * @dev m = Money Multiplier, r = Reserve Ratio
     * @param reserveRatio Reserve ratio (r) in basis points (e.g., 1000 = 10%)
     * @return moneyMultiplier Money multiplier (m) in 18 decimals
     */
    function calculateSimpleMoneyMultiplier(uint256 reserveRatio) internal pure returns (uint256 moneyMultiplier) {
        require(reserveRatio > 0, "MonetaryFormulas: reserve ratio must be positive");
        require(reserveRatio <= 10000, "MonetaryFormulas: reserve ratio cannot exceed 100%");
        
        // m = 1 / r, where r is in basis points
        // Convert to 18 decimals: m = (1e18 * 10000) / reserveRatio
        return (1e18 * 10000) / reserveRatio;
    }
    
    /**
     * @notice Calculate money multiplier with currency ratio: m = (1 + c) / (r + c)
     * @dev m = Money Multiplier, r = Reserve Ratio, c = Currency Ratio
     * @param reserveRatio Reserve ratio (r) in basis points
     * @param currencyRatio Currency ratio (c) in basis points
     * @return moneyMultiplier Money multiplier (m) in 18 decimals
     */
    function calculateMoneyMultiplierWithCurrency(uint256 reserveRatio, uint256 currencyRatio) internal pure returns (uint256 moneyMultiplier) {
        require(reserveRatio > 0, "MonetaryFormulas: reserve ratio must be positive");
        require(reserveRatio <= 10000, "MonetaryFormulas: reserve ratio cannot exceed 100%");
        require(currencyRatio <= 10000, "MonetaryFormulas: currency ratio cannot exceed 100%");
        
        // m = (1 + c) / (r + c), where r and c are in basis points
        // Convert to 18 decimals: m = (1e18 * (10000 + currencyRatio)) / (reserveRatio + currencyRatio)
        uint256 numerator = 1e18 * (10000 + currencyRatio);
        uint256 denominator = reserveRatio + currencyRatio;
        
        require(denominator > 0, "MonetaryFormulas: invalid denominator");
        return numerator / denominator;
    }
}
