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

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "./interfaces/IXAUOracle.sol";
import "../oracle/IAggregator.sol";

/**
 * @title XAUOracle
 * @notice Multi-source oracle aggregator for ETH/XAU pricing with safety margins
 * @dev Aggregates prices from multiple Aggregator feeds
 * 
 * COMPLIANCE NOTES:
 * - XAU (Gold) is the ISO 4217 commodity code used as universal unit of account
 * - All currency conversions in the system MUST triangulate through XAU
 * - XAU is NOT legal tender, but serves as the normalization standard
 */
contract XAUOracle is IXAUOracle, AccessControl, Pausable {
    bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
    bytes32 public constant FEED_MANAGER_ROLE = keccak256("FEED_MANAGER_ROLE");

    struct PriceFeed {
        address feed;
        uint256 weight; // in basis points (10000 = 100%)
        bool active;
    }

    PriceFeed[] private _priceFeeds;
    mapping(address => uint256) private _feedIndex; // feed address => index + 1 (0 means not found)
    mapping(address => bool) private _isFeed;

    uint256 public constant BASIS_POINTS = 10000;
    uint256 public constant SAFETY_MARGIN_BPS = 500; // 5% safety margin for liquidations
    uint256 public constant PRICE_DECIMALS = 18;

    uint256 private _lastPrice;
    uint256 private _lastUpdate;

    constructor(address admin) {
        _grantRole(DEFAULT_ADMIN_ROLE, admin);
        _grantRole(ADMIN_ROLE, admin);
        _grantRole(FEED_MANAGER_ROLE, admin);
    }

    /**
     * @notice Get ETH price in XAU
     * @return price ETH price in XAU (18 decimals)
     * @return timestamp Last update timestamp
     */
    function getETHPriceInXAU() external view override returns (uint256 price, uint256 timestamp) {
        require(!paused(), "XAUOracle: paused");
        require(_lastUpdate > 0, "XAUOracle: no price data");
        
        return (_lastPrice, _lastUpdate);
    }

    /**
     * @notice Get liquidation price for a vault (with safety margin)
     * @param vault Vault address (not used in current implementation, reserved for future use)
     * @return price Liquidation threshold price in XAU
     */
    function getLiquidationPrice(address vault) external view override returns (uint256 price) {
        require(!paused(), "XAUOracle: paused");
        require(_lastUpdate > 0, "XAUOracle: no price data");
        
        // Apply safety margin: liquidation price = current price * (1 - safety margin)
        price = (_lastPrice * (BASIS_POINTS - SAFETY_MARGIN_BPS)) / BASIS_POINTS;
    }

    /**
     * @notice Update price from aggregated feeds
     * @dev Can be called by anyone, but typically called by keeper
     */
    function updatePrice() external whenNotPaused {
        require(_priceFeeds.length > 0, "XAUOracle: no feeds");

        uint256 totalWeight = 0;
        uint256 weightedSum = 0;

        for (uint256 i = 0; i < _priceFeeds.length; i++) {
            if (!_priceFeeds[i].active) continue;

            IAggregator feed = IAggregator(_priceFeeds[i].feed);
            (, int256 answer, , uint256 updatedAt, ) = feed.latestRoundData();
            
            require(answer > 0, "XAUOracle: invalid price");
            require(updatedAt > 0, "XAUOracle: stale price");
            
            // Check price staleness (30 seconds)
            require(block.timestamp - updatedAt <= 30, "XAUOracle: stale price");

            uint256 price = uint256(answer);
            // Convert from feed decimals to 18 decimals
            uint8 feedDecimals = feed.decimals();
            if (feedDecimals < PRICE_DECIMALS) {
                price = price * (10 ** (PRICE_DECIMALS - feedDecimals));
            } else if (feedDecimals > PRICE_DECIMALS) {
                price = price / (10 ** (feedDecimals - PRICE_DECIMALS));
            }

            weightedSum += price * _priceFeeds[i].weight;
            totalWeight += _priceFeeds[i].weight;
        }

        require(totalWeight > 0, "XAUOracle: no active feeds");
        
        uint256 aggregatedPrice = weightedSum / totalWeight;
        _lastPrice = aggregatedPrice;
        _lastUpdate = block.timestamp;

        emit PriceUpdated(aggregatedPrice, block.timestamp);
    }

    /**
     * @notice Add a price feed source
     * @param feed Price feed address (must implement Aggregator interface)
     * @param weight Weight for this feed (in basis points)
     */
    function addPriceFeed(address feed, uint256 weight) external onlyRole(FEED_MANAGER_ROLE) {
        require(feed != address(0), "XAUOracle: zero feed");
        require(weight > 0 && weight <= BASIS_POINTS, "XAUOracle: invalid weight");
        require(!_isFeed[feed], "XAUOracle: feed already exists");

        _priceFeeds.push(PriceFeed({
            feed: feed,
            weight: weight,
            active: true
        }));
        _feedIndex[feed] = _priceFeeds.length; // index + 1
        _isFeed[feed] = true;

        emit PriceFeedAdded(feed, weight);
    }

    /**
     * @notice Remove a price feed source
     * @param feed Price feed address to remove
     */
    function removePriceFeed(address feed) external onlyRole(FEED_MANAGER_ROLE) {
        require(_isFeed[feed], "XAUOracle: feed not found");

        uint256 index = _feedIndex[feed] - 1;
        _priceFeeds[index].active = false;
        _isFeed[feed] = false;
        _feedIndex[feed] = 0;

        emit PriceFeedRemoved(feed);
    }

    /**
     * @notice Update feed weight
     * @param feed Price feed address
     * @param weight New weight
     */
    function updateFeedWeight(address feed, uint256 weight) external onlyRole(FEED_MANAGER_ROLE) {
        require(_isFeed[feed], "XAUOracle: feed not found");
        require(weight > 0 && weight <= BASIS_POINTS, "XAUOracle: invalid weight");

        uint256 index = _feedIndex[feed] - 1;
        uint256 oldWeight = _priceFeeds[index].weight;
        _priceFeeds[index].weight = weight;

        emit FeedWeightUpdated(feed, oldWeight, weight);
    }

    /**
     * @notice Freeze oracle (emergency)
     */
    function freeze() external onlyRole(ADMIN_ROLE) {
        _pause();
        emit OracleFrozen(block.timestamp);
    }

    /**
     * @notice Unfreeze oracle
     */
    function unfreeze() external onlyRole(ADMIN_ROLE) {
        _unpause();
        emit OracleUnfrozen(block.timestamp);
    }

    /**
     * @notice Check if oracle is frozen
     * @return frozen True if frozen
     */
    function isFrozen() external view override returns (bool) {
        return paused();
    }

    /**
     * @notice Get all price feeds
     * @return feeds Array of price feed structs
     */
    function getPriceFeeds() external view returns (PriceFeed[] memory feeds) {
        return _priceFeeds;
    }
}
