contract Registry is Ownable {
    uint256 public constant MAX_PROVIDER_FEE_BPS = 1000; // 10%
    uint256 public defaultFeeBps = 500; // 5%

    /// @dev service provider / admin treasury
    address public treasury;

    /// @dev `Forwarder` for meta-transacitons
    address public forwarder;

    /// @dev The Create2 `ProtocolControl` contract factory.
    IControlDeployer public deployer;

    struct ProtocolControls {
        // E.g. if `latestVersion == 2`, there are 2 `ProtocolControl` contracts deployed.
        uint256 latestVersion;
        // Mapping from version => contract address.
        mapping(uint256 => address) protocolControlAddress;
    }

    /// @dev Mapping from app deployer => versions + app addresses.
    mapping(address => ProtocolControls) private _protocolControls;
    /// @dev Mapping from app (protocol control) => protocol provider fees for the app.
    mapping(address => uint256) private protocolControlFeeBps;

    /// @dev Emitted when the treasury is updated.
    event TreasuryUpdated(address newTreasury);
    /// @dev Emitted when a new deployer is set.
    event DeployerUpdated(address newDeployer);
    /// @dev Emitted when the default protocol provider fees bps is updated.
    event DefaultFeeBpsUpdated(uint256 defaultFeeBps);
    /// @dev Emitted when the protocol provider fees bps for a particular `ProtocolControl` is updated.
    event ProtocolControlFeeBpsUpdated(address indexed control, uint256 feeBps);
    /// @dev Emitted when an instance of `ProtocolControl` is migrated to this registry.
    event MigratedProtocolControl(address indexed deployer, uint256 indexed version, address indexed controlAddress);
    /// @dev Emitted when an instance of `ProtocolControl` is deployed.
    event NewProtocolControl(
        address indexed deployer,
        uint256 indexed version,
        address indexed controlAddress,
        address controlDeployer
    );

    constructor(
        address _treasury,
        address _forwarder,
        address _deployer
    ) {
        treasury = _treasury;
        forwarder = _forwarder;
        deployer = IControlDeployer(_deployer);
    }

    /// @dev Deploys `ProtocolControl` with `_msgSender()` as admin.
    function deployProtocol(string memory uri) external {
        // Get deployer
        address caller = _msgSender();
        // Get version for deployment
        uint256 version = getNextVersion(caller);
        // Deploy contract and get deployment address.
        address controlAddress = deployer.deployControl(version, caller, uri);

        _protocolControls[caller].protocolControlAddress[version] = controlAddress;

        emit NewProtocolControl(caller, version, controlAddress, address(deployer));
    }

    /// @dev Returns the latest version of protocol control.
    function getProtocolControlCount(address _deployer) external view returns (uint256) {
        return _protocolControls[_deployer].latestVersion;
    }

    /// @dev Returns the protocol control address for the given version.
    function getProtocolControl(address _deployer, uint256 index) external view returns (address) {
        return _protocolControls[_deployer].protocolControlAddress[index];
    }

    /// @dev Lets the owner migrate `ProtocolControl` instances from a previous registry.
    function addProtocolControl(address _deployer, address _protocolControl) external onlyOwner {
        // Get version for protocolControl
        uint256 version = getNextVersion(_deployer);

        _protocolControls[_deployer].protocolControlAddress[version] = _protocolControl;

        emit MigratedProtocolControl(_deployer, version, _protocolControl);
    }

    /// @dev Sets a new `ProtocolControl` deployer in case `ProtocolControl` is upgraded.
    function setDeployer(address _newDeployer) external onlyOwner {
        deployer = IControlDeployer(_newDeployer);

        emit DeployerUpdated(_newDeployer);
    }

    /// @dev Sets a new protocol provider treasury address.
    function setTreasury(address _newTreasury) external onlyOwner {
        treasury = _newTreasury;

        emit TreasuryUpdated(_newTreasury);
    }

    /// @dev Sets a new `defaultFeeBps` for protocol provider fees.
    function setDefaultFeeBps(uint256 _newFeeBps) external onlyOwner {
        require(_newFeeBps <= MAX_PROVIDER_FEE_BPS, "Registry: provider fee cannot be greater than 10%");

        defaultFeeBps = _newFeeBps;

        emit DefaultFeeBpsUpdated(_newFeeBps);
    }

    /// @dev Sets the protocol provider fee for a particular instance of `ProtocolControl`.
    function setProtocolControlFeeBps(address protocolControl, uint256 _newFeeBps) external onlyOwner {
        require(_newFeeBps <= MAX_PROVIDER_FEE_BPS, "Registry: provider fee cannot be greater than 10%");

        protocolControlFeeBps[protocolControl] = _newFeeBps;

        emit ProtocolControlFeeBpsUpdated(protocolControl, _newFeeBps);
    }

    /// @dev Returns the protocol provider fee for a particular instance of `ProtocolControl`.
    function getFeeBps(address protocolControl) external view returns (uint256) {
        uint256 fees = protocolControlFeeBps[protocolControl];
        if (fees == 0) {
            return defaultFeeBps;
        }
        return fees;
    }

    /// @dev Returns the next version of `ProtocolControl` for the given `_deployer`.
    function getNextVersion(address _deployer) internal returns (uint256) {
        // Increment version
        _protocolControls[_deployer].latestVersion += 1;

        return _protocolControls[_deployer].latestVersion;
    }
}
function setDefaultFeeBps(uint256 _newFeeBps) external onlyOwner {
    require(_newFeeBps <= MAX_PROVIDER_FEE_BPS, "Registry: provider fee cannot be greater than 10%");

    defaultFeeBps = _newFeeBps;

    emit DefaultFeeBpsUpdated(_newFeeBps);
}

/// @dev Sets a new `defaultFeeBps` for protocol provider fees.