CREATE TABLE IF NOT EXISTS artifacts (
    slug           TEXT PRIMARY KEY,
    title          TEXT NOT NULL DEFAULT '',
    size_bytes     INTEGER NOT NULL,
    password_hash  TEXT,
    created_at     INTEGER NOT NULL,
    expires_at     INTEGER,
    view_count     INTEGER NOT NULL DEFAULT 0,
    last_viewed_at INTEGER
);

CREATE INDEX IF NOT EXISTS idx_artifacts_created_at ON artifacts(created_at DESC);

CREATE TABLE IF NOT EXISTS views (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    slug       TEXT NOT NULL,
    viewed_at  INTEGER NOT NULL,
    visitor    TEXT NOT NULL,
    referrer   TEXT,
    FOREIGN KEY (slug) REFERENCES artifacts(slug) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_views_slug_time ON views(slug, viewed_at);
CREATE INDEX IF NOT EXISTS idx_views_time ON views(viewed_at);

CREATE TABLE IF NOT EXISTS daily_stats (
    slug     TEXT NOT NULL,
    day      TEXT NOT NULL,
    views    INTEGER NOT NULL DEFAULT 0,
    uniques  INTEGER NOT NULL DEFAULT 0,
    PRIMARY KEY (slug, day),
    FOREIGN KEY (slug) REFERENCES artifacts(slug) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS idx_daily_stats_day ON daily_stats(day);

CREATE TABLE IF NOT EXISTS unlock_attempts (
    id        INTEGER PRIMARY KEY AUTOINCREMENT,
    slug      TEXT NOT NULL,
    at        INTEGER NOT NULL,
    visitor   TEXT NOT NULL,
    success   INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_unlock_attempts_slug_time ON unlock_attempts(slug, at);

CREATE TABLE IF NOT EXISTS meta (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);
