-- ============================================================================
-- SCHEMA: broker_integration
-- TABLE: broker_prices
-- DESCRIPTION: Precios en tiempo real de brokers
-- VERSION: 1.0.0
-- CREATED: 2026-01-16
-- SPRINT: Sprint 6 - DDL Implementation Roadmap Q1-2026
-- ============================================================================

-- Tabla de Precios de Broker
CREATE TABLE IF NOT EXISTS broker_integration.broker_prices (
    -- Identificadores
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL,
    broker_account_id UUID NOT NULL REFERENCES broker_integration.broker_accounts(id) ON DELETE CASCADE,

    -- Simbolo
    symbol VARCHAR(20) NOT NULL,

    -- Precios
    bid DECIMAL(15, 8) NOT NULL,
    ask DECIMAL(15, 8) NOT NULL,
    spread DECIMAL(10, 4),                              -- En pips
    spread_points INTEGER,                              -- En puntos

    -- Volumen
    bid_volume DECIMAL(15, 4),
    ask_volume DECIMAL(15, 4),

    -- Cambio
    change_24h DECIMAL(15, 8),
    change_percent_24h DECIMAL(10, 4),
    high_24h DECIMAL(15, 8),
    low_24h DECIMAL(15, 8),

    -- Sesion
    session_open DECIMAL(15, 8),
    session_high DECIMAL(15, 8),
    session_low DECIMAL(15, 8),
    previous_close DECIMAL(15, 8),

    -- Trading
    is_trading_enabled BOOLEAN NOT NULL DEFAULT TRUE,
    trading_session VARCHAR(20),                        -- 'pre_market', 'regular', 'after_hours'

    -- Timestamp del broker
    broker_timestamp TIMESTAMPTZ,

    -- Timestamps
    received_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

    -- Constraints
    CONSTRAINT broker_prices_unique UNIQUE (broker_account_id, symbol)
);

COMMENT ON TABLE broker_integration.broker_prices IS
'Precios en tiempo real recibidos de brokers conectados';

-- Indices
CREATE INDEX IF NOT EXISTS idx_broker_prices_account
    ON broker_integration.broker_prices(broker_account_id);

CREATE INDEX IF NOT EXISTS idx_broker_prices_symbol
    ON broker_integration.broker_prices(symbol);

CREATE INDEX IF NOT EXISTS idx_broker_prices_received
    ON broker_integration.broker_prices(received_at DESC);

-- Vista de spreads actuales
CREATE OR REPLACE VIEW broker_integration.v_current_spreads AS
SELECT
    bp.symbol,
    ba.broker_name,
    bp.bid,
    bp.ask,
    bp.spread,
    bp.spread_points,
    bp.is_trading_enabled,
    bp.received_at
FROM broker_integration.broker_prices bp
JOIN broker_integration.broker_accounts ba ON bp.broker_account_id = ba.id
WHERE ba.is_active = TRUE
ORDER BY bp.symbol, bp.spread;

-- RLS
ALTER TABLE broker_integration.broker_prices ENABLE ROW LEVEL SECURITY;

CREATE POLICY broker_prices_tenant ON broker_integration.broker_prices
    FOR ALL
    USING (tenant_id = current_setting('app.current_tenant_id', true)::UUID);

-- Grants
GRANT SELECT, INSERT, UPDATE, DELETE ON broker_integration.broker_prices TO trading_app;
GRANT SELECT ON broker_integration.broker_prices TO trading_readonly;
GRANT SELECT ON broker_integration.v_current_spreads TO trading_app;
