-- Telegram Helper Bot Database Schema
-- Compatible with Docker container deployment

-- System table for SQLite auto-increment sequences
CREATE TABLE IF NOT EXISTS sqlite_sequence (
    name TEXT NOT NULL,
    seq INTEGER NOT NULL
);

-- Users who have listened to audio messages
CREATE TABLE IF NOT EXISTS listen_audio_users (
    file_name TEXT NOT NULL,
    user_id INTEGER NOT NULL,
    is_listen BOOLEAN NOT NULL DEFAULT 0
);

-- Reference table for audio messages
CREATE TABLE IF NOT EXISTS audio_message_reference (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    file_name TEXT NOT NULL UNIQUE,
    author_id INTEGER NOT NULL,
    date_added DATE NOT NULL,
    listen_count INTEGER NOT NULL DEFAULT 0,
    file_id INTEGER NOT NULL
);

-- Database migrations tracking
CREATE TABLE IF NOT EXISTS migrations (
    version INTEGER NOT NULL PRIMARY KEY,
    script_name TEXT NOT NULL,
    created_at TEXT NOT NULL
);

-- Bot administrators
CREATE TABLE IF NOT EXISTS admins (
    user_id INTEGER NOT NULL PRIMARY KEY,
    role TEXT
);

-- User blacklist for banned users
CREATE TABLE IF NOT EXISTS blacklist (
    user_id INTEGER NOT NULL PRIMARY KEY,
    user_name TEXT,
    message_for_user TEXT,
    date_to_unban INTEGER
);

-- User message history
CREATE TABLE IF NOT EXISTS user_messages (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    message_text TEXT,
    user_id INTEGER,
    message_id INTEGER NOT NULL,
    date TEXT NOT NULL
);

-- Suggested posts from Telegram
CREATE TABLE IF NOT EXISTS post_from_telegram_suggest (
    message_id INTEGER NOT NULL PRIMARY KEY,
    text TEXT,
    helper_text_message_id INTEGER,
    author_id INTEGER,
    created_at TEXT NOT NULL
);

-- Links between posts and content
CREATE TABLE IF NOT EXISTS message_link_to_content (
    post_id INTEGER NOT NULL,
    message_id INTEGER NOT NULL,
    PRIMARY KEY (post_id, message_id)
);

-- Content associated with Telegram posts
CREATE TABLE IF NOT EXISTS content_post_from_telegram (
    message_id INTEGER NOT NULL,
    content_name TEXT NOT NULL,
    content_type TEXT,
    PRIMARY KEY (message_id, content_name)
);

-- Bot users information
CREATE TABLE IF NOT EXISTS our_users (
    id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    user_id INTEGER NOT NULL UNIQUE,
    first_name TEXT,
    full_name TEXT,
    username TEXT,
    is_bot BOOLEAN DEFAULT 0,
    language_code TEXT,
    has_stickers INTEGER DEFAULT 0 NOT NULL,
    emoji TEXT,
    date_added DATE NOT NULL,
    date_changed DATE NOT NULL
);

-- Audio moderation tracking
CREATE TABLE IF NOT EXISTS audio_moderate (
    user_id INTEGER NOT NULL,
    message_id INTEGER,
    PRIMARY KEY (user_id, message_id)
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_listen_audio_users_file_name ON listen_audio_users(file_name);
CREATE INDEX IF NOT EXISTS idx_listen_audio_users_user_id ON listen_audio_users(user_id);
CREATE INDEX IF NOT EXISTS idx_audio_message_reference_author_id ON audio_message_reference(author_id);
CREATE INDEX IF NOT EXISTS idx_user_messages_user_id ON user_messages(user_id);
CREATE INDEX IF NOT EXISTS idx_post_from_telegram_suggest_author_id ON post_from_telegram_suggest(author_id);
CREATE INDEX IF NOT EXISTS idx_our_users_user_id ON our_users(user_id);
CREATE INDEX IF NOT EXISTS idx_audio_moderate_user_id ON audio_moderate(user_id);
