# Path: invenio_communities/members/records/api.py
# class Member(Record):
#     """A Request Event."""
# 
#     model_cls = MemberModel
# 
#     # Systemfields
# 
#     metadata = None
# 
#     index = IndexField(
#         "communitymembers-members-v1.0.0", search_alias="communitymembers"
#     )
#     """The ES index used."""
# 
#     community_id = ModelField("community_id")
#     """The data-layer UUID of the community."""
# 
#     user_id = ModelField("user_id")
#     """The data-layer id of the user (or None)."""
# 
#     role = ModelField("role")
#     """The role of the entity."""
# 
#     # TODO: add visibility
#     # TODO: add group
# 
#     @classmethod
#     def get_record(cls, id_, with_deleted=True):
#         """Return Member."""
# 
#         with db.session.no_autoflush:
#             query = cls.model_cls.query.filter_by(**id_)
#             if not with_deleted:
#                 query = query.filter(cls.model_cls.is_deleted != True)
#             obj = query.one()
#             return cls(obj.data, model=obj)
# 
# Path: invenio_communities/members/services/permissions.py
# class CommunityMembersPermissionPolicy(BasePermissionPolicy):
#     """Permissions for Community Members CRUD operations."""
# 
#     # TODO #384
#     can_read = [AnyUser()]
# 
#     can_search = [AnyUser(), SystemProcess()]
# 
# Path: invenio_communities/members/services/schema.py
# class MemberSchema(BaseRecordSchema):
#     """Schema for a community member."""
# 
#     # input and output
#     community = SanitizedUnicode(required=True)
#     user = fields.Integer()
#     role = fields.String(
#         required=True,
#         validate=validate.OneOf(ROLE_TYPES)
#     )
#     # TODO: add visibility
#     # visibility = SanitizedUnicode(
#     #   required=True, validate=_not_blank(max=100))
#     # TODO: add group
#     # group = SanitizedUnicode()
# 
#     # output only
#     # TODO: add name
#     # name = SanitizedUnicode(dump_only=True)
# 
#     @validates_schema
#     def validate_member_entity(self, data, **kwargs):
#         """Check that at least one member entity is passed."""
#         valid_entities = ['user', 'group']
# 
#         # TODO?: Could maybe use something like ?
#         # validate.OneOf(valid_entities)
#         if not any(e in data for e in valid_entities):
#             raise ValidationError(
#                 _("There must be one of {}".format(valid_entities))
#             )
# 
#     # community is coming in from the API
#     # community_id is coming in from the Record when dumping


from invenio_records_resources.services import Link, RecordServiceConfig
from ..records import Member
from .permissions import CommunityMembersPermissionPolicy
from .schema import MemberSchema
# Copyright (C) 2022 Northwestern University.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Members Service Config."""




class MemberLink(Link):
    """Link variables setter for RequestEvent links."""

    @staticmethod
    def vars(record, vars):
        """Variables for the URI template."""
        # TODO: Revise for human-readable community id and different entities.
        vars.update({
            "community_id": record.community_id,
            "entity": "user",
            "id": record.user_id,
        })


class MemberServiceConfig(RecordServiceConfig):
    """Member Service Config."""

    record_cls = Member
    schema = MemberSchema


# Path: invenio_communities/members/records/api.py
# class Member(Record):
#     """A Request Event."""
# 
#     model_cls = MemberModel
# 
#     # Systemfields
# 
#     metadata = None
# 
#     index = IndexField(
#         "communitymembers-members-v1.0.0", search_alias="communitymembers"
#     )
#     """The ES index used."""
# 
#     community_id = ModelField("community_id")
#     """The data-layer UUID of the community."""
# 
#     user_id = ModelField("user_id")
#     """The data-layer id of the user (or None)."""
# 
#     role = ModelField("role")
#     """The role of the entity."""
# 
#     # TODO: add visibility
#     # TODO: add group
# 
#     @classmethod
#     def get_record(cls, id_, with_deleted=True):
#         """Return Member."""
# 
#         with db.session.no_autoflush:
#             query = cls.model_cls.query.filter_by(**id_)
#             if not with_deleted:
#                 query = query.filter(cls.model_cls.is_deleted != True)
#             obj = query.one()
#             return cls(obj.data, model=obj)
# 
# Path: invenio_communities/members/services/permissions.py
# class CommunityMembersPermissionPolicy(BasePermissionPolicy):
#     """Permissions for Community Members CRUD operations."""
# 
#     # TODO #384
#     can_read = [AnyUser()]
# 
#     can_search = [AnyUser(), SystemProcess()]
# 
# Path: invenio_communities/members/services/schema.py
# class MemberSchema(BaseRecordSchema):
#     """Schema for a community member."""
# 
#     # input and output
#     community = SanitizedUnicode(required=True)
#     user = fields.Integer()
#     role = fields.String(
#         required=True,
#         validate=validate.OneOf(ROLE_TYPES)
#     )
#     # TODO: add visibility
#     # visibility = SanitizedUnicode(
#     #   required=True, validate=_not_blank(max=100))
#     # TODO: add group
#     # group = SanitizedUnicode()
# 
#     # output only
#     # TODO: add name
#     # name = SanitizedUnicode(dump_only=True)
# 
#     @validates_schema
#     def validate_member_entity(self, data, **kwargs):
#         """Check that at least one member entity is passed."""
#         valid_entities = ['user', 'group']
# 
#         # TODO?: Could maybe use something like ?
#         # validate.OneOf(valid_entities)
#         if not any(e in data for e in valid_entities):
#             raise ValidationError(
#                 _("There must be one of {}".format(valid_entities))
#             )
# 
#     # community is coming in from the API
#     # community_id is coming in from the Record when dumping


# Path: invenio_communities/members/services/config.py
from invenio_records_resources.services import Link, RecordServiceConfig
from ..records import Member
from .permissions import CommunityMembersPermissionPolicy
from .schema import MemberSchema

# Copyright (C) 2022 Northwestern University.
#
# Invenio-Communities is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Members Service Config."""




class MemberLink(Link):
    """Link variables setter for RequestEvent links."""

    @staticmethod
    def vars(record, vars):
        """Variables for the URI template."""
        # TODO: Revise for human-readable community id and different entities.
        vars.update({
            "community_id": record.community_id,
            "entity": "user",
            "id": record.user_id,
        })


class MemberServiceConfig(RecordServiceConfig):
    """Member Service Config."""

    record_cls = Member
    schema = MemberSchema


    permission_policy_cls = CommunityMembersPermissionPolicy