from rest_framework import serializers
from .models import Establishment
from School.models import SchoolClass, Teacher, Speciality, Fee, Discount, PaymentMode, PaymentPlan
from Subscriptions.models import RegistrationForm, RegistrationFileGroup
from Auth.models import Profile

class EstablishmentSerializer(serializers.ModelSerializer):
    profile_count = serializers.SerializerMethodField()
    profiles = serializers.SerializerMethodField()
    school_class_count = serializers.SerializerMethodField()
    school_classes = serializers.SerializerMethodField()
    teacher_count = serializers.SerializerMethodField()
    teachers = serializers.SerializerMethodField()
    speciality_count = serializers.SerializerMethodField()
    specialities = serializers.SerializerMethodField()
    fee_count = serializers.SerializerMethodField()
    fees = serializers.SerializerMethodField()
    discount_count = serializers.SerializerMethodField()
    discounts = serializers.SerializerMethodField()
    active_payment_mode_count = serializers.SerializerMethodField()
    active_payment_modes = serializers.SerializerMethodField()
    active_payment_plan_count = serializers.SerializerMethodField()
    active_payment_plans = serializers.SerializerMethodField()
    file_group_count = serializers.SerializerMethodField()
    file_groups = serializers.SerializerMethodField()
    registration_form_count = serializers.SerializerMethodField()
    registration_forms = serializers.SerializerMethodField()

    class Meta:
        model = Establishment
        fields = '__all__'

    def get_profile_count(self, obj):
        return Profile.objects.filter(roles__establishment=obj).distinct().count()

    def get_profiles(self, obj):
        return list(Profile.objects.filter(roles__establishment=obj).distinct().values_list('email', flat=True))

    def get_school_class_count(self, obj):
        return SchoolClass.objects.filter(establishment=obj).distinct().count()

    def get_school_classes(self, obj):
        return list(SchoolClass.objects.filter(establishment=obj).distinct().values_list('atmosphere_name', flat=True))

    def get_teacher_count(self, obj):
        return Teacher.objects.filter(profile_role__establishment=obj).distinct().count()

    def get_teachers(self, obj):
        return list(Teacher.objects.filter(profile_role__establishment=obj).distinct().values_list('last_name', 'first_name'))

    def get_speciality_count(self, obj):
        return Speciality.objects.filter(establishment=obj).distinct().count()

    def get_specialities(self, obj):
        return list(Speciality.objects.filter(establishment=obj).distinct().values_list('name', flat=True))

    def get_fee_count(self, obj):
        return Fee.objects.filter(establishment=obj).distinct().count()

    def get_fees(self, obj):
        return list(Fee.objects.filter(establishment=obj).distinct().values_list('name', flat=True))

    def get_discount_count(self, obj):
        return Discount.objects.filter(establishment=obj).distinct().count()

    def get_discounts(self, obj):
        return list(Discount.objects.filter(establishment=obj).distinct().values_list('name', flat=True))

    def get_active_payment_mode_count(self, obj):
        return PaymentMode.objects.filter(establishment=obj).distinct().count()

    def get_active_payment_modes(self, obj):
        return list(PaymentMode.objects.filter(establishment=obj).distinct().values_list('mode', flat=True))

    def get_active_payment_plan_count(self, obj):
        return PaymentPlan.objects.filter(establishment=obj).distinct().count()

    def get_active_payment_plans(self, obj):
        return list(PaymentPlan.objects.filter(establishment=obj).distinct().values_list('plan_type', flat=True))

    def get_file_group_count(self, obj):
        return RegistrationFileGroup.objects.filter(establishment=obj).distinct().count()

    def get_file_groups(self, obj):
        return list(RegistrationFileGroup.objects.filter(establishment=obj).distinct().values_list('name', flat=True))

    def get_registration_form_count(self, obj):
        return RegistrationForm.objects.filter(establishment=obj).distinct().count()

    def get_registration_forms(self, obj):
        return list(RegistrationForm.objects.filter(establishment=obj).distinct().values_list('student__last_name', 'student__first_name'))