# Path: invenio_communities/communities/records/api.py
# class Community(Record):
#     """Community API."""
# 
#     pid = PIDField('id', provider=CommunitiesIdProvider, create=False)
#     schema = ConstantField(
#         '$schema', 'local://communities/communities-v1.0.0.json')
# 
#     model_cls = models.CommunityMetadata
# 
#     index = IndexField(
#         "communities-communities-v1.0.0",
#         search_alias="communities"
#     )
# 
#     access = CommunityAccessField()
# 
#     bucket_id = ModelField(dump=False)
#     bucket = ModelField(dump=False)
#     files = FilesField(
#         store=False,
#         file_cls=CommunityFile,
#         # Don't delete, we'll manage in the service
#         delete=False,
#     )


from invenio_db import db
from invenio_records.api import Record
from invenio_communities.communities.records.api import Community


class CommunitiesRelationManager:
    """Manager for a record's community relations."""

    def __init__(self, m2m_model_cls, record_id, data):
        self._m2m_model_cls = m2m_model_cls
        self._record_id = record_id
        self._default_id = None
        self._communities_ids = set()
        self._communities_cache = {}
        self.from_dict(data)

    #
    # Helpers
    #
    def _to_id(self, val):
        """Get the community id"""
        if isinstance(val, str):
            return val
        elif isinstance(val, Record):
            return str(val.id)
        return None

    def _lookup_community(self, community_id):
        """Retrieve a community by id.

        Caches the community.
        """
        if community_id not in self._communities_cache:

# Path: invenio_communities/communities/records/api.py
# class Community(Record):
#     """Community API."""
# 
#     pid = PIDField('id', provider=CommunitiesIdProvider, create=False)
#     schema = ConstantField(
#         '$schema', 'local://communities/communities-v1.0.0.json')
# 
#     model_cls = models.CommunityMetadata
# 
#     index = IndexField(
#         "communities-communities-v1.0.0",
#         search_alias="communities"
#     )
# 
#     access = CommunityAccessField()
# 
#     bucket_id = ModelField(dump=False)
#     bucket = ModelField(dump=False)
#     files = FilesField(
#         store=False,
#         file_cls=CommunityFile,
#         # Don't delete, we'll manage in the service
#         delete=False,
#     )


# Path: invenio_communities/records/records/systemfields/communities/manager.py
from invenio_db import db
from invenio_records.api import Record
from invenio_communities.communities.records.api import Community



class CommunitiesRelationManager:
    """Manager for a record's community relations."""

    def __init__(self, m2m_model_cls, record_id, data):
        self._m2m_model_cls = m2m_model_cls
        self._record_id = record_id
        self._default_id = None
        self._communities_ids = set()
        self._communities_cache = {}
        self.from_dict(data)

    #
    # Helpers
    #
    def _to_id(self, val):
        """Get the community id"""
        if isinstance(val, str):
            return val
        elif isinstance(val, Record):
            return str(val.id)
        return None

    def _lookup_community(self, community_id):
        """Retrieve a community by id.

        Caches the community.
        """
        if community_id not in self._communities_cache:

            c = Community.get_record(community_id)