-- ClickHouse: создание базы и таблицы для логирования пользовательских событий
-- Запуск: clickhouse-client --multiquery < migrations/20260314_clickhouse_events.sql

CREATE DATABASE IF NOT EXISTS kadrum_analytics;

DROP TABLE IF EXISTS kadrum_analytics.events_raw;

CREATE TABLE IF NOT EXISTS kadrum_analytics.events_raw
(
    event_time     DateTime64(3, 'Asia/Krasnoyarsk'),
    event_date     Date DEFAULT toDate(event_time),

    user_id        UInt32,
    telegram_id    String DEFAULT '',
    session_uuid   String DEFAULT '',

    platform       LowCardinality(String),
    region         LowCardinality(String),
    product        LowCardinality(String),

    event_name     LowCardinality(String),
    event_category LowCardinality(String),

    params         String DEFAULT '{}',

    source_id      UInt32  DEFAULT 0,
    ref_id         UInt32  DEFAULT 0,
    partner_id     UInt32  DEFAULT 0
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_name, event_date, user_id, event_time)
SETTINGS index_granularity = 8192;
