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

contract ProvenanceRegistryV3 {
    struct Record {
        string contentId;      // SHA-256
        string fileName;       // image file
        string platformName;   // source platform
        string watermarkId;    // hidden watermark id
        string pHashValue;     // perceptual hash
        uint256 timestamp;
        bool exists;
    }

    mapping(string => Record) private byContentId;
    mapping(string => string) private wmToContentId;
    string[] private allContentIds;

    event RecordRegistered(
        string contentId,
        string fileName,
        string platformName,
        string watermarkId,
        string pHashValue,
        uint256 timestamp
    );

    function registerRecord(
        string memory contentId,
        string memory fileName,
        string memory platformName,
        string memory watermarkId,
        string memory pHashValue
    ) public {
        require(!byContentId[contentId].exists, "Record already exists");

        byContentId[contentId] = Record({
            contentId: contentId,
            fileName: fileName,
            platformName: platformName,
            watermarkId: watermarkId,
            pHashValue: pHashValue,
            timestamp: block.timestamp,
            exists: true
        });

        wmToContentId[watermarkId] = contentId;
        allContentIds.push(contentId);

        emit RecordRegistered(
            contentId,
            fileName,
            platformName,
            watermarkId,
            pHashValue,
            block.timestamp
        );
    }

    function getRecordByContentId(string memory contentId)
        public
        view
        returns (
            string memory,
            string memory,
            string memory,
            string memory,
            string memory,
            uint256,
            bool
        )
    {
        Record memory r = byContentId[contentId];
        return (
            r.contentId,
            r.fileName,
            r.platformName,
            r.watermarkId,
            r.pHashValue,
            r.timestamp,
            r.exists
        );
    }

    function getContentIdByWatermarkId(string memory watermarkId)
        public
        view
        returns (string memory)
    {
        return wmToContentId[watermarkId];
    }

    function getAllContentIds() public view returns (string[] memory) {
        return allContentIds;
    }
}
