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

import { PartnershipBondV3Safe } from "../../contracts/bonds/PartnershipBondV3Safe.sol";
import { TaskEscrowV3 } from "../../contracts/escrow/TaskEscrowV3.sol";
import { V3Fixtures } from "../helpers/V3Fixtures.sol";

/// @title Inv09_TimeBoundedTerminalPath
/// @notice Every bond/escrow/task has a deadline; forceExit unlocks principal after deadline.
contract Inv09_TimeBoundedTerminalPath is V3Fixtures {
    PartnershipBondV3Safe internal bonds;
    TaskEscrowV3 internal escrow;
    uint256 internal bondId;
    address internal creator = address(0xC0FFEE);

    function setUp() public {
        _setUpDependencies();
        bonds = new PartnershipBondV3Safe(
            owner, address(0), address(consensus), address(disputeRegistry), 10 ether, 20 ether, 100 ether, address(0)
        );
        bondId = _createPartnership(bonds, _partnershipTerms(9, 1 ether, 1 ether));
        _fundActivePartnership(bonds, bondId, 1 ether, 1 ether);
        escrow = new TaskEscrowV3(owner, address(0xCAFE), 0, 100 ether, 1_000 ether, 1 days);
    }

    function test_expired_bond_has_permissionless_force_exit_that_credits_principal() external {
        vm.warp(block.timestamp + 30 days);
        bonds.markExpired(bondId);
        bonds.forceExit(bondId);
        assertEq(bonds.creditOf(address(0), partyA), 1 ether);
        assertEq(bonds.creditOf(address(0), partyB), 1 ether);
    }

    function test_unassigned_task_has_permissionless_deadline_refund() external {
        vm.deal(creator, 1 ether);
        vm.prank(creator);
        uint256 taskId = escrow.createTaskNative{ value: 1 ether }(
            uint64(block.timestamp + 1 days), uint64(block.timestamp + 2 days), uint64(block.timestamp + 3 days)
        );

        vm.warp(block.timestamp + 2 days);
        escrow.forceRefund(taskId);
        assertEq(escrow.creditOf(address(0), creator), 1 ether, "deadline did not unlock task principal");
    }
}
