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

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

/// @notice Every posted unit of principal is either held or credited exactly once.
contract Inv02_NoOrphanPrincipal is V3Fixtures {
    PartnershipBondV3Safe internal bonds;
    uint256 internal bondId;

    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(2, 1 ether, 2 ether));
        _fundActivePartnership(bonds, bondId, 1 ether, 2 ether);
    }

    function test_mutualSettlementAccountsForAllPostedPrincipal() external {
        PartnershipBondV3Safe.BondData memory data = bonds.bondOf(bondId);
        PartnershipBondV3Safe.MutualSettlement memory authorization = PartnershipBondV3Safe.MutualSettlement({
            bond: address(bonds),
            chainId: block.chainid,
            bondId: bondId,
            termsHash: data.termsHash,
            partyAAmount: 1.2 ether,
            partyBAmount: 1.8 ether,
            nonce: 1,
            deadline: uint64(block.timestamp + 1 days)
        });
        bonds.settleByMutualConsent(
            authorization,
            _signPartnershipMutualSettlement(bonds, authorization, PARTY_A_KEY),
            _signPartnershipMutualSettlement(bonds, authorization, PARTY_B_KEY)
        );
        assertEq(bonds.creditOf(address(0), partyA) + bonds.creditOf(address(0), partyB), 3 ether);
        assertEq(bonds.nativeLiability(), 3 ether, "credits retain their principal liability until withdrawal");
    }
}
