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

import "forge-std/Test.sol";
import "../../contracts/rwa/diamond/facets/RWAInstrumentFacet.sol";
import "../../contracts/rwa/diamond/facets/RWADocumentFacet.sol";
import "../../contracts/rwa/diamond/facets/RWAStandardsRegistryFacet.sol";
import "../../contracts/rwa/diamond/RWAStorage.sol";

contract RWADiamondFacetsTest is Test {
    RWAInstrumentFacet public instrument;
    RWADocumentFacet public documents;
    RWAStandardsRegistryFacet public standards;

    address public admin = address(0xA11CE);
    bytes32 public assetId = keccak256("ISIN-US0001-PRIVATE-CLOOP");

    function setUp() public {
        instrument = new RWAInstrumentFacet(admin);
        documents = new RWADocumentFacet(admin);
        standards = new RWAStandardsRegistryFacet(admin);
    }

    function test_privateClosedLoop_isinCusipHashes() public {
        bytes32 isinHash = keccak256("US1234567890");
        bytes32 cusipHash = keccak256("123456789");

        vm.prank(admin);
        instrument.setIssuanceMode(assetId, RWAStorage.IssuanceMode.PrivateClosedLoop);
        vm.prank(admin);
        instrument.setInstrumentIdentity(assetId, isinHash, cusipHash, bytes32(0), bytes32(0));

        assertEq(uint256(instrument.getIssuanceMode(assetId)), uint256(RWAStorage.IssuanceMode.PrivateClosedLoop));
    }

    function test_thirdPartyTokenization_pointer() public {
        address extToken = address(0xBEEF);

        vm.prank(admin);
        instrument.setIssuanceMode(assetId, RWAStorage.IssuanceMode.ThirdPartyTokenization);
        vm.prank(admin);
        instrument.setTokenPointer(assetId, extToken);

        assertEq(instrument.getTokenPointer(assetId), extToken);
    }

    function test_anchor_ipfs_and_filecoin_documents() public {
        bytes32 content = keccak256("prospectus-v1");

        vm.prank(admin);
        uint256 i0 = documents.anchorDocument(assetId, "ipfs://bafyProspectus", content);
        vm.prank(admin);
        uint256 i1 = documents.anchorDocument(assetId, "filecoin://f01234deal", content);

        assertEq(documents.documentCount(assetId), 2);
        (bytes32 h0, bytes32 u0, RWAStorage.UriScheme s0,) = documents.getDocument(assetId, i0);
        assertEq(h0, content);
        assertEq(uint256(s0), uint256(RWAStorage.UriScheme.IPFS));
        (, , RWAStorage.UriScheme s1,) = documents.getDocument(assetId, i1);
        assertEq(uint256(s1), uint256(RWAStorage.UriScheme.Filecoin));
        assertTrue(u0 != bytes32(0));
    }

    function test_enableFutureStandardFacet() public {
        bytes32 erc3643Id = keccak256("ERC-3643");
        address facet = address(uint160(0xFAC7));

        vm.prank(admin);
        standards.enableStandard(erc3643Id, facet);
        vm.prank(admin);
        standards.bindAssetStandardFacet(assetId, erc3643Id, facet);

        assertTrue(standards.isStandardEnabled(erc3643Id));
        assertEq(standards.assetStandardFacet(assetId, erc3643Id), facet);
    }
}
