-- ============================================================================
-- Schema: market_data
-- Table: tickers
-- Description: Catálogo de activos/tickers para datos de mercado
-- Dependencies: market_data.asset_type
-- ============================================================================

CREATE TABLE market_data.tickers (
    id SERIAL PRIMARY KEY,

    -- Identificación
    symbol VARCHAR(20) NOT NULL UNIQUE,        -- XAUUSD, EURUSD, BTCUSD
    name VARCHAR(100) NOT NULL,                -- Gold/US Dollar, Euro/US Dollar

    -- Clasificación
    asset_type market_data.asset_type NOT NULL,
    base_currency VARCHAR(10) NOT NULL,        -- XAU, EUR, BTC
    quote_currency VARCHAR(10) NOT NULL,       -- USD

    -- Configuración ML
    is_ml_enabled BOOLEAN DEFAULT true,        -- Habilitado para ML signals
    supported_timeframes VARCHAR(50)[] DEFAULT ARRAY['5m', '15m'],

    -- Polygon.io mapping
    polygon_ticker VARCHAR(20),                -- C:XAUUSD, X:BTCUSD

    -- Estado
    is_active BOOLEAN DEFAULT true,

    -- Timestamps
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Índices
CREATE INDEX idx_tickers_symbol ON market_data.tickers(symbol);
CREATE INDEX idx_tickers_asset_type ON market_data.tickers(asset_type);
CREATE INDEX idx_tickers_ml_enabled ON market_data.tickers(is_ml_enabled) WHERE is_ml_enabled = true;
CREATE INDEX idx_tickers_polygon ON market_data.tickers(polygon_ticker);

-- Comentarios
COMMENT ON TABLE market_data.tickers IS 'Catálogo de activos financieros para datos OHLCV';
COMMENT ON COLUMN market_data.tickers.symbol IS 'Símbolo del activo (XAUUSD, BTCUSD, etc.)';
COMMENT ON COLUMN market_data.tickers.polygon_ticker IS 'Símbolo en formato Polygon.io (C:XAUUSD, X:BTCUSD)';
COMMENT ON COLUMN market_data.tickers.is_ml_enabled IS 'Indica si el activo está habilitado para ML signals';

-- Seed: 7 activos principales (6 ML + AUDUSD)
INSERT INTO market_data.tickers (symbol, name, asset_type, base_currency, quote_currency, polygon_ticker) VALUES
    ('XAUUSD', 'Gold/US Dollar', 'commodity', 'XAU', 'USD', 'C:XAUUSD'),
    ('EURUSD', 'Euro/US Dollar', 'forex', 'EUR', 'USD', 'C:EURUSD'),
    ('BTCUSD', 'Bitcoin/US Dollar', 'crypto', 'BTC', 'USD', 'X:BTCUSD'),
    ('GBPUSD', 'British Pound/US Dollar', 'forex', 'GBP', 'USD', 'C:GBPUSD'),
    ('USDJPY', 'US Dollar/Japanese Yen', 'forex', 'USD', 'JPY', 'C:USDJPY'),
    ('AUDUSD', 'Australian Dollar/US Dollar', 'forex', 'AUD', 'USD', 'C:AUDUSD'),
    ('GBPJPY', 'British Pound/Japanese Yen', 'forex', 'GBP', 'JPY', 'C:GBPJPY');
