CREATE TABLE routes (
    route_id VARCHAR(50) PRIMARY KEY,
    agency_id VARCHAR(50),
    route_short_name VARCHAR(50),
    route_long_name VARCHAR(255),
    route_desc TEXT,
    route_type INT,
    route_url VARCHAR(255),
    route_color VARCHAR(10),
    route_text_color VARCHAR(10)
);

CREATE TABLE trips (
    trip_id VARCHAR(50) PRIMARY KEY,
    route_id VARCHAR(50),
    service_id VARCHAR(50),
    shape_id VARCHAR(50),
    trip_headsign VARCHAR(255),
    trip_short_name VARCHAR(50),          
    direction_id INT,
    block_id VARCHAR(50),
    wheelchair_accessible INT,            
    bikes_allowed INT,                    
    FOREIGN KEY (route_id) REFERENCES routes(route_id)
);
CREATE TABLE stops (
    stop_id VARCHAR(50) PRIMARY KEY,
    stop_code VARCHAR(50),
    stop_name VARCHAR(255),
    stop_desc TEXT,
    stop_lat DOUBLE,
    stop_lon DOUBLE,
    zone_id VARCHAR(50),
    stop_url VARCHAR(255),
    location_type INT DEFAULT NULL,
    parent_station VARCHAR(50),
    stop_timezone VARCHAR(50),
    wheelchair_boarding INT DEFAULT NULL
);
CREATE TABLE stop_times (
    trip_id VARCHAR(50),
    arrival_time VARCHAR(20),
    departure_time VARCHAR(20),
    stop_id VARCHAR(50),
    stop_sequence INT,
    stop_headsign VARCHAR(255),
    pickup_type INT,
    drop_off_type INT,
    shape_dist_traveled DOUBLE NOT NULL DEFAULT 0   
    timepoint INT,
    PRIMARY KEY (trip_id, stop_id, stop_sequence),
    FOREIGN KEY (trip_id) REFERENCES trips(trip_id),
    FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
);

CREATE TABLE shapes (
    shape_id VARCHAR(50),
    shape_pt_lat DOUBLE,
    shape_pt_lon DOUBLE,
    shape_pt_sequence INT,
    shape_dist_traveled DOUBLE,
    PRIMARY KEY (shape_id, shape_pt_sequence)
);

-- Realtime: vehicle positions
CREATE TABLE rt_vehicle_positions (
    vehicle_id VARCHAR(50),
    trip_id VARCHAR(50),
    route_id VARCHAR(50),
    lat DOUBLE,
    lon DOUBLE,
    bearing INT,
    speed DOUBLE,
    timestamp BIGINT,
    PRIMARY KEY (vehicle_id)
);

-- Realtime: trip updates
CREATE TABLE rt_trip_updates (
    trip_id VARCHAR(50),
    stop_id VARCHAR(50),
    arrival_time BIGINT,
    departure_time BIGINT,
    delay INT,
    schedule_relationship VARCHAR(20),
    PRIMARY KEY (trip_id, stop_id),
    FOREIGN KEY (stop_id) REFERENCES stops(stop_id)
);

-- Realtime: alerts
CREATE TABLE rt_alerts (
    alert_id VARCHAR(50) PRIMARY KEY,
    header TEXT,
    description TEXT,
    cause VARCHAR(50),
    effect VARCHAR(50),
    timestamp BIGINT
);
