from rest_framework import serializers

from devices.fields import DeviceGroupRelatedField
from devices.models.device import DeviceGroup
from devices.models.endpoint_device import EndpointModel
from devices.services.endpoint.endpoint_get_status import EndpointStatusService


class EndpointDeviceSerializersAll(serializers.ModelSerializer):
    """Serializer for create/update and show endpoint data."""

    status = serializers.SerializerMethodField()
    group = DeviceGroupRelatedField(queryset=DeviceGroup.objects.all(), default=None, allow_null=True)

    def get_status(self, endpoint: EndpointModel) -> dict:
        return EndpointStatusService(endpoint).get_status()

    def validate_scan_paths(self, value):
        if len(value) != len(set(value)):
            raise serializers.ValidationError('Scan paths must be unique')
        return value

    class Meta:
        model = EndpointModel
        fields = [
            'id', 'name', 'description', 'ip', 'port', 'adjust_datetime', 'updated', 'status',
            'settings_changed', 'incorrect_settings', 'config_errors', 'request_config', 'group',
            # white list
            'whitelist_enabled', 'whitelist_admin', 'white_list_paths',
            # Integrity control
            'integrity_control_enabled', 'integrity_control_timeout', 'scan_paths',
            # antivirus
            'antivirus_enabled', 'antivirus_remove_infected_files', 'antivirus_paths', 'antivirus_start_scan',
            'antivirus_update_db',
            # usb/dvd
            'device_control_enabled', 'prohibit_cd_access', 'usb_control_enabled',
            # rotation
            'event_rotation_type', 'event_rotation_size', 'event_rotation_period', 'event_rotation_time',

        ]


class EndpointConfigSerializer(serializers.ModelSerializer):
    """Serializer to update data from endpoint."""

    class Meta:
        model = EndpointModel
        fields = [
            'device_control_enabled', 'usb_control_enabled',
            'integrity_control_enabled', 'scan_paths', 'integrity_control_timeout',
            'whitelist_enabled', 'whitelist_admin', 'white_list_paths',
            'ip',
            'antivirus_enabled', 'antivirus_paths', 'antivirus_remove_infected_files', 'antivirus_update_db',
            "updated",
            'event_rotation_type', 'event_rotation_size', 'event_rotation_period', 'event_rotation_time',
        ]
