-- Trade Intelligence — External API Data Store
-- Sources: UN Comtrade, IMF, World Bank, Eurostat

CREATE DATABASE IF NOT EXISTS trade_intelligence;

-- UN Comtrade: bilateral trade flows
CREATE TABLE IF NOT EXISTS trade_intelligence.comtrade
(
    id             UUID    DEFAULT generateUUIDv4(),
    period         UInt32,                          -- YYYYMM or YYYY
    period_type    LowCardinality(String),           -- M=monthly, A=annual
    reporter_code  UInt16,
    reporter       String,
    partner_code   UInt16,
    partner        String,
    trade_flow     LowCardinality(String),           -- Import | Export | Re-Import | Re-Export
    commodity_code String,
    commodity      String,
    netweight_kg   Float64,
    trade_value_usd Float64,
    qty            Float64,
    qty_unit       LowCardinality(String),
    fetched_at     DateTime DEFAULT now()
)
ENGINE = ReplacingMergeTree(fetched_at)
ORDER BY (period, reporter_code, partner_code, trade_flow, commodity_code)
PARTITION BY toString(intDiv(period, 100));

-- IMF World Economic Outlook
CREATE TABLE IF NOT EXISTS trade_intelligence.imf_weo
(
    country_code   LowCardinality(String),
    country        String,
    indicator_code LowCardinality(String),
    indicator      String,
    year           UInt16,
    value          Nullable(Float64),
    unit           LowCardinality(String),
    scale          LowCardinality(String),
    fetched_at     DateTime DEFAULT now()
)
ENGINE = ReplacingMergeTree(fetched_at)
ORDER BY (country_code, indicator_code, year);

-- World Bank Open Data
CREATE TABLE IF NOT EXISTS trade_intelligence.worldbank
(
    country_code   LowCardinality(String),
    country        String,
    indicator_code LowCardinality(String),
    indicator      String,
    year           UInt16,
    value          Nullable(Float64),
    fetched_at     DateTime DEFAULT now()
)
ENGINE = ReplacingMergeTree(fetched_at)
ORDER BY (country_code, indicator_code, year);

-- Eurostat: Comext trade data (intra+extra EU)
CREATE TABLE IF NOT EXISTS trade_intelligence.eurostat_comext
(
    period         UInt32,
    reporter       LowCardinality(String),
    partner        LowCardinality(String),
    product        String,                           -- CN8 code
    flow           LowCardinality(String),           -- 1=Import 2=Export
    value_eur      Float64,
    quantity_ton   Float64,
    stat_value     Float64,
    fetched_at     DateTime DEFAULT now()
)
ENGINE = ReplacingMergeTree(fetched_at)
ORDER BY (period, reporter, partner, product, flow)
PARTITION BY toString(intDiv(period, 100));

-- Predictions: AI-generated trade forecasts stored here
CREATE TABLE IF NOT EXISTS trade_intelligence.predictions
(
    id             UUID    DEFAULT generateUUIDv4(),
    created_at     DateTime DEFAULT now(),
    commodity_code String,
    commodity      String,
    reporter       String,
    partner        String,
    horizon_months UInt8,
    predicted_value_usd Float64,
    confidence     Float32,
    model          LowCardinality(String),
    features       String,   -- JSON
    rationale      String
)
ENGINE = MergeTree()
ORDER BY (created_at, commodity_code, reporter);
