# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey and OneToOneField has `on_delete` set to the desired behavior
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models
from .validaciones import rut_valido


class Aplicacion(models.Model):
    id_aplicacion = models.IntegerField(primary_key=True)
    nombre_app = models.CharField(max_length=100, blank=False, null=False)
    path_app = models.CharField(max_length=200, blank=False, null=False)
    vigente = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'aplicacion'


class Comuna(models.Model):
    id_comuna = models.IntegerField(primary_key=True)
    id_region = models.ForeignKey('Region', models.DO_NOTHING, db_column='id_region', blank=True, null=True)
    nombre_comuna = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'comuna'


class Conductor(models.Model):
    patente = models.OneToOneField('Vehiculo', models.DO_NOTHING, db_column='patente', primary_key=True)
    rut = models.ForeignKey('Persona', models.DO_NOTHING, db_column='rut', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'conductor'


class Dispositivo(models.Model):
    id_dispositivo = models.CharField(primary_key=True, max_length=100)
    id_paradero = models.ForeignKey('Paradero', models.DO_NOTHING, db_column='id_paradero', blank=False, null=False)
    vigente = models.BooleanField(blank=True, null=True)
    ultima_conexion = models.DateField(blank=True, null=True)
    id_tipo_dispositivo = models.ForeignKey('TipoDispositivo', models.DO_NOTHING, db_column='id_tipo_dispositivo')

    class Meta:
        managed = False
        db_table = 'dispositivo'


class GtfsFrequencie(models.Model):
    id_trip = models.ForeignKey('GtfsTrips', models.DO_NOTHING, db_column='id_trip', blank=True, null=True)
    start_time = models.TimeField(blank=True, null=True)
    end_time = models.TimeField(blank=True, null=True)
    headway_secs = models.IntegerField(blank=True, null=True)
    exact_time = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_frequencie'


class GtfsPosiciones(models.Model):
    id = models.UUIDField(primary_key=True)
    trip_id = models.UUIDField()
    route_id = models.IntegerField()
    direction_id = models.SmallIntegerField(blank=True, null=True)
    start_time = models.TimeField(blank=True, null=True)
    start_date = models.DateField(blank=True, null=True)
    schedule_relationship = models.SmallIntegerField(blank=True, null=True)
    vehicle_license_plate = models.CharField(max_length=10, blank=True, null=True)
    latitude = models.FloatField(blank=True, null=True)
    longitude = models.FloatField(blank=True, null=True)
    bearing = models.IntegerField(blank=True, null=True)
    odometer = models.IntegerField(blank=True, null=True)
    speed = models.FloatField(blank=True, null=True)
    stop_sequence = models.IntegerField(blank=True, null=True)
    stop_id = models.IntegerField(blank=True, null=True)
    arrival_time = models.BigIntegerField(blank=True, null=True)
    hora_llegada = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_posiciones'


class GtfsRouteType(models.Model):
    id_route_type = models.DecimalField(primary_key=True, max_digits=2, decimal_places=0)
    descripcion = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_route_type'


class GtfsShape(models.Model):
    id_shape = models.CharField(max_length=150)
    shape_pt_lat = models.FloatField(blank=True, null=True)
    shape_pt_lon = models.FloatField(blank=True, null=True)
    shape_pt_sequence = models.IntegerField(blank=True, null=True)
    shape_dist_traveled = models.FloatField(blank=True, null=True)
    id_gtfs_pk = models.AutoField(primary_key=True)

    class Meta:
        managed = False
        db_table = 'gtfs_shape'
        unique_together = (('id_shape', 'shape_dist_traveled'),)


class GtfsStopTimes(models.Model):
    id_paradero = models.OneToOneField('Paradero', models.DO_NOTHING, db_column='id_paradero', primary_key=True)  # The composite primary key (id_paradero, id_trip) found, that is not supported. The first column is selected.
    id_trip = models.ForeignKey('GtfsTrips', models.DO_NOTHING, db_column='id_trip')
    arrival_time = models.CharField(max_length=15, blank=True, null=True)
    stop_sequence = models.IntegerField(blank=True, null=True)
    stop_headsign = models.CharField(max_length=100, blank=True, null=True)
    departure_time = models.CharField(max_length=15, blank=True, null=True)
    drop_off_type = models.IntegerField(blank=True, null=True)
    shape_dist_traveled = models.FloatField(blank=True, null=True)
    timepoint = models.IntegerField(blank=True, null=True)
    pickup_type = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_stop_times'
        unique_together = (('id_paradero', 'id_trip'),)


class GtfsTrips(models.Model):
    id_trip = models.CharField(primary_key=True, max_length=150)
    id_linea = models.CharField(max_length=150, blank=True, null=True)
    id_shape = models.CharField(max_length=150,blank=True, null=True)
    id_trip_regreso = models.ForeignKey('self', models.DO_NOTHING, db_column='id_trip_regreso', blank=True, null=True)
    trip_headsign = models.CharField(max_length=100, blank=True, null=True)
    trip_short_name = models.CharField(max_length=100, blank=True, null=True)
    direction_id = models.IntegerField(blank=True, null=True)
    service_id = models.CharField(max_length=50, blank=True, null=True, db_comment='de calendario')
    block_id = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_trips'


class LetreroLUR(models.Model):
    codigo = models.CharField(primary_key=True, max_length=10)
    linea1 = models.CharField(max_length=50)
    linea2 = models.CharField(max_length=50, blank=True, null=True)
    linea3 = models.CharField(max_length=50, blank=True, null=True)
    linea4 = models.CharField(max_length=50, blank=True, null=True)
    bgcolor1 = models.CharField(max_length=20, blank=True, null=True)
    color1 = models.CharField(max_length=20, blank=True, null=True)
    bgcolor2 = models.CharField(max_length=20, blank=True, null=True)
    color2 = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'letrero_lur'


class Linea(models.Model):
    id_linea = models.CharField(primary_key=True, max_length=150)
    id_operador = models.ForeignKey('Operador', models.DO_NOTHING, db_column='id_operador', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)
    route_short_name = models.CharField(max_length=150, blank=True, null=True)
    route_desc = models.CharField(max_length=150, blank=True, null=True)
    route_type = models.ForeignKey(GtfsRouteType, models.DO_NOTHING, db_column='route_type', blank=True, null=True)
    route_url = models.CharField(max_length=150, blank=True, null=True)
    route_color = models.CharField(max_length=150, blank=True, null=True)
    route_text_color = models.CharField(max_length=150, blank=True, null=True)
    route_long_name = models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'linea'


class LineaParadero(models.Model):
    id_linea_paradero = models.AutoField(primary_key=True)
    id_linea = models.ForeignKey(Linea, models.DO_NOTHING, db_column='id_linea')
    id_paradero = models.ForeignKey('Paradero', models.DO_NOTHING, db_column='id_paradero', blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'linea_paradero'


class Rol(models.Model):
    id_rol = models.IntegerField(primary_key=True)
    nombre_rol = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'rol'


class Operador(models.Model):
    id_operador = models.CharField(primary_key=True, max_length=150)
    id_region = models.ForeignKey('Region', models.DO_NOTHING, db_column='id_region', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)
    nombre_operador = models.CharField(max_length=150, blank=True, null=True)
    agency_url = models.CharField(max_length=200, blank=True, null=True)
    agency_timezone = models.CharField(max_length=200, blank=True, null=True)
    agency_lang = models.CharField(max_length=50, blank=True, null=True)
    agency_phone = models.CharField(max_length=50, blank=True, null=True)
    agency_fare_url = models.CharField(max_length=50, blank=True, null=True)
    id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol', blank=False, null=True)

    class Meta:
        managed = False
        db_table = 'operador'


class Paradero(models.Model):
    id_paradero = models.CharField(primary_key=True, max_length=50)
    id_comuna = models.ForeignKey(Comuna, models.DO_NOTHING, db_column='id_comuna', blank=True, null=True)
    id_tipo_paradero = models.ForeignKey('TipoParadero', models.DO_NOTHING, db_column='id_tipo_paradero', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)
    stop_code = models.CharField(max_length=100, blank=True, null=True)
    stop_name = models.CharField(max_length=100, blank=True, null=True)
    stop_desc = models.CharField(max_length=300, blank=True, null=True)
    stop_lat = models.FloatField(blank=True, null=True)
    stop_lon = models.FloatField(blank=True, null=True)
    zone_id = models.CharField(max_length=200, blank=True, null=True)
    stop_url = models.CharField(max_length=200, blank=True, null=True)
    location_type = models.CharField(max_length=200, blank=True, null=True)
    parent_station = models.CharField(max_length=200, blank=True, null=True)
    stop_timezonene_id = models.CharField(max_length=200, blank=True, null=True)
    wheelchair_boarding = models.DecimalField(max_digits=2, decimal_places=0, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'paradero'


class ParaderoImagen(models.Model):
    id_paradero_imagen = models.AutoField(primary_key=True)
    id_paradero = models.ForeignKey(Paradero, models.DO_NOTHING, db_column='id_paradero')
    imagen = models.BinaryField(blank=True, null=True)
    content_type = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'paradero_imagen'


class Persona(models.Model):
    rut = models.DecimalField(primary_key=True, max_digits=12, decimal_places=0)
    id_tipo_tratamiento = models.ForeignKey('TipoTratamientoPersona', models.DO_NOTHING, db_column='id_tipo_tratamiento', blank=True, null=True)
    id_comuna = models.ForeignKey(Comuna, models.DO_NOTHING, db_column='id_comuna', blank=True, null=True)
    dv = models.CharField(max_length=1, blank=True, null=True)
    nombres = models.CharField(max_length=100, blank=True, null=True)
    apellido_a = models.CharField(max_length=100, blank=True, null=True)
    apellido_b = models.CharField(max_length=100, blank=True, null=True)
    fono = models.CharField(max_length=100, blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    fecha_nacimiento = models.DateField(blank=True, null=True)
    direccion = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'persona'
    
    def save(self, *args, **kwargs):
        # validacion se realiza solo si se esta creando el registro
        if self._state.adding:
            rut_completo = f'{self.rut}-{self.dv}'
            if not rut_valido(rut_completo):
                raise Exception(f'RUT {rut_completo}, no es valido!')

        super().save(*args, **kwargs)


class Region(models.Model):
    id_region = models.IntegerField(primary_key=True)
    nombre_region = models.CharField(max_length=100)

    class Meta:
        managed = False
        db_table = 'region'




class RolAplicacion(models.Model):
    id_rol_app = models.AutoField(primary_key=True)
    id_aplicacion = models.ForeignKey(Aplicacion, models.DO_NOTHING, db_column='id_aplicacion', blank=False, null=False)
    id_rol = models.ForeignKey(Rol, on_delete=models.CASCADE, db_column='id_rol', blank=False, null=False)
    solo_visualizar = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'rol_aplicacion'
        unique_together = (('id_aplicacion', 'id_rol'),)

class RolOperador(models.Model):
    id_rol_operador = models.AutoField(primary_key=True)
    id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol')    
    id_operador = models.ForeignKey(Operador, on_delete=models.CASCADE, db_column='id_operador')


    class Meta:
        managed = True
        db_table = 'rol_operador'
        unique_together = (('id_rol', 'id_operador'),)

class SpatialRefSys(models.Model):
    srid = models.IntegerField(primary_key=True)
    auth_name = models.CharField(max_length=256, blank=True, null=True)
    auth_srid = models.IntegerField(blank=True, null=True)
    srtext = models.CharField(max_length=2048, blank=True, null=True)
    proj4text = models.CharField(max_length=2048, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'spatial_ref_sys'


class TipoDispositivo(models.Model):
    id_tipo_dispositivo = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 'tipo_dispositivo'


class TipoParadero(models.Model):
    id_tipo_paradero = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tipo_paradero'


class TipoTransporte(models.Model):
    id_tipo_transporte = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tipo_transporte'

class TipoCargo(models.Model):
    id_cargo = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tipo_cargo'
        db_table_comment = 'Registro de los cargos que se pueden asociar a un contrato'

class TipoTratamientoPersona(models.Model):
    id_tipo_tratamiento = models.IntegerField(primary_key=True)
    tratamiento = models.CharField(max_length=50, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tipo_tratamiento_persona'
        db_table_comment = 'Establece el tratamiento de como dirigirse hacia una persona:\r\nEjemplo\r\nSeñor\r\nSeñora\r\nSrta'


class TipoVehiculo(models.Model):
    id_tipo_vehiculo = models.IntegerField(primary_key=True)
    descripcion = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'tipo_vehiculo'


class Usuario(models.Model):
    login = models.CharField(primary_key=True, max_length=60)
    rut = models.ForeignKey(Persona, models.DO_NOTHING, db_column='rut', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)
    superuser = models.BooleanField(blank=True, null=True)
    id_rol = models.ForeignKey(Rol, models.DO_NOTHING, db_column='id_rol', blank=False, null=False)
    clave = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'usuario'


class Vehiculo(models.Model):
    ppu = models.CharField(primary_key=True, max_length=10)
    id_tipo_vehiculo = models.ForeignKey(TipoVehiculo, models.DO_NOTHING, db_column='id_tipo_vehiculo', blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'vehiculo'


class VehiculoLinea(models.Model):
    patente = models.OneToOneField(Vehiculo, models.DO_NOTHING, db_column='patente', primary_key=True)  # The composite primary key (patente, id_linea) found, that is not supported. The first column is selected.
    id_linea = models.ForeignKey(Linea, models.DO_NOTHING, db_column='id_linea')
    vigente = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'vehiculo_linea'
        unique_together = (('patente', 'id_linea'),)
        
class RedTransporte(models.Model):
    id_red = models.CharField(primary_key=True, max_length=10)
    nombre_red = models.CharField(max_length=100, blank=True, null=True)
    descripcion = models.CharField(max_length=1000, blank=True, null=True)
    url_gtfs_rt = models.CharField(max_length=100, blank=True, null=True)
    api_key = models.CharField(max_length=100, blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'red_transporte'
        db_table_comment = 'Tabla que contiene las diferentes tipos de red de transporte, como son los de transporte publico, trenes, colectivos, etc.'

class GtfsArchivo(models.Model):
    id_gtfs = models.AutoField(primary_key=True)
    id_red = models.ForeignKey('RedTransporte', models.DO_NOTHING, db_column='id_red', blank=True, null=True)
    archivo = models.CharField(max_length=200, blank=True, null=True)
    ruta_archivo = models.CharField(max_length=200, blank=True, null=True)
    valid_from = models.DateField(blank=True, null=True)
    #created = models.DateTimeField(blank=True, null=True)
    created = models.DateTimeField(auto_now_add=True)  
    usuario = models.CharField(max_length=100, blank=True, null=True)
    vigente = models.BooleanField(blank=True, null=True)
    status = models.CharField(max_length=100, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'gtfs_archivo'
        db_table_comment = 'Registro de los archivos GTFS que se cargan en el sistema'


class VistaFuncionario(models.Model):
    rut = models.ForeignKey('Persona', models.DO_NOTHING, db_column='rut', blank=True, null=True)
    id_operador = models.ForeignKey('Operador', models.DO_NOTHING, db_column='id_operador', blank=True, null=True)
    #patente = models.OneToOneField('Vehiculo', models.DO_NOTHING, db_column='patente', primary_key=True)
    class Meta:
        managed = False
        db_table = 'vw_funcionario'

class Contrato(models.Model):
    rut = models.OneToOneField('Persona', models.DO_NOTHING, db_column='rut', primary_key=True)
    id_operador = models.ForeignKey('Operador', models.DO_NOTHING, db_column='id_operador', blank=True, null=True)
    desde = models.DateField(blank=True, null=True)
    hasta = models.DateField(blank=True, null=True)
    id_cargo = models.ForeignKey('TipoCargo', models.DO_NOTHING, db_column='id_cargo')
    class Meta:
        managed = False
        db_table = 'contrato'

class PersonaDatosLaborales(models.Model):
    rut = models.OneToOneField('Persona', models.DO_NOTHING, db_column='rut', primary_key=True)    
    vigencia_clase_licencia = models.DateField(blank=True, null=True)
    clase_licencia_conducir = models.CharField(max_length=50, blank=True, null=True)     
    profesion = models.CharField(max_length=100, blank=True, null=True)    
    class Meta:
        managed = False
        db_table = 'persona_datos_laborales'