from sqlalchemy.sql import func
from sqlalchemy.dialects import postgresql
from sqlalchemy import Integer, BigInteger, Double, Float, String, Time, Column, ForeignKey, UniqueConstraint


Model = declarative_base()
engine = create_engine(os.environ.get('SQLALCHEMY_HOTDB_URI'), echo=False)
Session = sessionmaker(bind=engine, autocommit=False, autoflush=True)
session = Session()


class ArchivosGTFS(Model):
    __tablename__   = 'archivos'
    __bind_key__    = 'gtfs_work'
    id              = Column(Integer,  primary_key=True, nullable=False)

    analisis        = Column(Integer, default=0)
    terminado       = Column(Integer, default=0)
    activo          = Column(Integer, default=0)

class Agencia(Model):
    __tablename__   = 'agency'
    __bind_key__    = 'gtfs_work'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    agency_id       = Column(String, unique=True)
    agency_name     = Column(String)
    agency_url      = Column(String)
    agency_timezone = Column(String)
    agency_lang     = Column(String)
    agency_phone    = Column(String)
    agency_fare_url = Column(String)

class Calendario(Model):
    __tablename__   = 'calendars'
    __bind_key__    = 'gtfs_work'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    label           = Column(String, unique=True)
    monday          = Column(Integer, default=0)
    tuesday         = Column(Integer, default=0)
    wednesday       = Column(Integer, default=0)
    thursday        = Column(Integer, default=0)
    friday          = Column(Integer, default=0)
    saturday        = Column(Integer, default=0)
    sunday          = Column(Integer, default=0)


#shape_id,shape_pt_lat,shape_pt_lon,shape_pt_sequence,shape_dist_traveled
#1136979693,-36.843,-73.00984,1
#1136979693,-36.843,-73.00984,2
class Formas(Model):
    __tablename__       = 'shapes'
    __bind_key__        = 'gtfs_work'

    id                  = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)
    shape_1pt_lat       = Column(Double, nullable=False)
    shape_1pt_lon       = Column(Double, nullable=False)

    shape_num_pt        = Column(Integer, nullable=False)
    shape_length        = Column(Double)

class Segmentos(Model):
    __tablename__       = 'shapes_point'
    __bind_key__        = 'gtfs_work'

    id                  = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)
    pt_start_lat        = Column(Double, nullable=False)
    pt_start_lon        = Column(Double, nullable=False)
    pt_end_lat          = Column(Double, nullable=False)
    pt_end_lon          = Column(Double, nullable=False)

    shape_pt_sequence   = Column(Integer, nullable=False)
    shape_slope         = Column(Double)
    shape_dist_traveled = Column(Double)
    shape_id            = Column(Integer, ForeignKey('shapes.id'), nullable=False)

    __table_args__ = (UniqueConstraint('shape_id', 'shape_pt_sequence', name='_shapeid_sequence_uc'), )


class Comunas(Model):
    __tablename__  = 'comuna'
    __bind_key__   = 'gtfs_work'

    id             = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)
    name           = Column(String, unique=True)


#stop_id,stop_code,stop_name,stop_desc,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,wheelchair_boarding
#40921,,Pque Zoologico Concepcion  Poniente,,-36.8400453,-73.00696914,,,,,
#40808,,Cno. Nonguen esq Las Vertientes,,-36.83675878,-73.00343935,,,,,
class Paradas(Model):
    __tablename__       = 'stops'
    __bind_key__        = 'gtfs_work'

    id                  = Column(BigInteger,  primary_key=True, nullable=False, autoincrement=True)

    stop_code           = Column(String, unique=True, nullable=True)
    stop_name           = Column(String)
    stop_desc           = Column(String)
    stop_lat            = Column(Double, nullable=False)
    stop_lon            = Column(Double, nullable=False)

    comuna_id           = Column(Integer, ForeignKey('comuna.id'), nullable=False)
    stop_url            = Column(String)
    parent_station      = Column(BigInteger)
    wheelchair_boarding = Column(Integer, default=0)


#route_id,agency_id,route_short_name,route_long_name,route_desc,route_type,route_url,route_color,route_text_color
#625,DTPR,70KH,Nonguén - Parque Tumbes,,3,,0d7215,ffffff
#600,DTPR,41CR,Parque Empresarial Biobío - Terminal Collao,,3,,ad0101,ffffff
class Lineas(Model):
    __tablename__       = 'routes'
    __bind_key__        = 'gtfs_work'

    id                  = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    agency_id           = Column(Integer, ForeignKey('agency.id'), nullable=False)
    route_short_name    = Column(String)
    route_long_name     = Column(String)
    route_desc          = Column(String)
    route_url           = Column(String)
    route_color         = Column(Integer, default=0)
    route_text_color    = Column(Integer, default=0)


#route_id,service_id,trip_id,trip_headsign,                direction_id,block_id,shape_id,wheelchair_accessible,bikes_allowed
#route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,block_id,shape_id,wheelchair_accessible,bikes_allowed
#625,S,c8b17d5f-4-76aabf89-b,Nonguén,,1,,1136979694,,
#625,S,4d018d35-7-76aabf89-b,Parque Tumbes,,0,,1136979693,,
#ac48288b-1-56195eaf-8,b1910f5b,1,0,1168259538,,0,0,1-1,1
#d9e94be9-f-56195eaf-8,b1910f5b,1,0,1168259538,,0,0,1-1,2
class Servicio(Model):
    __tablename__           = 'trips'
    __bind_key__            = 'gtfs_work'

    id                      = Column(Integer,  primary_key=True, nullable=False)

    route_id                = Column(Integer, ForeignKey('routes.id'), nullable=False) # A que linea corresponde
    calendar_id             = Column(Integer, ForeignKey('calendars.id'), nullable=False)
    gtfs_id                 = Column(Integer, ForeignKey('archivos.id'), nullable=False) # Identifiador unico del servicio
    shape_id                = Column(Integer, ForeignKey('shapes.id'), nullable=False) # shapes.id
    direction_id            = Column(Integer, nullable=False) # 0=ida, 1=vuelta

    trip_headsign           = Column(String)
    trip_short_name         = Column(String)
    wheelchair_accessible   = Column(Integer, default=0)
    bikes_allowed           = Column(Integer, default=0)

#trip_id,arrival_time,departure_time,stop_id,stop_sequence,                                        timepoint,shape_dist_traveled
#trip_id,arrival_time,departure_time,stop_id,stop_sequence,stop_headsign,pickup_type,drop_off_type,timepoint
#c8b17d5f-4-76aabf89-b,05:01:00,05:01:00,40439,1,,,,1
#c8b17d5f-4-76aabf89-b,05:02:00,05:02:00,40440,2,,,,0
class Detenciones(Model):
    __tablename__   = 'stop_times'
    __bind_key__    = 'gtfs_work'

    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    trip_id         = Column(Integer, ForeignKey('trips.id'), nullable=False)
    stop_sequence   = Column(Integer, nullable=False)
    stop_id         = Column(BigInteger, ForeignKey('stops.id'), nullable=False)

    departure_time  = Column(String(9), index=True)
    arrival_time    = Column(String(9))
    dist_traveled   = Column(Integer, default=0)

    stop_headsign   = Column(String)
    pickup_type     = Column(Integer, default=0)
    drop_off_type   = Column(Integer, default=0)
    timepoint       = Column(Integer, default=1)

    __table_args__ = (UniqueConstraint('trip_id', 'stop_sequence', name='_trip_sequence_uc'), )

# 4cc08782-c-76aabf89-b
