from rest_framework import viewsets
from rest_framework.permissions import AllowAny
from .models import Organization
from .serializers import OrganizationSerializer, OrganizationTreeSerializer
from apps.accounts.permissions import IsSuperAdmin


class OrganizationPublicViewSet(viewsets.ReadOnlyModelViewSet):
    """公开只读：门户展示用，返回顶级组织（集团）及其子公司树"""
    queryset = Organization.objects.filter(is_active=True)
    serializer_class = OrganizationTreeSerializer

    def get_queryset(self):
        # 列表只返回顶级，详情可查任意
        if self.action == 'list':
            return Organization.objects.filter(is_active=True, parent__isnull=True)
        return Organization.objects.filter(is_active=True)
    permission_classes = [AllowAny]


class OrganizationManageViewSet(viewsets.ModelViewSet):
    """超管：完整增删改查"""
    queryset = Organization.objects.all()
    serializer_class = OrganizationSerializer
    permission_classes = [IsSuperAdmin]
