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

import "../../dex/DODOPMMIntegration.sol";

/**
 * @title PMMPriceProvider
 * @notice Provides asset price in quote token using DODO PMM pool (oracle-backed when configured)
 * @dev Used for vault collateral valuation or off-chain reporting when asset is a PMM pair token.
 *      Ledger uses XAUOracle for primary valuation; this adapter can be used by a future
 *      Ledger extension or by keepers/UIs to get PMM-based price (e.g. cUSDT/cUSDC in USD terms).
 */
contract PMMPriceProvider {
    DODOPMMIntegration public immutable pmmIntegration;

    constructor(address pmmIntegration_) {
        require(pmmIntegration_ != address(0), "PMMPriceProvider: zero address");
        pmmIntegration = DODOPMMIntegration(pmmIntegration_);
    }

    /**
     * @notice Get price of asset in terms of quoteToken (18 decimals: quoteToken per 1 unit of asset)
     * @param asset Asset to price (e.g. cUSDT)
     * @param quoteToken Quote token (e.g. USDT or cUSDC)
     * @return price Price in 18 decimals (1e18 = 1:1 for stablecoins); 0 if no pool
     */
    function getPrice(address asset, address quoteToken) external view returns (uint256 price) {
        if (asset == quoteToken) return 1e18;

        address pool = pmmIntegration.pools(asset, quoteToken);
        if (pool == address(0)) {
            pool = pmmIntegration.pools(quoteToken, asset);
            if (pool == address(0)) return 0;
            // asset is quote, quoteToken is base: price = 1e36 / basePerQuote
            uint256 basePerQuote = pmmIntegration.getPoolPriceOrOracle(pool);
            if (basePerQuote == 0) return 0;
            return (1e36) / basePerQuote;
        }
        // asset is base, quoteToken is quote
        return pmmIntegration.getPoolPriceOrOracle(pool);
    }
}
