CREATE DATABASE grosolo;

CREATE TABLE `users`(
	`uid` int not null AUTO_INCREMENT,
	`name` varchar(100) not null,
	`password` varchar(100) not null,
	`salt` varchar(80) DEFAULT '' not null,
	`group` int not null,
	`active` tinyint DEFAULT 0 not null,
	`is_super_admin` tinyint(1) not null,
	`createdAt` datetime not null,
	`lastActiveAt` datetime not null,
	`session` varchar(200) DEFAULT '' not null,
	`last_ip` varchar(200) DEFAULT '0.0.0.0.0' not null,
	`email` varchar(200) DEFAULT '' not null,
	`avatar` varchar(20) DEFAULT '' not null,
	`message` text not null,
	`url_prefix` varchar(20) DEFAULT '' not null,
	`url_name` varchar(100) DEFAULT '' not null,
	primary key(`uid`),
	unique(`name`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `users_groups`(
	`gid` int not null AUTO_INCREMENT,
	`name` varchar(100) not null,
	`permissions` text not null,
	`is_mod` tinyint DEFAULT 0 not null,
	`is_admin` tinyint DEFAULT 0 not null,
	`is_banned` tinyint DEFAULT 0 not null,
	`tag` varchar(50) DEFAULT '' not null,
	primary key(`gid`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `forums`(
	`fid` int not null AUTO_INCREMENT,
	`name` varchar(100) not null,
	`active` tinyint DEFAULT 1 not null,
	`lastTopic` varchar(100) DEFAULT '' not null,
	`lastTopicID` int DEFAULT 0 not null,
	`lastReplyer` varchar(100) DEFAULT '' not null,
	`lastReplyerID` int DEFAULT 0 not null,
	`lastTopicTime` datetime not null,
	primary key(`fid`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `topics`(
	`tid` int not null AUTO_INCREMENT,
	`title` varchar(100) not null,
	`content` text not null,
	`parsed_content` text not null,
	`createdAt` datetime not null,
	`lastReplyAt` datetime not null,
	`createdBy` int not null,
	`is_closed` tinyint DEFAULT 0 not null,
	`sticky` tinyint DEFAULT 0 not null,
	`parentID` int DEFAULT 1 not null,
	`ipaddress` varchar(200) DEFAULT '0.0.0.0.0' not null,
	`data` varchar(200) DEFAULT '' not null,
	primary key(`tid`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `replies`(
	`rid` int not null AUTO_INCREMENT,
	`tid` int not null,
	`content` text not null,
	`parsed_content` text not null,
	`createdAt` datetime not null,
	`createdBy` int not null,
	`lastEdit` int not null,
	`lastEditBy` int not null,
	`ipaddress` varchar(200) DEFAULT '0.0.0.0.0' not null,
	primary key(`rid`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `users_replies`(
	`rid` int not null AUTO_INCREMENT,
	`uid` int not null,
	`content` text not null,
	`parsed_content` text not null,
	`createdAt` datetime not null,
	`createdBy` int not null,
	`lastEdit` int not null,
	`lastEditBy` int not null,
	primary key(`rid`)
) CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;

CREATE TABLE `settings`(
	`name` varchar(200) not null,
	`content` varchar(250) not null,
	`type` varchar(50) not null,
	`constraints` varchar(200) DEFAULT '' not null,
	unique(`name`)
);

CREATE TABLE `plugins`(
	`uname` varchar(200) not null,
	`active` tinyint DEFAULT 0 not null,
	unique(`uname`)
);

INSERT INTO settings(`name`,`content`,`type`) VALUES ('url_tags','1','bool');

INSERT INTO users(`name`,`group`,`is_super_admin`,`createdAt`,`lastActiveAt`,`message`) 
VALUES ('Admin',1,1,NOW(),NOW(),'');

INSERT INTO users_groups(`name`,`permissions`,`active`,`is_mod`,`is_admin`,`tag`) VALUES ('Administrator','{}',1,1,1,"Admin");
INSERT INTO users_groups(`name`,`permissions`,`is_mod`,`tag`) VALUES ('Moderator','{}',1,"Mod");
INSERT INTO users_groups(`name`,`permissions`) VALUES ('Member','{}');
INSERT INTO users_groups(`name`,`permissions`,`is_banned`) VALUES ('Banned','{}',1);

INSERT INTO forums(`name`,`lastTopicTime`) VALUES ('General',NOW());
INSERT INTO topics(`title`,`content`,`createdAt`,`lastReplyAt`,`createdBy`,`parentID`) 
VALUES ('Test Topic','A topic automatically generated by the software.',NOW(),NOW(),1,1);

INSERT INTO replies(`tid`,`content`,`createdAt`,`createdBy`,`lastEdit`,`lastEditBy`) 
VALUES (1,'Reply 1',NOW(),1,0,0);