CREATE TABLE users (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    username   TEXT    NOT NULL,
    password   TEXT    NOT NULL,
    created_at INTEGER NOT NULL
);
CREATE UNIQUE INDEX idx_users_username ON users (username);

CREATE TABLE sites (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    owner_id   INTEGER NOT NULL,
    title      TEXT    NOT NULL,
    tagline    TEXT    NOT NULL,
    created_at INTEGER NOT NULL,

    FOREIGN KEY (owner_id) REFERENCES users (id) ON DELETE CASCADE
);
CREATE INDEX idx_site_owner ON sites (owner_id);

CREATE TABLE publish_targets (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    site_id     INTEGER NOT NULL,
    target_type TEXT    NOT NULL,
    enabled     INT     NOT NULL,
    base_url    TEXT    NOT NULL,
    target_ref  TEXT    NOT NULL,
    target_key  TEXT    NOT NULL
);
CREATE INDEX idx_publish_targets_site ON publish_targets (site_id);

CREATE TABLE posts (
    id           INTEGER PRIMARY KEY AUTOINCREMENT,
    site_id      INTEGER NOT NULL,
    state        INTEGER NOT NULL,
    guid         TEXT    NOT NULL,
    title        TEXT    NOT NULL,
    body         TEXT    NOT NULL,
    slug         TEXT    NOT NULL,
    created_at   INTEGER NOT NULL,
    updated_at   INTEGER NOT NULL,
    published_at INTEGER NOT NULL,
    deleted_at   INTEGER NOT NULL
);
CREATE INDEX idx_post_site ON posts (site_id);
CREATE UNIQUE INDEX idx_post_guid ON posts (guid);