-- =============================================================================
-- 02_dictionaries.sql — ASN geo dictionary + bot reference tables
-- =============================================================================

-- -----------------------------------------------------------------------------
-- IPLocate ASN geo-location dictionary
-- CSV file must be placed at: /var/lib/clickhouse/user_files/iplocate-ip-to-asn.csv
-- -----------------------------------------------------------------------------
DROP DICTIONARY IF EXISTS ja4_processing.dict_iplocate_asn;

CREATE DICTIONARY IF NOT EXISTS ja4_processing.dict_iplocate_asn
(
    network      String,
    asn          UInt32,
    country_code String,
    name         String
)
PRIMARY KEY network
SOURCE(FILE(path '/var/lib/clickhouse/user_files/iplocate-ip-to-asn.csv' format 'CSVWithNames'))
LAYOUT(IP_TRIE())
LIFETIME(MIN 3600 MAX 7200);


-- -----------------------------------------------------------------------------
-- Bot network CIDR reference table (ReplacingMergeTree for upserts)
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS ja4_processing.ref_bot_networks
(
    network       String,
    bot_name      LowCardinality(String),
    is_legitimate UInt8,
    last_update   DateTime
)
ENGINE = ReplacingMergeTree(last_update)
ORDER BY (network, bot_name);


-- -----------------------------------------------------------------------------
-- Bot IP flat table (CSV file engine)
-- CSV file must be placed at: /var/lib/clickhouse/user_files/bot_ip.csv
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS ja4_processing.bot_ip
(
    ip String
)
ENGINE = File(CSV, 'bot_ip.csv');


-- -----------------------------------------------------------------------------
-- Bot JA4 flat table (CSV file engine)
-- CSV file must be placed at: /var/lib/clickhouse/user_files/bot_ja4.csv
-- -----------------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS ja4_processing.bot_ja4
(
    ja4 String
)
ENGINE = File(CSV, 'bot_ja4.csv');
