CREATE DATABASE sensors;
GRANT ALL PRIVILEGES ON sensors.* TO SensorsAdmin;

USE sensors;
GO

CREATE TABLE Users (`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `username` TEXT, `pwd` INT);
INSERT INTO Users (`ID`, `username`, `pwd`) VALUES (1,'nouser',NULL), (NULL,'Admin',1);

CREATE TABLE Permissions (`userID` INT, `roomID` INT, `view` BOOLEAN DEFAULT 0, `purge_data` BOOLEAN DEFAULT 0, `administer` BOOLEAN DEFAULT 0);
CREATE TABLE Rooms (`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` TEXT, `shortname` TEXT UNIQUE);
CREATE TABLE Sensors(`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `type` INT, `roomID` INT, `mqttTopic` TEXT);
CREATE TABLE Types(`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `type_desc` TEXT);

CREATE TABLE Devices(`ID` INT NOT NULL PRIMARY KEY AUTO_INCREMENT, `roomID` INT, `name` TEXT, `description` TEXT);

CREATE TABLE Readings(`sensorID` INT, `Timestamp` DATETIME, `reading` DOUBLE);
CREATE INDEX `sensor_index` ON Readings (`sensorID`);

CREATE TABLE Log( `timestamp` TIMESTAMP, `type` INT, `message` TEXT);

INSERT INTO Rooms (`name`,`shortname`) VALUES ('101','101'),('102','102'),('210','210'),('211','211'),('215 - Studovna','215');
INSERT INTO Sensors (`type`, `roomID`,`mqttTopic`) VALUES (0,1,"101/floortemp"),(0,1,"101/ceiltemp"),(1,1,"101/humidity"),(0,3,"210/temp"),(0,5,"215/temp"),(1,5,"215/humidity");
INSERT INTO Types (type_desc) VALUES ("Temperature"), ('Humidity');
INSERT INTO `readings`(`Timestamp`,`reading`,`sensorID`) VALUES
    ('2025-11-12 00:00:00',25.7,1),('2025-11-12 01:00:00',26.7,1),('2025-11-12 02:00:00',27.4,1),('2025-11-12 02:04:00',28.0,1),('2025-11-12 03:22:00',28.2,1),
    ('2025-11-12 00:00:00',22.7,2),('2025-11-12 01:00:00',23.4,2),('2025-11-12 02:00:00',23.9,2),('2025-11-12 02:05:00',25.5,2),('2025-11-12 03:20:00',27.7,2),
    ('2025-11-12 00:00:00',88.2,3),('2025-11-12 00:30:00',77.2,3),('2025-11-12 01:02:00',69.8,3),('2025-11-12 01:25:00',65.1,3),('2025-11-12 02:02:00',58.2,3);
INSERT INTO `permissions`(`userID`,`roomID`,`view`) VALUES (1,1,1),(1,2,1),(1,3,1);

# other useful queries
SELECT reading, Timestamp FROM Readings WHERE `sensorID` = 1 ORDER BY `Timestamp` DESC LIMIT 10000;
# SELECT roomID FROM permissions WHERE `userID` = 1 AND `view` = 1 INNER JOIN Rooms ON `permissions`.`roomID` = `Rooms`.`ID`;

SELECT rooms.name, rooms.shortname from permissions INNER JOIN rooms ON permissions.`roomID` = rooms.`ID` WHERE permissions.`userID` = 1 AND permissions.`view` = 1;

SELECT rooms.`ID` from rooms WHERE rooms.shortname = '101';

SELECT Sensors.`ID` from Sensors LEFT JOIN rooms ON Sensors.`roomID` = rooms.ID WHERE rooms.shortname = '101';
SELECT types.`type_desc` from types LEFT JOIN Sensors ON types.`ID` = Sensors.`type` WHERE Sensors.`ID` = 1;

SELECT permissions.`view` FROM permissions LEFT JOIN rooms ON permissions.`roomID` = rooms.ID WHERE rooms.shortname = '101' AND `userID` = 1;

# get latest reading from a sensor
SELECT readings.`Timestamp`, readings.`reading` FROM readings WHERE readings.`sensorID` = '1' ORDER BY readings.`Timestamp` DESC FETCH FIRST 1 ROWS ONLY;
