from rest_framework import serializers
from .models import Organization


class OrganizationSerializer(serializers.ModelSerializer):
    job_count = serializers.SerializerMethodField()

    class Meta:
        model = Organization
        fields = ['id', 'name', 'parent', 'logo', 'description', 'email', 'is_active', 'job_count']

    def get_job_count(self, obj):
        return obj.jobs.filter(status='published').count()


class OrganizationTreeSerializer(serializers.ModelSerializer):
    """带子公司列表，用于门户展示"""
    children = serializers.SerializerMethodField()
    job_count = serializers.SerializerMethodField()

    class Meta:
        model = Organization
        fields = ['id', 'name', 'logo', 'description', 'email', 'job_count', 'children']

    def get_job_count(self, obj):
        return obj.jobs.filter(status='published').count()

    def get_children(self, obj):
        return OrganizationSerializer(
            obj.children.filter(is_active=True), many=True, context=self.context
        ).data
