from .models import (Mroom, MroomBooking, MroomSlot,  LendingSeal)
#                      Vehicle, Publicity,
# FileRecord, BorrowRecord, Publicity, PatentInfo, PaperOfm, Platform, Project, PatentRecord, PaperRecord, ProjectApproval, ProjectInfo)
from apps.utils.serializers import CustomModelSerializer
from rest_framework import serializers
from django.db import transaction
from rest_framework.exceptions import ParseError
from apps.utils.constants import EXCLUDE_FIELDS


class MroomSerializer(CustomModelSerializer):
    class Meta:
        model = Mroom
        fields = '__all__'


class MroomBookingSerializer(CustomModelSerializer):
    mroom = serializers.PrimaryKeyRelatedField(queryset=Mroom.objects.all(), write_only=True, label="会议室")
    mdate = serializers.DateField(write_only=True, label="预订日期")
    slots = serializers.ListField(child=serializers.IntegerField(), write_only=True, label="时段索引")
    create_by_name = serializers.CharField(source='create_by.username', read_only=True)
    class Meta:
        model = MroomBooking
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS
    
    @transaction.atomic
    def create(self, validated_data):
        mroom = validated_data.pop('mroom')
        slots = validated_data.pop('slots')
        mdate = validated_data.pop('mdate')
        booking = MroomBooking.objects.create(**validated_data)
        MroomSlot.objects.filter(booking=booking).delete()
        for slot in slots:
            if slot < 0 or slot > 47:
                raise ParseError("时段索引超出范围")
            try:
                MroomSlot.objects.create(booking=booking, slot=slot, mdate=mdate, mroom=mroom)
            except Exception as e:
                raise ParseError(f"时段已预订,请刷新重选-{e}")
        return booking

    @transaction.atomic
    def update(self, instance, validated_data):
        mroom = validated_data.pop('mroom')
        slots = validated_data.pop('slots')
        mdate = validated_data.pop('mdate')
        booking = super().update(instance, validated_data)
        MroomSlot.objects.filter(booking=instance).delete()
        for slot in slots:
            if slot < 0 or slot > 47:
                raise ParseError("时段索引超出范围")
            try:
                MroomSlot.objects.create(booking=booking, slot=slot, mdate=mdate, mroom=mroom)
            except Exception as e:
                raise ParseError(f"时段已预订,请刷新重选-{e}")
        return booking
        

class MroomSlotSerializer(CustomModelSerializer):
    booking_title = serializers.CharField(source='booking.title', read_only=True)
    class Meta:
        model = MroomSlot
        fields = '__all__'
        

# class SealSerializer(CustomModelSerializer):
#     class Meta:
#         model = Seal
#         fields = '__all__'


# class SealManageSerializer(CustomModelSerializer):
#     seal_name = serializers.CharField(source='seal.name', read_only=True)
#     class Meta:
#         model = SealManage
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


class LendingSealSerializer(CustomModelSerializer):
    # seal_name = serializers.CharField(source='seal.name', read_only=True)
    class Meta:
        model = LendingSeal
        fields = '__all__'
        read_only_fields = EXCLUDE_FIELDS

    
# class VehicleSerializer(CustomModelSerializer):
#     class Meta:
#         model = Vehicle
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS + ['actual_km']


# class FileRecordSerializer(CustomModelSerializer):
#     class Meta:
#         model = FileRecord
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class BorrowRecordSerializer(CustomModelSerializer):
#     files = serializers.PrimaryKeyRelatedField(queryset=FileRecord.objects.all(), many=True, write_only=True, label="借阅文件")
#     file_detail = FileRecordSerializer(source='file', many=True, read_only=True, label="借阅文件详情")
#     class Meta:
#         model = BorrowRecord
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS 


# class PublicitySerializer(CustomModelSerializer):
#     class Meta:
#         model = Publicity
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class PatentInfoSerializer(CustomModelSerializer):
#     class Meta:
#         model = PatentInfo
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class PaperSerializer(CustomModelSerializer):
#     class Meta:
#         model = PaperOfm
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class PlatformSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Platform
#         fields = ['id', 'name']


# class ProjectSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = Project
#         fields = ['id', 'name']


# class ProjectMemberSerializer(CustomModelSerializer):
#     affiliated_platforms = serializers.PrimaryKeyRelatedField(
#         many=True,
#         queryset=Platform.objects.all(),
#         write_only=True
#     )
#     affiliated_platforms_detail = PlatformSerializer(
#         source='affiliated_platforms', many=True, read_only=True
#     )

#     affiliated_projects = serializers.PrimaryKeyRelatedField(
#         many=True,
#         queryset=Project.objects.all(),
#         write_only=True
#     )
#     affiliated_projects_detail = ProjectSerializer(
#         source='affiliated_projects', many=True, read_only=True
#     )
#     class Meta:
#         model = PatentRecord
#         fields = '__all__'


# class PaperRecordSerializer(CustomModelSerializer):
#     class Meta:
#         model = PaperRecord
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class ProjectApprovalSerializer(CustomModelSerializer):
#     class Meta:
#         model = ProjectApproval
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS


# class ProjectInfoSerializer(CustomModelSerializer):
#     class Meta:
#         model = ProjectInfo
#         fields = '__all__'
#         read_only_fields = EXCLUDE_FIELDS
        