from django.contrib.auth.models import User, Group
from rest_framework import serializers
from parts import models as parts_models

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email', 'first_name', 'last_name', 'groups']

class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'id', 'url', 'name']

class PackageSerializerNoLink(serializers.ModelSerializer):
    class Meta:
        model = parts_models.Package
        fields = '__all__'

class PackageSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = parts_models.Package
        fields = '__all__'

class StorageSerializer(serializers.HyperlinkedModelSerializer):
    full_path = serializers.ReadOnlyField(source='get_full_path')

    class Meta:
        model = parts_models.Storage
        fields = ['url', 'id', 'name', 'parent_storage', 'responsible', 'full_path']

class ComponentSerializer(serializers.HyperlinkedModelSerializer):

    package_data = PackageSerializerNoLink(source='package', read_only=True)
    ro_manufacturer_name = serializers.ReadOnlyField(source='manufacturer.name')
    ro_image = serializers.ReadOnlyField(source='get_resolved_image')

    class Meta:
        model = parts_models.Component
        fields = ['url', 'id', 'name', 'package_data', 'package', 'pref_distri', 'image', 'manufacturer', 'ro_manufacturer_name', 'ro_image']
        

class StockSerializer(serializers.HyperlinkedModelSerializer):
    ro_package_name = serializers.ReadOnlyField(source='component.package.name')
    ro_component_name = serializers.ReadOnlyField(source='component.name')
    ro_manufacturer_name = serializers.ReadOnlyField(source='component.manufacturer.name')
    ro_image = serializers.ReadOnlyField(source='component.get_resolved_image')
    class Meta:
        model = parts_models.Stock
        fields = '__all__'

class DistributorSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = parts_models.Distributor
        fields = '__all__'

class StockIncrementDecrementSerializer(serializers.Serializer):
	increment = serializers.IntegerField()

class ManufacturerSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = parts_models.Manufacturer
        fields = '__all__'

class ComponentTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = parts_models.ComponentType
        fields = '__all__'

class ComponentParameterTypeSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = parts_models.ComponentParameterType
        fields = '__all__'