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

import "forge-std/Test.sol";
import "../../contracts/vault/VaultFactory.sol";
import "../../contracts/vault/Vault.sol";
import "../../contracts/vault/tokens/DepositToken.sol";
import "../../contracts/vault/tokens/DebtToken.sol";

contract MockLedger {
    function grantVaultRole(address) external {}
}

contract VaultFactoryTest is Test {
    VaultFactory public factory;

    address public admin = address(0x1);
    address public owner = address(0x2);
    address public entity = address(0x3);
    address public asset = address(0); // ETH
    address public currency = address(0x100);

    // Implementation addresses (would be deployed separately)
    address public vaultImpl;
    address public depositTokenImpl;
    address public debtTokenImpl;
    address public ledger;
    address public entityRegistry;
    address public collateralAdapter;
    address public eMoneyJoin;

    function setUp() public {
        vm.startPrank(admin);

        // Deploy MockLedger (factory calls ledger.grantVaultRole)
        MockLedger mockLedger = new MockLedger();
        ledger = address(mockLedger);

        // Deploy implementations
        vaultImpl = address(new Vault(
            address(0),
            address(0),
            ledger,
            address(0x888), // entity registry placeholder
            address(0x777), // collateral adapter placeholder
            address(0x666)  // eMoney join placeholder
        ));

        depositTokenImpl = address(new DepositToken());
        debtTokenImpl = address(new DebtToken());

        // Deploy factory
        factory = new VaultFactory(
            admin,
            vaultImpl,
            depositTokenImpl,
            debtTokenImpl,
            ledger,
            address(0x888), // entity registry
            address(0x777), // collateral adapter
            address(0x666)  // eMoney join
        );

        vm.stopPrank();
    }

    function test_CreateVault() public {
        vm.prank(admin);
        (address vault, address depositToken, address debtToken) = factory.createVault(
            owner,
            entity,
            asset,
            currency
        );

        assertNotEq(vault, address(0), "Vault should be created");
        assertNotEq(depositToken, address(0), "Deposit token should be created");
        assertNotEq(debtToken, address(0), "Debt token should be created");

        // Check vault tracking
        address[] memory vaults = factory.getVaultsByEntity(entity);
        assertEq(vaults.length, 1);
        assertEq(vaults[0], vault);

        // Check entity mapping
        assertEq(factory.vaultToEntity(vault), entity);
    }

    function test_CreateVault_RevertIfZeroOwner() public {
        vm.prank(admin);
        vm.expectRevert("VaultFactory: zero owner");
        factory.createVault(
            address(0),
            entity,
            asset,
            currency
        );
    }

    function test_CreateVault_RevertIfZeroEntity() public {
        vm.prank(admin);
        vm.expectRevert("VaultFactory: zero entity");
        factory.createVault(
            owner,
            address(0),
            asset,
            currency
        );
    }
}
