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

import { V3Fixtures } from "../helpers/V3Fixtures.sol";
import { AccountabilityBondV3Safe } from "../../contracts/bonds/AccountabilityBondV3Safe.sol";

/// @notice Regression for the signed-term expiry boundary on accountability bonds.
/// @dev Before the validation below, a relayer could submit mutually signed terms
///      after their active period had started, then the company could fund and
///      activate an already-running (or already-ended) bond immediately.
contract AccountabilityBondLifecycleExpiryV3Test is V3Fixtures {
    AccountabilityBondV3Safe internal accountability;

    function setUp() public {
        _setUpDependencies();
        accountability = new AccountabilityBondV3Safe(
            owner, address(consensus), address(disputeRegistry), 10 ether, 100 ether, address(0)
        );
    }

    function test_signedTermsCannotBeCreatedAfterTheirActivePeriodStarts() external {
        AccountabilityBondV3Safe.Terms memory terms = _accountabilityTerms(1, 1 ether);
        terms.activePeriodStart = uint64(block.timestamp);
        terms.activePeriodEnd = uint64(block.timestamp + 1 days);

        bytes memory companySig = _signAccountability(accountability, terms, COMPANY_KEY);
        bytes memory counterpartySig = _signAccountability(accountability, terms, COUNTERPARTY_KEY);

        vm.expectRevert(AccountabilityBondV3Safe.EnforcementRequired.selector);
        accountability.createBond(terms, companySig, counterpartySig);
        assertEq(accountability.nextBondId(), 1, "stale signed terms consumed a bond id");
        assertFalse(accountability.nonceUsed(company, terms.nonce), "stale signed terms consumed company nonce");
        assertFalse(
            accountability.nonceUsed(counterparty, terms.nonce), "stale signed terms consumed counterparty nonce"
        );
    }
}
