//
// Created by eric on 8/15/15.
//

#ifndef HATHOR_SQL_HH
#define HATHOR_SQL_HH

#include <map>
#include <iomanip>

sql::SQLString userSql("DROP TABLE IF EXISTS `users`;");

sql::SQLString artistsSql("DROP TABLE IF EXISTS `artists`;");

sql::SQLString albumsSql("DROP TABLE IF EXISTS `albums`;");

sql::SQLString tracksSql("DROP TABLE IF EXISTS `tracks`;");

sql::SQLString playlistsSql("DROP TABLE IF EXISTS `playlists`;");

sql::SQLString userCreateSql("CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'the user id int', `uid` varchar(64) NOT NULL COMMENT 'user id in md5', `username` varchar(20) NOT NULL COMMENT 'the users name', `email` varchar(255) NOT NULL COMMENT 'the users email', `password` varchar(255) NOT NULL COMMENT 'the users hashed password', `isadmin` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'true or false, admin rights', `pls` blob COMMENT 'json list of playlist ids', `ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'timestamp of user record', PRIMARY KEY (`id`), UNIQUE KEY `hathor_username` (`username`), UNIQUE KEY `hathor_email` (`email`), UNIQUE KEY `hathor_id` (`id`), UNIQUE KEY `hathor_uid` (`uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

sql::SQLString artistCreateSql("CREATE TABLE IF NOT EXISTS `artists` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `arid` varchar(64) NOT NULL COMMENT 'unique artist id in md5 format', `name` varchar(255) NOT NULL COMMENT 'name of the artist', `web` blob COMMENT 'json formated information with different web sites', `tour` blob COMMENT 'json formated information about touring', `rating` tinyint(2) unsigned NOT NULL DEFAULT '0' COMMENT 'rating from 1 to 10 of artist', `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'updated timestamp', `extra` blob COMMENT 'any extra information about artist in json format', PRIMARY KEY (`id`), UNIQUE KEY `arid_hathor` (`arid`), UNIQUE KEY `name_hathor` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

sql::SQLString albumCreateSql("CREATE TABLE `albums` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id of the album', `aid` varchar(64) NOT NULL COMMENT 'album id in md5', `arid` varchar(64) NOT NULL COMMENT 'matching arist id from artist table', `released` year(4) DEFAULT '1969' COMMENT 'year released', `web` blob COMMENT 'json list of web sites for this album', `extra` blob COMMENT 'any extra information about this album in json format.', `first_available` date DEFAULT '1969-01-01', PRIMARY KEY (`id`), UNIQUE KEY `aid_hathor` (`aid`), UNIQUE KEY `arid_hathor` (`arid`), CONSTRAINT `arid_artists` FOREIGN KEY (`arid`) REFERENCES `artists` (`arid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

sql::SQLString tracksCreateSql("CREATE TABLE IF NOT EXISTS `tracks` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id of the song in the database', `tid` varchar(64) NOT NULL COMMENT 'song id md5 hash', `arid` varchar(64) NOT NULL COMMENT 'corresponding artist id from artist table in md5 hash', `aid` varchar(64) NOT NULL COMMENT 'corresponding album id from album table in md5 hash', `title` varchar(255) NOT NULL COMMENT 'title of song', `track` tinyint(4) unsigned DEFAULT '0' COMMENT 'track number of song', `genre` varchar(45) DEFAULT NULL COMMENT 'genre of song', `year` tinyint(4) DEFAULT '0' COMMENT 'year of song', `label` varchar(255) DEFAULT NULL COMMENT 'music label of song e.g. Columbia Records', `lyrics` blob COMMENT 'lyrics if any', `bitrate` mediumint(6) unsigned DEFAULT '0' COMMENT 'bitrate of song', `sample` mediumint(6) unsigned DEFAULT '0' COMMENT 'sample rate of song', `channels` smallint(2) unsigned DEFAULT '0' COMMENT 'channels of song i.e stereo = 2, mono = 1', `length` int(10) unsigned DEFAULT '0' COMMENT 'length of song', `minutes` tinytext COMMENT 'total length of song in minutes and seconds', `seconds` tinyint(4) unsigned DEFAULT '0' COMMENT 'seconds of song', `pls` blob COMMENT 'playlists this song is associated with', `ts` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'timestamp of last update of song', PRIMARY KEY (`id`), UNIQUE KEY `arid_hathor` (`arid`) USING BTREE, UNIQUE KEY `aid_hathor` (`aid`), UNIQUE KEY `title_hathor` (`title`), UNIQUE KEY `sid_hathor` (`tid`), CONSTRAINT `aid_albums_table` FOREIGN KEY (`aid`) REFERENCES `albums` (`aid`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `arid_artists_table` FOREIGN KEY (`arid`) REFERENCES `artists` (`arid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

sql::SQLString playlistsCreateSql("CREATE TABLE IF NOT EXISTS `playlists` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'playlist id', `uid` varchar(64) NOT NULL COMMENT 'uid in the users table in md5', `plsid` varchar(64) NOT NULL COMMENT 'playlist id in md5', `name` varchar(255) NOT NULL COMMENT 'name for this playlist', `track` varchar(64) NOT NULL COMMENT 'the track in md5 format from the tracks table', `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'playlist last updated timestamp', PRIMARY KEY (`id`), UNIQUE KEY `uid_hathor` (`uid`), UNIQUE KEY `uid_pls_hathor` (`uid`), KEY `playlist_track_idx` (`track`), CONSTRAINT `playlist_track` FOREIGN KEY (`track`) REFERENCES `tracks` (`tid`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `playlist_uid` FOREIGN KEY (`uid`) REFERENCES `users` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");

std::map< std::string, sql::SQLString > SqlMap =
{
  {"userSql", userSql},
  {"userCreateSql", userCreateSql},
  {"artistsSql", artistsSql},
  {"artistCreateSql", artistCreateSql},
  {"albumsSql", albumsSql},
  {"albumCreateSql", albumCreateSql},
  {"tracksSql", tracksSql},
  {"tracksCreateSql", tracksCreateSql},
  {"playlistsSql", playlistsSql},
  {"playlistsCreateSql", playlistsCreateSql},

};

#endif //HATHOR_SQL_HH
