SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;


CREATE TABLE `coffee` (
  `id` char(36) NOT NULL,
  `date_created` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
DELIMITER $$
CREATE TRIGGER `coffee_stats_update` AFTER INSERT ON `coffee` FOR EACH ROW BEGIN
    DECLARE count_recent INT;
    DECLARE count_average INT;

    
    DELETE FROM coffee_stats;

    
    SELECT COUNT(*) INTO count_recent
    FROM coffee
    WHERE date_created > NOW() - INTERVAL 7 DAY;
    
    
    SELECT COUNT(*) / COUNT(DISTINCT YEAR(date_created), WEEK(date_created))
    INTO count_average
    FROM coffee;

    
    INSERT INTO coffee_stats (count_week, count_week_average) VALUES (count_recent, count_average);
END
$$
DELIMITER ;

CREATE TABLE `coffee_stats` (
  `count_week` smallint(5) UNSIGNED NOT NULL DEFAULT 0,
  `count_week_average` smallint(5) UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `languages` (
  `id` char(36) NOT NULL,
  `name` varchar(255) NOT NULL,
  `bytes` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `messages` (
  `id` char(36) NOT NULL,
  `email` varchar(255) DEFAULT NULL,
  `message` text NOT NULL,
  `date_created` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `search` (
  `id` char(36) NOT NULL,
  `query` text NOT NULL,
  `type` enum('WORK') NOT NULL,
  `title` varchar(255) NOT NULL,
  `text` text DEFAULT NULL,
  `href` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `work` (
  `id` char(36) NOT NULL,
  `namespace` varchar(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `summary` text DEFAULT NULL,
  `date_created` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `work_actions` (
  `id` char(36) NOT NULL,
  `ref_work_id` char(36) NOT NULL,
  `order_idx` tinyint(3) UNSIGNED NOT NULL DEFAULT 0,
  `href` varchar(255) DEFAULT NULL,
  `text` varchar(255) NOT NULL,
  `classlist` varchar(255) DEFAULT NULL,
  `icon_prepend` varchar(255) DEFAULT NULL,
  `icon_append` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `work_tags` (
  `id` char(36) NOT NULL,
  `ref_work_id` char(36) NOT NULL,
  `label` enum('VLW','WEBSITE','REPO') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE `work_timeline` (
  `id` char(36) NOT NULL,
  `ref_work_id` char(36) NOT NULL,
  `year` smallint(5) UNSIGNED NOT NULL,
  `month` tinyint(3) UNSIGNED NOT NULL,
  `day` tinyint(3) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;


ALTER TABLE `coffee`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `languages`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `name` (`name`);

ALTER TABLE `messages`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `search`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `search` ADD FULLTEXT KEY `query` (`query`);

ALTER TABLE `work`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `pathname` (`namespace`);

ALTER TABLE `work_actions`
  ADD PRIMARY KEY (`id`),
  ADD KEY `ref_work_id` (`ref_work_id`);

ALTER TABLE `work_tags`
  ADD PRIMARY KEY (`id`),
  ADD KEY `ref_work_id` (`ref_work_id`);

ALTER TABLE `work_timeline`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `ref_work_id` (`ref_work_id`);


ALTER TABLE `work_actions`
  ADD CONSTRAINT `work_actions_ibfk_1` FOREIGN KEY (`ref_work_id`) REFERENCES `work` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `work_tags`
  ADD CONSTRAINT `work_tags_ibfk_1` FOREIGN KEY (`ref_work_id`) REFERENCES `work` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `work_timeline`
  ADD CONSTRAINT `work_timeline_ibfk_1` FOREIGN KEY (`ref_work_id`) REFERENCES `work` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
