CREATE TABLE station (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    notes TEXT,
    amount_people INTEGER,
    last_login DATETIME,
    pw TEXT NOT NULL DEFAULT (upper(hex(randomblob(4)))),
    lat REAL,
    lng REAL
);

CREATE TABLE route (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE -- e.g. 'wiwö'
);

CREATE TABLE route_station (
    route_id INTEGER,
    station_id INTEGER,
    pos INTEGER,
    PRIMARY KEY (route_id, station_id),
    FOREIGN KEY (route_id) REFERENCES route(id),
    FOREIGN KEY (station_id) REFERENCES station(id)
);

CREATE TABLE "group" (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL UNIQUE,
    notes TEXT,
    amount_people INTEGER,
    first_station_id INTEGER NOT NULL,
    route_id INTEGER NOT NULL,
    FOREIGN KEY (first_station_id) REFERENCES station(id),
    FOREIGN KEY (route_id) REFERENCES route(id)
);

CREATE TABLE group_station (
    group_id INTEGER,
    station_id INTEGER,
    points INTEGER,
    notes TEXT,
    arrived_at DATETIME,
    started_at DATETIME,
    left_at DATETIME,
    PRIMARY KEY (group_id, station_id),
    FOREIGN KEY (group_id) REFERENCES "group"(id),
    FOREIGN KEY (station_id) REFERENCES station(id)
);


