-- =====================================================
-- ML SCHEMA - FEATURE STORE TABLE
-- =====================================================
-- Description: Pre-calculated features for ML models
-- Schema: ml
-- Author: Database Agent
-- Date: 2025-12-06
-- =====================================================

CREATE TABLE ml.feature_store (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Identificación del feature set
  symbol VARCHAR(20) NOT NULL,
  timeframe VARCHAR(10) NOT NULL,
  timestamp TIMESTAMPTZ NOT NULL,

  -- Features técnicos
  technical_features JSONB, -- RSI, MACD, Bollinger, SMA, EMA, etc.

  -- Features de volumen
  volume_features JSONB, -- Volume profiles, OBV, VWAP, etc.

  -- Features de precio
  price_features JSONB, -- Price changes, returns, volatility, etc.

  -- Features de sentimiento (si disponible)
  sentiment_features JSONB, -- Social sentiment, news sentiment, etc.

  -- Features on-chain (para crypto)
  onchain_features JSONB, -- Network metrics, whale activity, etc.

  -- Features derivados
  derived_features JSONB, -- Features calculados de otros features

  -- Metadata
  feature_version VARCHAR(20), -- Versión del cálculo de features
  calculation_timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  calculation_duration_ms INTEGER,

  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

  -- Constraints
  CONSTRAINT unique_feature_set UNIQUE(symbol, timeframe, timestamp)
);

-- Índices
CREATE INDEX idx_feature_store_symbol ON ml.feature_store(symbol);
CREATE INDEX idx_feature_store_symbol_time ON ml.feature_store(symbol, timeframe, timestamp DESC);
CREATE INDEX idx_feature_store_timestamp ON ml.feature_store(timestamp DESC);
CREATE INDEX idx_feature_store_version ON ml.feature_store(feature_version);

-- Particionamiento por fecha (recomendado para alto volumen)
-- Se puede implementar particionamiento mensual o trimestral

-- Comentarios
COMMENT ON TABLE ml.feature_store IS 'Pre-calculated features for ML model inference and training';
COMMENT ON COLUMN ml.feature_store.symbol IS 'Trading symbol (e.g., BTCUSDT)';
COMMENT ON COLUMN ml.feature_store.timeframe IS 'Timeframe for features (1m, 5m, 15m, 1h, 4h, 1d)';
COMMENT ON COLUMN ml.feature_store.timestamp IS 'Timestamp of the candle/bar these features represent';
COMMENT ON COLUMN ml.feature_store.feature_version IS 'Version of feature calculation logic';

-- Ejemplos de features JSONB:
COMMENT ON COLUMN ml.feature_store.technical_features IS
'Example: {
  "rsi_14": 65.42,
  "rsi_9": 68.21,
  "macd": 0.0234,
  "macd_signal": 0.0189,
  "macd_histogram": 0.0045,
  "bb_upper": 45678.90,
  "bb_middle": 45234.12,
  "bb_lower": 44789.34,
  "sma_20": 45123.45,
  "ema_12": 45234.56,
  "ema_26": 45012.34
}';

COMMENT ON COLUMN ml.feature_store.volume_features IS
'Example: {
  "volume": 1234567.89,
  "volume_sma_20": 1123456.78,
  "volume_ratio": 1.098,
  "obv": 98765432.10,
  "vwap": 45234.12,
  "buy_volume": 678901.23,
  "sell_volume": 555666.66
}';

COMMENT ON COLUMN ml.feature_store.price_features IS
'Example: {
  "open": 45100.00,
  "high": 45500.00,
  "low": 44900.00,
  "close": 45234.12,
  "price_change_1h": 0.0145,
  "price_change_4h": 0.0234,
  "price_change_24h": 0.0567,
  "volatility_1h": 0.0089,
  "atr_14": 234.56
}';

COMMENT ON COLUMN ml.feature_store.sentiment_features IS
'Example: {
  "social_sentiment": 0.72,
  "news_sentiment": 0.65,
  "fear_greed_index": 68,
  "twitter_volume": 12345,
  "reddit_mentions": 678
}';

COMMENT ON COLUMN ml.feature_store.onchain_features IS
'Example: {
  "active_addresses": 123456,
  "transaction_volume": 98765.43,
  "exchange_inflow": 1234.56,
  "exchange_outflow": 2345.67,
  "whale_transactions": 23,
  "network_growth": 0.023
}';
