-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
CREATE TABLE IF NOT EXISTS users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    username TEXT DEFAULT "",
    created DATETIME DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE IF NOT EXISTS chats (
    id INTEGER PRIMARY KEY,
    name TEXT,
    username TEXT DEFAULT "",
    created DATETIME DEFAULT CURRENT_TIMESTAMP
);


CREATE TABLE IF NOT EXISTS admins (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL,
    chat_id INTEGER NOT NULL,
    created DATETIME DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (user_id) REFERENCES users (id),
    FOREIGN KEY (chat_id) REFERENCES chats (id)
);


CREATE TABLE IF NOT EXISTS karma (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    from_user INTEGER NOT NULL,
    to_user INTEGER NOT NULL,
    change INTEGER,
    message TEXT DEFAULT "",
    created DATETIME DEFAULT CURRENT_TIMESTAMP,
    CHECK(from_user != to_user),
    
    FOREIGN KEY (from_user) REFERENCES users(id),
    FOREIGN KEY (to_user) REFERENCES users(id)
);


CREATE TABLE IF NOT EXISTS warnings (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    user INTEGER NOT NULL,
    admin INTEGER NOT NULL,
    message_id INTEGER DEFAULT 0,
    topic_id INTEGER DEFAULT 0,
    chat_id INTEGER NOT NULL,
    active INTEGER DEFAULT 1,
    reason TEXT DEFAULT "",
    
    created DATETIME DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (user) REFERENCES users (id),
    FOREIGN KEY (admin) REFERENCES users (id),
    FOREIGN KEY (chat_id) REFERENCES chats (id)
);

CREATE TABLE IF NOT EXISTS admin_topics (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    chat_id INTEGER NOT NULL,
    topic_id INTEGER NOT NULL,

    created DATETIME DEFAULT CURRENT_TIMESTAMP,

    FOREIGN KEY (chat_id) REFERENCES chats (id)
);

CREATE TABLE IF NOT EXISTS message_templates (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    tag TEXT NOT NULL,
    text TEXT NOT NULL,
    triggers TEXT,

    created DATETIME DEFAULT CURRENT_TIMESTAMP
);

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
DROP TABLE users ;
DROP TABLE chats ;
DROP TABLE admins ;
DROP TABLE karma ;
DROP TABLE warnings ;
DROP TABLE admin_topics ;
DROP TABLE message_templates ;

-- +goose StatementEnd
