from rest_framework import serializers
from django.forms.models import model_to_dict
from django.core.serializers import serialize

from . import models

class AplicacionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Aplicacion
        fields = '__all__'

class DispositivoSerializer(serializers.ModelSerializer):
    tipo_dispositivo = serializers.SerializerMethodField()

    def get_tipo_dispositivo(self, row):
        return model_to_dict(row.id_tipo_dispositivo)

    class Meta:
        model = models.Dispositivo
        fields = '__all__'

class TipoDispositivoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoDispositivo
        fields = '__all__'

class TipoParaderoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoParadero
        fields = '__all__'

class TipoTransporteSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoTransporte
        fields = '__all__'

class TipoVehiculoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoVehiculo
        fields = '__all__'

class TipoTratamientoPersonaSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoTratamientoPersona
        fields = '__all__'

class PersonaSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Persona
        fields = '__all__'

class ComunaSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Comuna
        fields = '__all__'

class RegionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Region
        fields = '__all__'

class ParaderoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Paradero
        fields = '__all__'

class ParaderoImagenSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.ParaderoImagen
        fields = '__all__'

    def to_representation(self, instance):
        representation = super().to_representation(instance)
        del representation['imagen']
        request = self.context.get('request')
        representation['url'] = request.build_absolute_uri(f'/api/paraderos-image/{instance.id_paradero_imagen}/')
        return representation

class OperadorSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Operador
        fields = '__all__'

class LineaSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Linea
        fields = '__all__'

class LineaParaderoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.LineaParadero
        fields = '__all__'

class UsuarioSerializer(serializers.ModelSerializer):
    # muestro informacion de persona en un objeto aparte
    persona = serializers.SerializerMethodField()
    rol = serializers.SerializerMethodField()

    class Meta:
        model = models.Usuario
        fields = '__all__'
        read_only_fields = ['login']

    def get_persona(self, row):
        return model_to_dict(row.rut)

    def get_rol(self, row):
        return model_to_dict(row.id_rol)
    
    def to_representation(self, instance):
        representation = super().to_representation(instance)

        # Elimina el campo que deseas después de la actualización
        if 'clave' in representation:
            del representation['clave']

        return representation

class AuthSerializer(serializers.Serializer):
    username = serializers.CharField(required=True)
    password = serializers.CharField(required=True, style={'input_type':'password'})

class RolSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Rol
        fields = '__all__'

class RolAplicacionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.RolAplicacion
        fields = '__all__'

class RolOperadorSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.RolOperador
        fields = '__all__'

class LetreroLUR_Serializer(serializers.ModelSerializer):
    class Meta:
        model = models.LetreroLUR
        fields = '__all__'

class RedTransporteSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.RedTransporte
        fields = '__all__'

class GtfsArchivoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.GtfsArchivo
        fields = '__all__'

class TipoCargoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.TipoCargo
        fields = '__all__'

class VistaFuncionarioSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.VistaFuncionario
        fields = '__all__'

class ContratoSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Contrato
        fields = '__all__'

class PersonaDatosLaboralesSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.PersonaDatosLaborales
        fields = '__all__'