CREATE DATABASE IF NOT EXISTS qrust;

CREATE TABLE IF NOT EXISTS qrust.assets (
    symbol 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
    ),
    status Boolean,
    time_added DateTime DEFAULT now(),
    qty Float64
)
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
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY (symbol, time)
PARTITION BY toYYYYMM(time);

CREATE TABLE IF NOT EXISTS qrust.backfills_bars (
    symbol LowCardinality(String),
    time DateTime,
    fresh Boolean
)
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,
    source String,
    summary String,
    content String,
    url String,
    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,
    fresh Boolean
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY symbol;

CREATE TABLE IF NOT EXISTS qrust.orders (
    id UUID,
    client_order_id UUID,
    time_submitted DateTime,
    time_created DateTime,
    time_updated DateTime,
    time_filled DateTime,
    time_expired DateTime,
    time_cancel_requested DateTime,
    time_canceled DateTime,
    time_failed DateTime,
    time_replaced DateTime,
    replaced_by UUID,
    replaces UUID,
    symbol LowCardinality(String),
    order_class Enum('simple' = 1, 'bracket' = 2, 'oco' = 3, 'oto' = 4),
    order_type Enum('market' = 1, 'limit' = 2, 'stop' = 3, 'stop_limit' = 4, 'trailing_stop' = 5),
    side Enum('buy' = 1, 'sell' = -1),
    time_in_force Enum('day' = 1, 'gtc' = 2, 'opg' = 3, 'cls' = 4, 'ioc' = 5, 'fok' = 6),
    extended_hours Boolean,
    notional Float64,
    qty Float64,
    filled_qty Float64,
    filled_avg_price Float64,
    status Enum(
        'new' = 1,
        'partially_filled' = 2,
        'filled' = 3,
        'done_for_day' = 4,
        'canceled' = 5,
        'expired' = 6,
        'replaced' = 7,
        'pending_cancel' = 8,
        'pending_replace' = 9,
        'accepted' = 10,
        'pending_new' = 11,
        'accepted_for_bidding' = 12,
        'stopped' = 13,
        'rejected' = 14,
        'suspended' = 15,
        'calculated' = 16
    ),
    limit_price Float64,
    stop_price Float64,
    trail_percent Float64,
    trail_price Float64,
    hwm Float64,
    legs Array(UUID)
)
ENGINE = ReplacingMergeTree()
PARTITION BY toYYYYMM(time_submitted)
PRIMARY KEY id;

CREATE TABLE IF NOT EXISTS qrust.calendar (
    date Date,
    open DateTime,
    close DateTime
)
ENGINE = ReplacingMergeTree()
PRIMARY KEY date;
