
from rest_framework import permissions
from .models import Team,Member

class IsMemberOrTeamOwner(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        # Check if user is a member of the team or the team owner
        return request.user in obj.team.members.all() or request.user == obj.team.owner

class IsTeamOwner(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if isinstance(obj, Team):
            return obj.owner == request.user
        elif isinstance(obj, Member):
            return obj.team.owner == request.user
        return False

class IsTeamOwnerOrMember(permissions.BasePermission):
    def has_permission(self, request, view):
        team_id = view.kwargs.get('team_id')
        if not team_id:
            return False
        team = Team.objects.get(id=team_id)
        return team.owner == request.user or team.members.filter(user=request.user).exists()

