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

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

/// @notice Principal recovery replaces the disabled generic stalled-settlement path.
contract StalledSettlementRecoveryV3Test is V3Fixtures {
    PartnershipBondV3Safe private partnership;
    AccountabilityBondV3Safe private accountability;

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

    function test_disabledLegacySettlementRecoveryCallsCannotMoveCustody() external {
        vm.expectRevert(PartnershipBondV3Safe.PilotMutualConsentOnly.selector);
        partnership.requestSettlement(1);
        vm.expectRevert(PartnershipBondV3Safe.PilotMutualConsentOnly.selector);
        partnership.advanceToFinalizable(1);
        vm.expectRevert(PartnershipBondV3Safe.PilotMutualConsentOnly.selector);
        partnership.cancelStalledSettlement(1);
        vm.expectRevert(PartnershipBondV3Safe.PilotMutualConsentOnly.selector);
        partnership.settle(1);

        (bool advanceOk,) = address(accountability).call(abi.encodeWithSignature("advanceToFinalizable(uint256)", 1));
        (bool cancelOk,) = address(accountability).call(abi.encodeWithSignature("cancelStalledSettlement(uint256)", 1));
        (bool settleOk,) = address(accountability).call(abi.encodeWithSignature("settle(uint256)", 1));
        assertFalse(advanceOk || cancelOk || settleOk);
    }

    function test_permissionlessExpiryAndForceExitStillReturnPrincipal() external {
        uint256 id = _createPartnership(partnership, _partnershipTerms(1, 1 ether, 1 ether));
        vm.deal(partyA, 1 ether);
        vm.prank(partyA);
        partnership.fundNative{ value: 1 ether }(id);
        PartnershipBondV3Safe.BondData memory bond = partnership.bondOf(id);
        vm.warp(bond.terms.proposalDeadline);
        partnership.expireUnfunded(id);
        assertEq(uint256(partnership.bondOf(id).state), uint256(BondStateV3.State.ForceExited));
        assertEq(partnership.creditOf(address(0), partyA), 1 ether);
    }
}
