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

import "../KeyVault.sol";

/// @dev Malicious contract that tries to re-enter KeyVault during execute()
contract ReentrancyAttacker {
    KeyVault public target;
    uint256 public attackCount;
    bool public attacking;

    constructor(address _target) {
        target = KeyVault(payable(_target));
    }

    /// @dev When this contract receives ETH, it tries to re-enter execute()
    receive() external payable {
        if (attacking && attackCount < 3) {
            attackCount++;
            // Try to re-enter
            try target.execute(address(this), 0.01 ether, "") {
                // If this succeeds, the reentrancy guard is broken
            } catch {
                // Expected: should revert with "ReentrancyGuard: reentrant call"
            }
        }
    }

    function startAttack() external {
        attacking = true;
        attackCount = 0;
    }

    function stopAttack() external {
        attacking = false;
    }
}
