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

import "@openzeppelin/contracts/access/AccessControl.sol";
import "../interop/BridgeRegistry.sol";
import "../../emoney/TokenFactory138.sol";

/**
 * @title eMoneyBridgeIntegration
 * @notice Automatically registers eMoney tokens with BridgeRegistry
 * @dev Extends eMoney token system to auto-register tokens with bridge
 */
contract eMoneyBridgeIntegration is AccessControl {
    bytes32 public constant INTEGRATOR_ROLE = keccak256("INTEGRATOR_ROLE");

    BridgeRegistry public bridgeRegistry;

    // Default bridge configuration for eMoney tokens
    uint256 public defaultMinBridgeAmount = 100e18; // 100 tokens minimum
    uint256 public defaultMaxBridgeAmount = 1_000_000e18; // 1M tokens maximum
    uint8 public defaultRiskLevel = 60; // Medium-high risk (credit instrument)
    uint256 public defaultBridgeFeeBps = 15; // 0.15% default fee

    // Destination chain IDs (regulated entities only - EVM chains)
    uint256[] public defaultDestinations;

    event eMoneyTokenRegistered(
        address indexed token,
        string indexed currencyCode,
        uint256[] destinationChainIds
    );

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

        require(bridgeRegistry_ != address(0), "eMoneyBridgeIntegration: zero bridge registry");

        bridgeRegistry = BridgeRegistry(bridgeRegistry_);

        // Set default destinations (EVM chains only - regulated entities)
        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(42793); // Etherlink (Tezos EVM L2)
    }

    /**
     * @notice Register an eMoney token with bridge registry
     * @param token eMoney token address
     * @param currencyCode Currency code (for tracking)
     * @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 registereMoneyToken(
        address token,
        string memory currencyCode,
        uint256[] memory destinationChainIds,
        uint256 minAmount,
        uint256 maxAmount,
        uint8 riskLevel,
        uint256 bridgeFeeBps
    ) public onlyRole(INTEGRATOR_ROLE) {
        require(token != address(0), "eMoneyBridgeIntegration: zero token");
        require(destinationChainIds.length > 0, "eMoneyBridgeIntegration: no destinations");
        require(minAmount > 0, "eMoneyBridgeIntegration: zero min amount");
        require(maxAmount >= minAmount, "eMoneyBridgeIntegration: max < min");
        require(bridgeFeeBps <= 10000, "eMoneyBridgeIntegration: fee > 100%");

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

        emit eMoneyTokenRegistered(token, currencyCode, destinationChainIds);
    }

    /**
     * @notice Register an eMoney token with default configuration
     * @param token eMoney token address
     * @param currencyCode Currency code
     */
    function registereMoneyTokenDefault(
        address token,
        string memory currencyCode
    ) external onlyRole(INTEGRATOR_ROLE) {
        registereMoneyToken(
            token,
            currencyCode,
            defaultDestinations,
            defaultMinBridgeAmount,
            defaultMaxBridgeAmount,
            defaultRiskLevel,
            defaultBridgeFeeBps
        );
    }

    /**
     * @notice Set default bridge configuration
     */
    function setDefaultMinBridgeAmount(uint256 minAmount) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(minAmount > 0, "eMoneyBridgeIntegration: zero min amount");
        defaultMinBridgeAmount = minAmount;
    }

    function setDefaultMaxBridgeAmount(uint256 maxAmount) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(maxAmount >= defaultMinBridgeAmount, "eMoneyBridgeIntegration: max < min");
        defaultMaxBridgeAmount = maxAmount;
    }

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

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

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

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