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

import "@openzeppelin/contracts/access/AccessControl.sol";
import "../interop/BridgeRegistry.sol";
import "../../vault/VaultFactory.sol";
import "../../vault/tokens/DepositToken.sol";

/**
 * @title VaultBridgeIntegration
 * @notice Automatically registers vault deposit tokens with BridgeRegistry
 * @dev Extends VaultFactory to auto-register deposit tokens on creation
 */
contract VaultBridgeIntegration is AccessControl {
    bytes32 public constant INTEGRATOR_ROLE = keccak256("INTEGRATOR_ROLE");

    VaultFactory public vaultFactory;
    BridgeRegistry public bridgeRegistry;

    // Default bridge configuration for vault tokens
    uint256 public defaultMinBridgeAmount = 1e18; // 1 token minimum
    uint256 public defaultMaxBridgeAmount = 1_000_000e18; // 1M tokens maximum
    uint8 public defaultRiskLevel = 50; // Medium risk
    uint256 public defaultBridgeFeeBps = 10; // 0.1% default fee

    // Destination chain IDs allowed by default (Polygon, Optimism, Base, Arbitrum, Avalanche, BNB Chain, Monad)
    uint256[] public defaultDestinations;

    event DepositTokenRegistered(
        address indexed depositToken,
        address indexed vault,
        uint256[] destinationChainIds
    );

    constructor(
        address admin,
        address vaultFactory_,
        address bridgeRegistry_
    ) {
        _grantRole(DEFAULT_ADMIN_ROLE, admin);
        _grantRole(INTEGRATOR_ROLE, admin);

        require(vaultFactory_ != address(0), "VaultBridgeIntegration: zero vault factory");
        require(bridgeRegistry_ != address(0), "VaultBridgeIntegration: zero bridge registry");

        vaultFactory = VaultFactory(vaultFactory_);
        bridgeRegistry = BridgeRegistry(bridgeRegistry_);

        // Set default destinations (EVM chains only for now)
        defaultDestinations.push(137); // Polygon
        defaultDestinations.push(10);  // Optimism
        defaultDestinations.push(8453); // Base
        defaultDestinations.push(42161); // Arbitrum
        defaultDestinations.push(43114); // Avalanche
        defaultDestinations.push(56);   // BNB Chain
        defaultDestinations.push(10143); // Monad (example chain ID)
        defaultDestinations.push(42793); // Etherlink (Tezos EVM L2)
    }

    /**
     * @notice Register a deposit token with bridge registry
     * @param depositToken Deposit token address
     * @param destinationChainIds Array of allowed destination chain IDs
     * @param minAmount Minimum bridge amount
     * @param maxAmount Maximum bridge amount
     * @param riskLevel Risk level (0-255)
     * @param bridgeFeeBps Bridge fee in basis points
     */
    function registerDepositToken(
        address depositToken,
        uint256[] memory destinationChainIds,
        uint256 minAmount,
        uint256 maxAmount,
        uint8 riskLevel,
        uint256 bridgeFeeBps
    ) public onlyRole(INTEGRATOR_ROLE) {
        require(depositToken != address(0), "VaultBridgeIntegration: zero deposit token");
        require(destinationChainIds.length > 0, "VaultBridgeIntegration: no destinations");

        bridgeRegistry.registerToken(
            depositToken,
            minAmount,
            maxAmount,
            destinationChainIds,
            riskLevel,
            bridgeFeeBps
        );

        // Find associated vault (would need to track this in VaultFactory)
        // For now, emit event with deposit token only
        emit DepositTokenRegistered(depositToken, address(0), destinationChainIds);
    }

    /**
     * @notice Register a deposit token with default configuration
     * @param depositToken Deposit token address
     */
    function registerDepositTokenDefault(address depositToken) external onlyRole(INTEGRATOR_ROLE) {
        registerDepositToken(
            depositToken,
            defaultDestinations,
            defaultMinBridgeAmount,
            defaultMaxBridgeAmount,
            defaultRiskLevel,
            defaultBridgeFeeBps
        );
    }

    /**
     * @notice Set default bridge configuration
     */
    function setDefaultMinBridgeAmount(uint256 minAmount) external onlyRole(DEFAULT_ADMIN_ROLE) {
        defaultMinBridgeAmount = minAmount;
    }

    function setDefaultMaxBridgeAmount(uint256 maxAmount) external onlyRole(DEFAULT_ADMIN_ROLE) {
        defaultMaxBridgeAmount = maxAmount;
    }

    function setDefaultRiskLevel(uint8 riskLevel) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(riskLevel <= 255, "VaultBridgeIntegration: invalid risk level");
        defaultRiskLevel = riskLevel;
    }

    function setDefaultBridgeFeeBps(uint256 feeBps) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(feeBps <= 10000, "VaultBridgeIntegration: fee > 100%");
        defaultBridgeFeeBps = feeBps;
    }

    function setDefaultDestinations(uint256[] memory chainIds) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(chainIds.length > 0, "VaultBridgeIntegration: no destinations");
        defaultDestinations = chainIds;
    }

    /**
     * @notice Get default destinations
     */
    function getDefaultDestinations() external view returns (uint256[] memory) {
        return defaultDestinations;
    }
}
