CREATE TYPE battery_status_enum AS ENUM ('unknown', 'unplugged', 'charging', 'full');

CREATE TABLE locations (
    timestamp bigint PRIMARY KEY,
    latitude numeric(9,6) NOT NULL,
    longitude numeric(9,6) NOT NULL,
    accuracy numeric(7,2) NOT NULL,
    altitude numeric(7,2),
    velocity numeric(7,2),
    battery_level smallint NOT NULL,
    bearing numeric(6,3),
    battery_status battery_status_enum DEFAULT 'unknown' NOT NULL,
    CONSTRAINT Location_battery_level_check CHECK ((battery_level >= 0) AND (battery_level <= 100)),
    CONSTRAINT Location_bearing_check CHECK ((bearing >= (0)::numeric) AND (bearing <= (360)::numeric))
)
WITH (oids = false);

CREATE INDEX locations_timestamp ON locations USING btree (timestamp);