from sqlalchemy.sql import func
from sqlalchemy.dialects import postgresql
from webinterface import db
from sqlalchemy.ext.declarative import declarative_base

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



class sArchivosGTFS(Model):
    __tablename__   = 'gtfs'
    __bind_key__    = 'gtfs_static'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    descripcion     = Column(String)
    file            = Column(String)
    hash            = Column(String)
    size            = Column(Integer)
    loaded          = Column(Integer, default=0)
    ts              = Column(Datetime, default=func.now())

#agency_id,agency_name,agency_url,agency_timezone,agency_lang,agency_phone,agency_fare_url
#DTPR,División de Transporte Público Regional,http://www.dtpr.gob.cl/,America/Santiago,es,+562 2421 3580,
class sAgencia(Model):
    __tablename__   = 'agency'
    __bind_key__    = 'gtfs_static'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    agency_id       = Column(String)
    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)

#service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday
#S,20210101,20311231,0,0,0,0,0,1,0
#D,20210101,20311231,0,0,0,0,0,0,1
#L,20210101,20311231,1,1,1,1,1,0,0
class sCalendario(Model):
    __tablename__ = 'calendar'
    __bind_key__ = 'gtfs_static'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    service_id      = Column(String)
    start_date      = Column(String)
    end_date        = Column(String)
    monday          = Column(Integer)
    tuesday         = Column(Integer)
    wednesday       = Column(Integer)
    thursday        = Column(Integer)
    friday          = Column(Integer)
    saturday        = Column(Integer)
    sunday          = Column(Integer)

#feed_publisher_name,feed_publisher_url,feed_lang,feed_start_date,feed_end_date,feed_version
#División de Transporte Público Regional,http://www.dtpr.gob.cl/,es,20210101,20311231,Gran Concepción20220616
class sFeedInfo(Model):
    __tablename__ = 'feed_info'
    __bind_key__ = 'gtfs_static'
    id              = Column(Integer,  primary_key=True, nullable=False, autoincrement=True)

    feed_publisher_name = Column(String)
    feed_publisher_url  = Column(String)
    feed_lang           = Column(String)
    feed_start_date     = Column(String)
    feed_end_date       = Column(String)
    feed_version        = Column(String)

#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 sRuta(Model):
    __tablename__ = 'routes'
    __bind_key__ = 'gtfs_static'

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

    route_id            = Column(String)
    agency_id           = Column(String)
    route_short_name    = Column(String)
    route_long_name     = Column(String)
    route_desc          = Column(String)
    route_type          = Column(Integer)
    route_url           = Column(String)
    route_color         = Column(String)
    route_text_color    = Column(String)

#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 sFormas(Model):
    __tablename__ = 'shapes'
    __bind_key__ = 'gtfs_static'

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

    shape_id            = Column(String)
    shape_pt_lat        = Column(Double)
    shape_pt_lon        = Column(Double)
    shape_pt_sequence   = Column(Integer)
    shape_dist_traveled = Column(Double)

#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 sParadas(Model):
    __tablename__ = 'stops'
    __bind_key__ = 'gtfs_static'

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

    stop_id             = Column(String)
    stop_code           = Column(String)
    stop_name           = Column(String)
    stop_desc           = Column(String)
    stop_lat            = Column(Double)
    stop_lon            = Column(Double)
    zone_id             = Column(String)
    stop_url            = Column(String)
    location_type       = Column(Integer)
    parent_station      = Column(String)
    wheelchair_boarding = Column(Integer)
#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 sDetenciones(Model):
    __tablename__ = 'stop_times'
    __bind_key__ = 'gtfs_static'

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

    trip_id         = Column(String)
    arrival_time    = Column(String)
    departure_time  = Column(String)
    stop_id         = Column(String)
    stop_sequence   = Column(Integer)
    stop_headsign   = Column(String)
    pickup_type     = Column(Integer)
    drop_off_type   = Column(Integer)
    timepoint       = Column(Integer)
#,,,,,,

#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 sServicio(Model):
    __tablename__ = 'trips'
    __bind_key__ = 'gtfs_static'

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

    route_id                = Column(String) # Ruta.rutaid
    service_id              = Column(String)
    trip_id                 = Column(String)
    trip_headsign           = Column(String)
    trip_short_name         = Column(String)
    direction_id            = Column(Integer)
    block_id                = Column(String)
    shape_id                = Column(String)
    wheelchair_accessible   = Column(Integer)
    bikes_allowed           = Column(Integer)

# 4cc08782-c-76aabf89-b
