CREATE TABLE payout_queue (
    `payout_id` String,
    `merchant_id` String,
    `customer_id` String,
    `address_id` String,
    `payout_type` LowCardinality(String),
    `payout_method_id` Nullable(String),
    `amount` UInt64,
    `destination_currency` LowCardinality(String),
    `source_currency` LowCardinality(String),
    `description` Nullable(String),
    `recurring` Bool,
    `auto_fulfill` Bool,
    `return_url` Nullable(String),
    `entity_type` LowCardinality(String),
    `metadata` Nullable(String),
    `created_at` DateTime CODEC(T64, LZ4),
    `last_modified_at` DateTime CODEC(T64, LZ4),
    `attempt_count` UInt16,
    `profile_id` String,
    `status` LowCardinality(String),
    `sign_flag` Int8
) ENGINE = Kafka SETTINGS kafka_broker_list = 'kafka0:29092',
kafka_topic_list = 'hyperswitch-payout-events',
kafka_group_name = 'hyper-c1',
kafka_format = 'JSONEachRow',
kafka_handle_error_mode = 'stream';

CREATE TABLE payout (
    `payout_id` String,
    `merchant_id` String,
    `customer_id` String,
    `address_id` String,
    `payout_type` LowCardinality(String),
    `payout_method_id` Nullable(String),
    `amount` UInt64,
    `destination_currency` LowCardinality(String),
    `source_currency` LowCardinality(String),
    `description` Nullable(String),
    `recurring` Bool,
    `auto_fulfill` Bool,
    `return_url` Nullable(String),
    `entity_type` LowCardinality(String),
    `metadata` Nullable(String),
    `created_at` DateTime DEFAULT now() CODEC(T64, LZ4),
    `last_modified_at` DateTime DEFAULT now() CODEC(T64, LZ4),
    `attempt_count` UInt16,
    `profile_id` String,
    `status` LowCardinality(String),
    `inserted_at` DateTime DEFAULT now() CODEC(T64, LZ4),
    `sign_flag` Int8,
    INDEX payoutTypeIndex payout_type TYPE bloom_filter GRANULARITY 1,
    INDEX destinationCurrencyIndex destination_currency TYPE bloom_filter GRANULARITY 1,
    INDEX sourceCurrencyIndex source_currency TYPE bloom_filter GRANULARITY 1,
    INDEX entityTypeIndex entity_type TYPE bloom_filter GRANULARITY 1,
    INDEX statusIndex status TYPE bloom_filter GRANULARITY 1
) ENGINE = CollapsingMergeTree(sign_flag) PARTITION BY toStartOfDay(created_at)
ORDER BY
    (created_at, merchant_id, payout_id) TTL created_at + toIntervalMonth(6);

CREATE MATERIALIZED VIEW kafka_parse_payout TO payout (
    `payout_id` String,
    `merchant_id` String,
    `customer_id` String,
    `address_id` String,
    `payout_type` LowCardinality(String),
    `payout_method_id` Nullable(String),
    `amount` UInt64,
    `destination_currency` LowCardinality(String),
    `source_currency` LowCardinality(String),
    `description` Nullable(String),
    `recurring` Bool,
    `auto_fulfill` Bool,
    `return_url` Nullable(String),
    `entity_type` LowCardinality(String),
    `metadata` Nullable(String),
    `created_at` DateTime64(3),
    `last_modified_at` DateTime64(3),
    `attempt_count` UInt16,
    `profile_id` String,
    `status` LowCardinality(String),
    `inserted_at` DateTime64(3),
    `sign_flag` Int8
) AS
SELECT
    payout_id,
    merchant_id,
    customer_id,
    address_id,
    payout_type,
    payout_method_id,
    amount,
    destination_currency,
    source_currency,
    description,
    recurring,
    auto_fulfill,
    return_url,
    entity_type,
    metadata,
    created_at,
    last_modified_at,
    attempt_count,
    profile_id,
    status,
    now() as inserted_at,
    sign_flag
FROM
    payout_queue;