DROP DATABASE IF EXISTS `territory-walker`;
CREATE DATABASE `territory-walker`;
USE `territory-walker`;

CREATE TABLE `gameInstances` (
    gameID VARCHAR(6) UNIQUE NOT NULL,
    PRIMARY KEY (gameID)
);

CREATE TABLE `users` (
    userID VARCHAR(6) UNIQUE NOT NULL,
    nickname VARCHAR (40) NOT NULL,
    PRIMARY KEY (userID)
);

CREATE TABLE `userJoinedGames` (
    userID VARCHAR(6) NOT NULL,
    gameID VARCHAR(6) NOT NULL,
    colour VARCHAR(6) NOT NULL,
    FOREIGN KEY (userID) REFERENCES users(userID),
    FOREIGN KEY (gameID) REFERENCES gameInstances(gameID),
    PRIMARY KEY (userID, gameID)
);

CREATE TABLE `polygonArea` (
    gameID VARCHAR(6) NOT NULL,
    polygonID VARCHAR(36) UNIQUE NOT NULL,
    userID VARCHAR(6) NOT NULL,
    area DOUBLE(30,4) NOT NULL,
    PRIMARY KEY (polygonID),
    FOREIGN KEY (gameID) REFERENCES gameInstances(gameID),
    FOREIGN KEY (userID) REFERENCES users(userID)
);

CREATE TABLE `polygonPoints` (
    polygonID VARCHAR(36) NOT NULL,
    lat DOUBLE(11,8) NOT NULL,
    lng DOUBLE(11,8) NOT NULL,
    FOREIGN KEY (polygonID) REFERENCES polygonArea(polygonID)
);

DELIMITER //
CREATE PROCEDURE getPolygonPoints
(IN id VARCHAR(6))
BEGIN
    SELECT * FROM `polygonPoints`
    JOIN `polygonArea` ON
    `polygonArea`.polygonID = polygonPoints.polygonID
    JOIN (SELECT * FROM `userJoinedGames` WHERE gameID = id) AS userColour ON
    `polygonArea`.userID = userColour.UserID
    WHERE
        polygonArea.gameID = id;
END //
DELIMITER ;


DELIMITER //
CREATE PROCEDURE insertPolygon
(IN gID VARCHAR(6),
IN usID VARCHAR(6),
IN a DOUBLE(30,4))
BEGIN
    DECLARE lastid VARCHAR(36) DEFAULT UUID();
    INSERT INTO `polygonArea` (gameID, polygonID, userID, area) VALUES
    (gID, lastid, usID, a);
    SELECT * FROM `polygonArea`
    JOIN userJoinedGames ON
    userJoinedGames.userID = polygonArea.userID AND userJoinedGames.gameID = polygonArea.gameID
    WHERE polygonID = lastid;
END //
DELIMITER ;

INSERT INTO `gameInstances` (gameID) VALUES ("samdum");
INSERT INTO `users` (userID, nickname) VALUES ("samded", "Lesmaux");
INSERT INTO `users` (userID, nickname) VALUES ("fanded", "Umbra Sheep");
INSERT INTO `userJoinedGames` (userID, gameID, colour) VALUES ("samded", "samdum", "0000FF");
INSERT INTO `polygonArea` (gameID, polygonID, userID, area) VALUES
("samdum", "someuniqueid", "samded", 100000.1);
INSERT INTO `polygonPoints` (polygonID, lat, lng) VALUES
("someuniqueid", -43.5623, 172.5655),
("someuniqueid", -43.5623, 172.5650),
("someuniqueid", -43.5628, 172.5650),
("someuniqueid", -43.5628, 172.5655);
-- TODO CREATE PROCEDURE FOR GENERATING GAME ID
-- concat(substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1),
-- substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', rand()*36+1, 1))