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

/**
 * @title RWAStorage
 * @notice ERC-7201 namespaced app storage for RWA ERC-2535 facets (upgrade-safe, expandable).
 * @dev Attach facets to M00 Diamond or a dedicated RWAM00Diamond; do not collide with GRUStorage slots.
 */
library RWAStorage {
    /// @custom:storage-location erc7201:dbis.storage.RWA
    bytes32 private constant RWA_STORAGE_LOCATION =
        0x8a3c9e1f4b2d7065c8e0a1f3d9b4e7c2a5f8d1b6e3c9a0f4d7b2e5c8a1f3d900;

    /// @notice How the instrument is issued — not a public market security offering by default.
    enum IssuanceMode {
        Unset,
        ThirdPartyTokenization,
        PrivateClosedLoop,
        CommodityIndex,
        InternalLedgerOnly
    }

    /// @notice Off-chain payload scheme (URI stored off-chain; on-chain: hash + scheme id).
    enum UriScheme {
        None,
        IPFS,
        Filecoin,
        Arweave,
        HTTPS,
        InternalDB
    }

    struct DocumentRef {
        bytes32 contentHash;
        bytes32 uriHash;
        UriScheme scheme;
        uint64 updatedAt;
    }

    struct InstrumentIdentity {
        bytes32 isinHash;
        bytes32 cusipHash;
        bytes32 issuerLeiHash;
        bytes32 underlyingIsinHash;
    }

    struct AssetRecord {
        address tokenPointer;
        IssuanceMode issuanceMode;
        InstrumentIdentity identity;
        bytes32 primaryContentHash;
        DocumentRef[] documents;
        mapping(bytes32 standardId => address) standardFacets;
    }

    struct Layout {
        mapping(bytes32 assetId => AssetRecord) assets;
        mapping(bytes32 standardId => bool) enabledStandards;
        address governance;
        address documentAdmin;
    }

    function layout() internal pure returns (Layout storage l) {
        bytes32 slot = RWA_STORAGE_LOCATION;
        assembly {
            l.slot := slot
        }
    }
}
