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

import "@openzeppelin/contracts/access/AccessControl.sol";
import "../RWAStorage.sol";
import "../interfaces/IRWAStandardsRegistryFacet.sol";

/**
 * @title RWAStandardsRegistryFacet
 * @notice Enable future EIP/ERC facets (e.g. ERC-3643 partition facet) via diamond cut without hub redeploy.
 */
contract RWAStandardsRegistryFacet is AccessControl, IRWAStandardsRegistryFacet {
    bytes32 public constant STANDARDS_ADMIN_ROLE = keccak256("STANDARDS_ADMIN_ROLE");

    constructor(address admin) {
        _grantRole(DEFAULT_ADMIN_ROLE, admin);
        _grantRole(STANDARDS_ADMIN_ROLE, admin);
    }

    function enableStandard(bytes32 standardId, address facet) external onlyRole(STANDARDS_ADMIN_ROLE) {
        require(facet != address(0), "RWAStandards: facet");
        RWAStorage.layout().enabledStandards[standardId] = true;
        emit StandardEnabled(standardId, facet);
    }

    function disableStandard(bytes32 standardId) external onlyRole(STANDARDS_ADMIN_ROLE) {
        RWAStorage.layout().enabledStandards[standardId] = false;
        emit StandardDisabled(standardId);
    }

    function bindAssetStandardFacet(bytes32 assetId, bytes32 standardId, address facet)
        external
        onlyRole(STANDARDS_ADMIN_ROLE)
    {
        require(RWAStorage.layout().enabledStandards[standardId], "RWAStandards: disabled");
        require(facet != address(0), "RWAStandards: facet");
        RWAStorage.layout().assets[assetId].standardFacets[standardId] = facet;
        emit AssetStandardFacetBound(assetId, standardId, facet);
    }

    function isStandardEnabled(bytes32 standardId) external view returns (bool) {
        return RWAStorage.layout().enabledStandards[standardId];
    }

    function assetStandardFacet(bytes32 assetId, bytes32 standardId) external view returns (address) {
        return RWAStorage.layout().assets[assetId].standardFacets[standardId];
    }
}
