CREATE DATABASE vintagecoding;
USE vintagecoding;

CREATE TABLE roles (
  role_id INT PRIMARY KEY AUTO_INCREMENT,
  role VARCHAR(16) NOT NULL
);

CREATE TABLE tags (
  tag_id INT PRIMARY KEY AUTO_INCREMENT,
  tag VARCHAR(16) NOT NULL
);

CREATE TABLE user (
  user_id INT PRIMARY KEY,
  username VARCHAR(32) NOT NULL UNIQUE,
  email VARCHAR(64) NOT NULL UNIQUE,
  password_hash VARCHAR(256) NOT NULL,
  last_active TIMESTAMP DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  role_id INT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  is_active BOOLEAN DEFAULT FALSE,
  profile_picture VARCHAR(256),
  FOREIGN KEY (role_id) REFERENCES roles (role_id)
);

CREATE TABLE article (
  article_id INT PRIMARY KEY AUTO_INCREMENT,
  author_id INT NOT NULL,
  title VARCHAR(26) NOT NULL,
  slug VARCHAR(32),
  read_count INT DEFAULT 0,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  published_at TIMESTAMP DEFAULT NULL,
  published BOOLEAN DEFAULT FALSE,
  excerpt VARCHAR(256),
  FOREIGN KEY (author_id) REFERENCES user(user_id)
);

CREATE TABLE article_tags (
  article_id INT NOT NULL,
  tag_id INT NOT NULL,
  PRIMARY KEY (article_id, tag_id),
  FOREIGN KEY (article_id) REFERENCES article(article_id),
  FOREIGN KEY (tag_id) REFERENCES tags(tag_id)
);

INSERT INTO roles (role)
  VALUES ('owner'), ('admin'), ('contributor'), ('reader');

INSERT INTO tags (tag)
  VALUES ('linux'), ('web-dev'), ('raspberry-pi'), ('self-hosting'), ('devlog'), ('testing');

INSERT INTO user (user_id, username, email, password_hash, role_id, profile_picture)
  VALUES (2025, 'quaxlyqueen', 'me@joshashton.dev', 'changeme', 1, '2025.jpg');

INSERT INTO article (author_id, title, excerpt, published_at)
  VALUES (2025, 'Building vintagecoding.net', 'This site is intended to be a platform for exposing, celebrating, and sharing the style of Vintage Coding. Topics from hardware to software and kernel to web, this site aims to cover it all.', CURRENT_TIMESTAMP);

INSERT INTO article (author_id, title, excerpt, published_at)
  VALUES (2025, 'Testing vintagecoding.net', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas a mauris sit amet dui pellentesque sodales. Pellentesque non viverra leo, et congue metus.', CURRENT_TIMESTAMP);

INSERT INTO article_tags (article_id, tag_id) VALUES (1, 2), (1, 4), (1, 5);
INSERT INTO article_tags (article_id, tag_id) VALUES (2, 2), (2, 6);
