-- SQL Schema for Crunch

-- Note all dates must be in epoch seconds to allow for sqlite comparison

-- User table

DROP TABLE IF EXISTS "users";

CREATE TABLE "users" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "username" TEXT NOT NULL UNIQUE,
    "password" TEXT NOT NULL, -- sha256 hash of plaintext password + salt
    "salt" TEXT NOT NULL, -- salt that is appended to the password
    "email" TEXT UNIQUE,
    "displayName" TEXT,
    "createDate" TEXT
);

-- Content Tables

DROP TABLE IF EXISTS "posts";

CREATE TABLE "posts" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "title" TEXT, -- The title of the post
    "slug" TEXT NOT NULL, -- A slug form of the title or id
    "markdown" TEXT NOT NULL, -- The post content in markdown format
    "postDate" INTEGER, -- The date that the post should go live and be sorted by
    "updatedDate" INTEGER, -- The date that the post was last updated
    "createDate" INTEGER NOT NULL, -- The date the post was created
    "lastGenerateDate" INTEGER, -- The date this post was last built
    "lastUpload" INTEGER -- The date the post was last uploaded
);

DROP TABLE IF EXISTS "galleries";

CREATE TABLE "galleries" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "title" TEXT, -- The title of the gallery
    "slug" TEXT NOT NULL UNIQUE, -- A slug form of the title or id
    "description" TEXT NOT NULL, -- The gallery description in markdown format
    "postDate" INTEGER, -- The date that the gallery should go live and be sorted by
    "updatedDate" INTEGER, -- The date that the gallery was last updated
    "createDate" INTEGER NOT NULL, -- The date the gallery was created
    "lastGenerateDate" INTEGER, -- The date this gallery was last built
    "lastUpload" INTEGER -- The date the gallery was last uploaded
);

DROP TABLE IF EXISTS "photos";

CREATE TABLE "photos" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "path" TEXT NOT NULL, -- The path of the photo location on disk
    "title" TEXT, -- The title of the photo
    "slug" TEXT NOT NULL UNIQUE, -- A slug form of the title or id
    "description" TEXT, -- The photo description in markdown format
    "photoDate" INTEGER, -- The date that the photo should go live and be sorted by
    "updatedDate" INTEGER, -- The date that the photo was last updated
    "createDate" INTEGER NOT NULL, -- The date the photo was created
    "lastGenerateDate" INTEGER, -- The date this photo was last built
    "lastUpload" INTEGER, -- The date the photo was last uploaded
    "extension" TEXT, -- The original extension of the photo
    "mimetype" TEXT, -- The original mimetype of the photo
    "orientation" TEXT, -- The correct orientation of the photo (landscape, portrait)
    "origWidth" INTEGER, -- The original width of the photo
    "origHeight" INTEGER, -- The original height of the photo
    "aperture" REAL, -- The aperture of the original photo
    "speed" REAL, -- The shutter speed of the original photo (in seconds)
    "location" TEXT -- The location data of the original photo
);

DROP TABLE IF EXISTS "galleryPhotos";

CREATE TABLE "galleryPhotos" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "galleryId" INTEGER NOT NULL, -- the id of the gallery
    "photoId" INTEGER NOT NULL, -- the id of the photo
    FOREIGN KEY(galleryId) REFERENCES galleries(id),
    FOREIGN KEY(photoId) REFERENCES photos(id)
);

DROP TABLE IF EXISTS "categories";

CREATE TABLE "categories" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT NOT NULL, -- The name of this category
    "slug" TEXT NOT NULL UNIQUE, -- A slug form of the name or id
    "description" TEXT, -- A brief description of the category in markdown
    "createDate" INTEGER NOT NULL -- The date the category was created
);

INSERT INTO "categories" VALUES (1, "Uncategorized", "uncategorized", "Uncategorized posts", (strftime('%s','now')*1000));

DROP TABLE IF EXISTS "categoryPosts";

CREATE TABLE "categoryPosts" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "categoryId" INTEGER NOT NULL, -- the id of the category
    "postId" INTEGER NOT NULL, -- the id of the post
    FOREIGN KEY(categoryId) REFERENCES categories(id),
    FOREIGN KEY(postId) REFERENCES posts(id)
);

DROP TABLE IF EXISTS "tags";

CREATE TABLE "tags" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT NOT NULL, -- The name of this tag
    "slug" TEXT NOT NULL UNIQUE, -- A slug form of the name or id
    "createDate" INTEGER NOT NULL -- The date the tag was created
);

DROP TABLE IF EXISTS "postTags";

CREATE TABLE "postTags" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "postId" INTEGER NOT NULL, -- the id of the post
    "tagId" INTEGER NOT NULL, -- the id of the tag
    FOREIGN KEY(postId) REFERENCES posts(id),
    FOREIGN KEY(tagId) REFERENCES tags(id)
);

DROP TABLE IF EXISTS "photoTags";

CREATE TABLE "photoTags" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "photoId" INTEGER NOT NULL, -- the id of the photo
    "tagId" INTEGER NOT NULL, -- the id of the tag
    FOREIGN KEY(photoId) REFERENCES photos(id),
    FOREIGN KEY(tagId) REFERENCES tags(id)
);
