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,
  role_id INT NOT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  last_active TIMESTAMP DEFAULT NULL ON UPDATE 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 TRUE,
  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', '057ba03d6c44104863dc7361fe4578965d1887360f90a0895882e58a6248fc86', 1, '2025.jpg');

INSERT INTO article (author_id, title, excerpt, published_at)
  VALUES (2025, 'Building vintagecoding.net', 'A devlog of the creation of vintagecoding.net and a test at the formatting of article cards and articles. Let the great experiment begin!', CURRENT_TIMESTAMP);

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