contract ERC20DocumentRegistry is Ownable {
    using SafeMath for uint256;

    struct HashedDocument {
        uint256 timestamp;
        string documentUri;
    }

    // array of all documents 
    HashedDocument[] private _documents;

    event LogDocumentedAdded(string documentUri, uint256 indexed documentIndex);

    /**
    * @notice adds a document's uri from IPFS to the array
    * @param documentUri string
    */
    function addDocument(string calldata documentUri) external onlyOwner {
        require(bytes(documentUri).length > 0, "invalid documentUri");

        HashedDocument memory document = HashedDocument({
            timestamp: block.timestamp,
            documentUri: documentUri
        });

        _documents.push(document);

        emit LogDocumentedAdded(documentUri, _documents.length.sub(1));
    }

    /**
    * @notice fetch the latest document on the array
    * @return uint256, string, uint256 
    */
    function currentDocument() external view 
        returns (uint256 timestamp, string memory documentUri, uint256 index) {
            require(_documents.length > 0, "no documents exist");
            uint256 last = _documents.length.sub(1);

            HashedDocument storage document = _documents[last];
            return (document.timestamp, document.documentUri, last);
        }

    /**
    * @notice fetches a document's uri
    * @param documentIndex uint256
    * @return uint256, string, uint256 
    */
    function getDocument(uint256 documentIndex) external view
        returns (uint256 timestamp, string memory documentUri, uint256 index) {
            require(documentIndex < _documents.length, "invalid index");

            HashedDocument storage document = _documents[documentIndex];
            return (document.timestamp, document.documentUri, documentIndex);
        }

    /**
    * @notice return the total amount of documents in the array
    * @return uint256
    */
    function documentCount() external view returns (uint256) {
        return _documents.length;
    }
}
// solhint-disable not-rely-on-time
function getDocument(uint256 documentIndex) external view
    returns (uint256 timestamp, string memory documentUri, uint256 index) {
        require(documentIndex < _documents.length, "invalid index");

        HashedDocument storage document = _documents[documentIndex];
        return (document.timestamp, document.documentUri, documentIndex);
    }

/**
 * @notice fetches a document's uri
 * @param documentIndex uint256
 * @return uint256, string, uint256 
 */