CREATE TABLE stop_names (
	_stopID TEXT NOT NULL,
	_stopName TEXT NOT NULL
);

CREATE INDEX ix_stop_names_stopName ON stop_names ( _stopName ); 

CREATE TABLE stop_geo (
_stopID varchar(7) PRIMARY KEY not null,
_stopLoc GEOMETRY not null
);
--SELECT AddGeometryColumn('stop_geo', '_stopLoc', 4326, 'POINT', 2);

CREATE INDEX ix_stop_geo_loc ON stop_geo ( _stopLoc ); 

/* CREATE TABLE stops_on_trip (
	_tripID varchar(11) not null,
	_stopID varchar(7) not null,
	_stopSeq integer NOT NULL,
	_stopDept time with time zone NOT NULL,
	_stopDeptOnNextDay boolean not NULL,
	_stopArri time with time zone NOT NULL,
	_stopArriOnNextDay boolean not NULL,
	PRIMARY KEY(_tripID, _stopID)
);

CREATE INDEX ix_stops_on_trip_tripID ON stops_on_trip (_tripID);
CREATE INDEX ix_stops_on_trip_stopID ON stops_on_trip (_stopID);
CREATE INDEX ix_stops_on_trip_stopSeq ON stops_on_trip (_stopSeq);

CREATE INDEX ix_stops_on_trip_stopDept ON stops_on_trip (_stopDept);
CREATE INDEX ix_stops_on_trip_stopArri ON stops_on_trip (_stopArri);
CREATE INDEX ix_stops_on_trip_stopDeptOnNextDay ON stops_on_trip (_stopDeptOnNextDay);
CREATE INDEX ix_stops_on_trip_stopArriOnNextDay ON stops_on_trip (_stopArriOnNextDay);
CREATE INDEX ix_stops_on_trip_stopDeptEx ON stops_on_trip (_stopDept,_stopDeptOnNextDay);
CREATE INDEX ix_stops_on_trip_stopArriEx ON stops_on_trip (_stopArri,_stopArriOnNextDay);

CREATE TABLE trips (
	_tripID varchar(11) NOT NULL,
	_routeID varchar(3) not null,
	_serviceID varchar(2) not null,
	_tripHeadSign varchar(17) not null,
	PRIMARY KEY(_tripID)
);

CREATE INDEX ix_trips_routeID ON trips (_routeID);
CREATE INDEX ix_trips_serviceID ON trips (_serviceID);
CREATE INDEX ix_trips__tripHeadSign ON trips (_tripHeadSign); */

--///////////////////////////////////////////////////////////

CREATE TABLE stops_on_trip_frequency (
	_uniqtripID varchar(31) not null,
	_stopID varchar(7) not null,
	_stopSeq integer NOT NULL,
	PRIMARY KEY(_uniqtripID, _stopID)
);

CREATE INDEX ix_stops_on_trip_frequency_uniqtripID ON stops_on_trip_frequency (_uniqtripID);
CREATE INDEX ix_stops_on_trip_frequency_stopID ON stops_on_trip_frequency (_stopID);
CREATE INDEX ix_stops_on_trip_frequency_stopSeq ON stops_on_trip_frequency (_stopSeq);

CREATE TABLE trips_sum (
	_tripID varchar(11) NOT NULL,
	_routeID varchar(3) not null,
	_serviceID varchar(2) not null,
	_tripHeadSign varchar(17) not null,	
	_uniqtripID varchar(31) not null,
	_start time with time zone NOT NULL,
	_start_next_day boolean not null,
	_end time with time zone NOT NULL,
	_end_next_day boolean not null,
	_freq integer not null,	
	PRIMARY KEY(_tripID)
);

CREATE INDEX ix_trips_routeID ON trips_sum (_routeID);
CREATE INDEX ix_trips_serviceID ON trips_sum (_serviceID);
CREATE INDEX ix_trips_tripHeadSign ON trips_sum (_tripHeadSign);
CREATE INDEX ix_trips_uniqtripID ON trips_sum (_uniqtripID);

CREATE TABLE schedule_rules (
	_serviceID varchar(2) NOT NULL,
	_serviceDay dayOfWeek not null,
	_startPeriodDate DATE not null,
	_endPeriodDate DATE not null,
	PRIMARY KEY(_serviceID, _serviceDay)
);

CREATE INDEX ix_schedule_rules_serviceID ON schedule_rules (_serviceID);
CREATE INDEX ix_schedule_rules_serviceDay ON schedule_rules (_serviceDay);
CREATE INDEX ix_schedule_rules_startPeriodDate ON schedule_rules (_startPeriodDate);
CREATE INDEX ix_schedule_rules_endPeriodDate ON schedule_rules (_endPeriodDate);

CREATE TABLE routes (
	_routeID varchar(3) not null,
	_agencyID varchar(3) not null,
	_routeName varchar(5) NOT NULL,
	_routeLongName varchar(54) NOT NULL,	
	_routeType transportType not NULL,
	PRIMARY KEY(_routeID)
);

CREATE INDEX ix_routes_agencyID ON routes (_agencyID);
CREATE INDEX ix_routes_routeName ON routes (_routeName);
CREATE INDEX ix_routes_routeType ON routes (_routeType);