// Certora Specification for ChallengeManager
// Verifies challenge window, fraud proof verification, and finalization logic

using ChallengeManager as CM;
using BondManager as BM;

// Import required contracts
import "../contracts/bridge/trustless/ChallengeManager.sol";
import "../contracts/bridge/trustless/BondManager.sol";

// ============================================================================
// INVARIANTS
// ============================================================================

// Invariant: Claim cannot be both finalized and challenged
invariant claimStateExclusive(uint256 depositId)
    CM.claims(depositId).finalized == false || CM.claims(depositId).challenged == false;

// Invariant: Challenge window end is always in the future when claim is registered
invariant challengeWindowFuture(uint256 depositId)
    CM.claims(depositId).depositId == 0 || 
    CM.claims(depositId).challengeWindowEnd > CM.claims(depositId).depositId; // Simplified check

// ============================================================================
// RULES FOR registerClaim
// ============================================================================

// Rule: Challenge window is set correctly
rule challengeWindowSet(uint256 depositId, address asset, uint256 amount, address recipient) {
    env e;
    uint256 currentTime = e.block.timestamp;
    
    CM.registerClaim(e, depositId, asset, amount, recipient);
    
    if (!lastReverted) {
        uint256 windowEnd = CM.claims(depositId).challengeWindowEnd;
        assert windowEnd == currentTime + CM.challengeWindow();
        assert windowEnd > currentTime;
    }
}

// Rule: Claim cannot be registered twice
rule noDuplicateClaims(uint256 depositId, address asset1, address asset2, uint256 amount1, uint256 amount2, address recipient) {
    env e1, e2;
    
    // First registration succeeds
    CM.registerClaim(e1, depositId, asset1, amount1, recipient);
    assume !lastReverted;
    
    // Second registration must fail (handled by ChallengeManager logic)
    // Note: This is enforced in the contract, verify it works
}

// ============================================================================
// RULES FOR challengeClaim
// ============================================================================

// Rule: Cannot challenge after window expires
rule cannotChallengeAfterWindow(uint256 depositId, uint8 proofType, bytes proof) {
    env e1, e2;
    
    // Register claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // Advance time past window
    e2.block.timestamp = CM.claims(depositId).challengeWindowEnd + 1;
    
    // Challenge must fail
    CM.challengeClaim@withrevert(e2, depositId, proofType, proof);
    assert lastReverted;
}

// Rule: Cannot challenge finalized claim
rule cannotChallengeFinalized(uint256 depositId, uint8 proofType, bytes proof) {
    env e1, e2, e3;
    
    // Register and finalize claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    e2.block.timestamp = CM.claims(depositId).challengeWindowEnd + 1;
    CM.finalizeClaim(e2, depositId);
    assume !lastReverted;
    
    // Challenge must fail
    CM.challengeClaim@withrevert(e3, depositId, proofType, proof);
    assert lastReverted;
}

// Rule: Cannot challenge already challenged claim
rule cannotChallengeTwice(uint256 depositId, uint8 proofType1, uint8 proofType2, bytes proof1, bytes proof2) {
    env e1, e2;
    
    // Register claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // First challenge succeeds (assuming valid proof)
    CM.challengeClaim(e1, depositId, proofType1, proof1);
    // Note: May revert if proof invalid, but if it succeeds...
    
    // Second challenge must fail
    CM.challengeClaim@withrevert(e2, depositId, proofType2, proof2);
    // If first challenge succeeded, second must fail
}

// Rule: Slashing triggered on valid challenge
rule slashingOnChallenge(uint256 depositId, uint8 proofType, bytes proof) {
    env e1, e2;
    
    // Register claim (assumes bond already posted)
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // Challenge with valid proof
    CM.challengeClaim@withrevert(e2, depositId, proofType, proof);
    
    // If challenge succeeds, bond should be slashed
    // Note: This depends on fraud proof verification logic
}

// ============================================================================
// RULES FOR finalizeClaim
// ============================================================================

// Rule: Cannot finalize before window expires
rule cannotFinalizeBeforeWindow(uint256 depositId) {
    env e1, e2;
    
    // Register claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // Try to finalize before window expires
    e2.block.timestamp = CM.claims(depositId).challengeWindowEnd - 1;
    CM.finalizeClaim@withrevert(e2, depositId);
    assert lastReverted;
}

// Rule: Cannot finalize challenged claim
rule cannotFinalizeChallenged(uint256 depositId, uint8 proofType, bytes proof) {
    env e1, e2, e3;
    
    // Register and challenge claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    CM.challengeClaim(e2, depositId, proofType, proof);
    // If challenge succeeds...
    
    // Finalization must fail
    e3.block.timestamp = CM.claims(depositId).challengeWindowEnd + 1;
    CM.finalizeClaim@withrevert(e3, depositId);
    // Should fail if challenged
}

// Rule: Cannot finalize twice
rule cannotFinalizeTwice(uint256 depositId) {
    env e1, e2, e3;
    
    // Register claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // First finalization succeeds
    e2.block.timestamp = CM.claims(depositId).challengeWindowEnd + 1;
    CM.finalizeClaim(e2, depositId);
    assume !lastReverted;
    
    // Second finalization must fail
    CM.finalizeClaim@withrevert(e3, depositId);
    assert lastReverted;
}

// Rule: Finalization sets finalized flag
rule finalizationSetsFlag(uint256 depositId) {
    env e1, e2;
    
    // Register claim
    CM.registerClaim(e1, depositId, address(0), 1 ether, address(0x1234));
    assume !lastReverted;
    
    // Finalize
    e2.block.timestamp = CM.claims(depositId).challengeWindowEnd + 1;
    CM.finalizeClaim(e2, depositId);
    
    if (!lastReverted) {
        assert CM.claims(depositId).finalized == true;
    }
}

// ============================================================================
// RULES FOR finalizeClaimsBatch
// ============================================================================

// Rule: Batch finalization respects same rules as single
rule batchFinalizationRules(uint256[] depositIds) {
    env e;
    
    // Batch finalization should only finalize valid claims
    CM.finalizeClaimsBatch(e, depositIds);
    
    // Each finalized claim must have passed window and not be challenged
    // This is enforced by the contract logic
}

// ============================================================================
// REENTRANCY PROTECTION
// ============================================================================

// Rule: No reentrancy in challengeClaim
rule noReentrancyChallenge(uint256 depositId, uint8 proofType, bytes proof) {
    env e;
    CM.challengeClaim(e, depositId, proofType, proof);
}

// Rule: No reentrancy in finalizeClaim
rule noReentrancyFinalize(uint256 depositId) {
    env e;
    CM.finalizeClaim(e, depositId);
}

