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

/**
 * @title Compliance
 * @dev Smart contract for tracking compliance and audit requirements
 */
contract Compliance {
    enum ComplianceStatus {
        COMPLIANT,
        NON_COMPLIANT,
        PENDING_REVIEW,
        EXEMPTED
    }

    enum ComplianceFramework {
        GDPR,
        HIPAA,
        SOC2,
        ISO27001,
        CUSTOM
    }

    struct ComplianceRecord {
        string resourceId;
        ComplianceFramework framework;
        ComplianceStatus status;
        string findings;
        address reviewedBy;
        uint256 reviewedAt;
        uint256 createdAt;
    }

    mapping(string => ComplianceRecord[]) public complianceRecords;
    mapping(string => mapping(ComplianceFramework => ComplianceStatus)) public resourceCompliance;

    event ComplianceChecked(
        string indexed resourceId,
        ComplianceFramework framework,
        ComplianceStatus status,
        uint256 timestamp
    );

    event ComplianceReviewed(
        string indexed resourceId,
        ComplianceFramework framework,
        ComplianceStatus status,
        address indexed reviewedBy,
        uint256 timestamp
    );

    /**
     * @dev Record a compliance check
     */
    function recordComplianceCheck(
        string memory resourceId,
        ComplianceFramework framework,
        ComplianceStatus status,
        string memory findings
    ) public returns (bool) {
        ComplianceRecord memory record = ComplianceRecord({
            resourceId: resourceId,
            framework: framework,
            status: status,
            findings: findings,
            reviewedBy: address(0),
            reviewedAt: 0,
            createdAt: block.timestamp
        });

        complianceRecords[resourceId].push(record);
        resourceCompliance[resourceId][framework] = status;

        emit ComplianceChecked(resourceId, framework, status, block.timestamp);
        return true;
    }

    /**
     * @dev Review and update compliance status
     */
    function reviewCompliance(
        string memory resourceId,
        ComplianceFramework framework,
        ComplianceStatus status,
        string memory findings
    ) public {
        ComplianceRecord memory record = ComplianceRecord({
            resourceId: resourceId,
            framework: framework,
            status: status,
            findings: findings,
            reviewedBy: msg.sender,
            reviewedAt: block.timestamp,
            createdAt: block.timestamp
        });

        complianceRecords[resourceId].push(record);
        resourceCompliance[resourceId][framework] = status;

        emit ComplianceReviewed(resourceId, framework, status, msg.sender, block.timestamp);
    }

    /**
     * @dev Get compliance status for a resource and framework
     */
    function getComplianceStatus(
        string memory resourceId,
        ComplianceFramework framework
    ) public view returns (ComplianceStatus) {
        return resourceCompliance[resourceId][framework];
    }

    /**
     * @dev Get all compliance records for a resource
     */
    function getComplianceRecords(string memory resourceId) 
        public 
        view 
        returns (ComplianceRecord[] memory) 
    {
        return complianceRecords[resourceId];
    }
}

