/**
 * /config/database setup.php
 * @version 1.0
 * @desc Database setup
 * @author Fándly Gergő Zoltán (gergo@systemtest.tk, systemtest.tk)
 * @copy 2018 Fándly Gergő Zoltán
 * License:
    Systemtest.tk website's.
    Copyright (C) 2018  Fándly Gergő Zoltán

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 **/

CREATE TABLE `users`(
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `username` varchar(65) NOT NULL default '', /* optional */
    `fullname` varchar(65) NOT NULL default '',
    `email` varchar(65) NOT NULL default '',
    `accesslevel` tinyint(1) UNSIGNED NOT NULL default 0, /* 0-regular user; 1-blogger; 2-order handler/helpdesk; 3-admin */
    `quota` int(4) NOT NULL default 100, /* quota in MBs; -1 for unlimited */
    `orderer` varchar(35) default null,
    `password` varchar(255) NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`orderer`) REFERENCES orderers(`reference`) ON DELETE SET NULL
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `login_history`(
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `user` int(4) UNSIGNED NOT NULL default 1,   /* id of nouser */
    `date` timestamp NOT NULL default current_timestamp,
    `ip` varchar(45) NOT NULL default '0.0.0.0',
    `auth_token` varchar(65) NOT NULL default '',
    `user_agent` varchar(500) NOT NULL default '',
    `success` tinyint(1) NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`user`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `login_remember`(
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `user` int(4) UNSIGNED NOT NULL default 0,
    `remember_token` varchar(65) NOT NULL default '',
    `until` timestamp NOT NULL default current_timestamp,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`user`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `login_bans`(
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `ip` varchar(45) NOT NULL default '0.0.0.0',
    `until` timestamp NOT NULL default current_timestamp,
    PRIMARY KEY (`id`)
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `projects` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `name` varchar(65) NOT NULL default '',
    `description` text NOT NULL default '',
    `owner` int(4) UNSIGNED NOT NULL default 0,
    `path` varchar(125) NOT NULL default '',
    `repo` varchar(125) NOT NULL default '',
    `status` varchar(65) NOT NULL default '',
    `image` text NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`owner`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `files` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `token` varchar(35) NOT NULL default '', /* MD5 hash computed from {name}<<<>>>{random 16 char string} */
    `owner` int(4) UNSIGNED NOT NULL default 0,
    `name` varchar(65) NOT NULL default 0,
    `extension` varchar(20) NOT NULL default '.*',
    `size` int(4) UNSIGNED NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`owner`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `news` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `owner` int(4) UNSIGNED NOT NULL default 0,
    `date` timestamp NOT NULL default current_timestamp,
    `subject_eng` varchar(65) NOT NULL default '',
    `subject_hun` varchar(65) NOT NULL default '',
    `subject_rou` varchar(65) NOT NULL default '',
    `content_eng` text NOT NULL default '',
    `content_hun` text NOT NULL default '',
    `content_rou` text NOT NULL default '',
    `published` tinyint(1) UNSIGNED NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`owner`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `blog` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `title` varchar(65) NOT NULL default '',
    `owner` int(4) UNSIGNED NOT NULL default 0,
    `date` timestamp NOT NULL default current_timestamp,
    `content` text NOT NULL default '',
    `published` tinyint(1) UNSIGNED NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`owner`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `blog_tags` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `blogentry` int(4) UNSIGNED NOT NULL default 0,
    `tag` varchar(65) NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`blogentry`) REFERENCES blog(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `products` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `name_eng` varchar(65) NOT NULL default '',
    `name_hun` varchar(65) NOT NULL default '',
    `name_rou` varchar(65) NOT NULL default '',
    `description_eng` text NOT NULL default '',
    `description_hun` text NOT NULL default '',
    `description_rou` text NOT NULL default '',
    `price` int(4) NOT NULL default 0, /* if -2: to be announced, -1: contact us */
    PRIMARY KEY (`id`)
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `product_tags` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `product` int(4) UNSIGNED NOT NULL default 0,
    `tag` varchar(65) NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`product`) REFERENCES products(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `product_images` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `product` int(4) UNSIGNED NOT NULL default 0,
    `image` int(4) UNSIGNED NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`product`) REFERENCES products(`id`) ON DELETE CASCADE,
    FOREIGN KEY (`image`) REFERENCES files(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `product_reviews` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `orderid` int(4) UNSIGNED NOT NULL default 0,
    `writer` varchar(65) NOT NULL default 'Anonymous',
    `rating` tinyint(1) UNSIGNED NOT NULL default 5, /* from 1 to 5 */
    `comment` varchar(2000) NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`orderid`) REFERENCES orders(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `orderers` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `reference` varchar(35) NOT NULL default '', /* md5 hash computed from {current timestamp}<<<>>>{random 16 char string} */
    `name` text NOT NULL default '', /* encrypted */
    `address` text NOT NULL default '',
    `email` text NOT NULL default '',
    `phone` text NOT NULL default '',
    PRIMARY KEY (`id`),
    UNIQUE KEY (`reference`)
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `orders` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `reference` varchar(35) NOT NULL default '', /* md5 hash reference computed from {current timestamp}<<<>>>{random 16 char string} */
    `date` timestamp NOT NULL default current_timestamp,
    `product` int(4) UNSIGNED default 0,
    `quantity` int(4) UNSIGNED NOT NULL default 1,
    `orderer` varchar(35) default '',
    `comments` text NOT NULL default '',
    `status` tinyint(1) UNSIGNED NOT NULL default 0, /* 0:order placed; 1:preprocessing, produceing; 2:waiting payment; 3:payed; 4:packing; 5:shipped; 6:arrieved; 10:order cancelled */
    PRIMARY KEY (`id`),
    FOREIGN KEY (`product`) REFERENCES products(`id`) ON DELETE SET NULL,
    FOREIGN KEY (`orderer`) REFERENCES orderers(`reference`) ON DELETE SET NULL
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `messages` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `date` timestamp NOT NULL default current_timestamp,
    `subject` varchar(65) NOT NULL default '',
    `message` varchar(1500) NOT NULL default '',
    `blogentry` int(4) UNSIGNED default NULL,
    `product` int(4) UNSIGNED default NULL,
    `orderid` int(4) UNSIGNED default NULL,
    `replyemail` varchar(65) NOT NULL default '',
    PRIMARY KEY (`id`),
    FOREIGN KEY (`blogentry`) REFERENCES blog(`id`) ON DELETE SET NULL,
    FOREIGN KEY (`product`) REFERENCES products(`id`) ON DELETE SET NULL,
    FOREIGN KEY (`orderid`) REFERENCES orders(`id`) ON DELETE SET NULL
) CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `data_requests` (
    `id` int(4) UNSIGNED NOT NULL auto_increment,
    `date` timestamp NOT NULL default current_timestamp,
    `user` int(4) UNSIGNED NOT NULL default 1,
    `pgp` varchar(10000) NOT NULL default '',
    `finished` tinyint(1) NOT NULL default 0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`user`) REFERENCES users(`id`) ON DELETE CASCADE
) CHARACTER SET utf8 COLLATE utf8_general_ci;

INSERT INTO users (`id`, `username`) VALUES (1, 'nouser');
