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,
    org String,
    domain 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);



-- Suppression si existe pour reconfiguration
DROP TABLE IF EXISTS ja4_processing.ref_bot_networks;

-- Table optimisée pour le filtrage binaire de CIDR
CREATE TABLE ja4_processing.ref_bot_networks (
    network IPv6CIDR,          -- Gère nativement '1.2.3.0/24' et '2001:db8::/32'
    bot_name LowCardinality(String),
    is_legitimate UInt8,       -- 1 = Whitelist, 0 = Blacklist
    last_update DateTime
) ENGINE = ReplacingMergeTree(last_update)
ORDER BY (network, bot_name)
