CREATE TABLE IF NOT EXISTS qrust.assets (
    symbol LowCardinality(String),
    abbreviation LowCardinality(String),
    class Enum('us_equity' = 1, 'crypto' = 2),
    exchange Enum(
        'AMEX' = 1,
        'ARCA' = 2,
        'BATS' = 3,
        'NASDAQ' = 4,
        'NYSE' = 5,
        'NYSEARCA' = 6,
        'OTC' = 7,
        'CRYPTO' = 8
    ),
    time_added DateTime DEFAULT now(),
    CONSTRAINT abbreviation ASSUME replace(symbol, '/', '') = abbreviation
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY symbol;

CREATE TABLE IF NOT EXISTS qrust.bars (
    symbol LowCardinality(String),
    time DateTime,
    open Float64,
    high Float64,
    low Float64,
    close Float64,
    volume Float64,
    trades Int64,
    vwap Float64
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY (symbol, time)
PARTITION BY toYYYYMM(time);

CREATE TABLE IF NOT EXISTS qrust.backfills_bars (
    symbol LowCardinality(String),
    time DateTime
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY symbol;

CREATE TABLE IF NOT EXISTS qrust.news (
    id Int64,
    time_created DateTime,
    time_updated DateTime,
    symbols Array(LowCardinality(String)),
    headline String,
    author String,
    content String,
    sentiment Enum(
        'very_negative' = -3,
        'negative' = -2,
        'mildly_negative' = -1,
        'neutral' = 0,
        'mildly_positive' = 1,
        'positive' = 2,
        'very_positive' = 3
    ),
    confidence Enum(
        'very_uncertain' = -3,
        'uncertain' = -2,
        'mildly_uncertain' = -1,
        'neutral' = 0,
        'mildly_certain' = 1,
        'certain' = 2,
        'very_certain' = 3
    ),
    INDEX index_symbols symbols TYPE bloom_filter()
)
ENGINE = ReplacingMergeTree()
PARTITION BY toYYYYMM(time_created)
PRIMARY KEY id;

CREATE TABLE IF NOT EXISTS qrust.backfills_news (
    symbol LowCardinality(String),
    time DateTime
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY symbol;
