-- 016_adaptive_strategy_seed_data.sql
-- Seed Data for Adaptive Strategy Configuration
--
-- Purpose: Replace hardcoded Default::default() implementations with database-backed
--          configuration that can be hot-reloaded and audited.
--
-- Strategies:
--   1. default-production: Conservative production-safe configuration
--   2. development: Permissive configuration for testing and development
--   3. aggressive: High-frequency configuration for low-latency scenarios
--
-- Migration: Phase 3 of Config Migration (Wave 64 Agent 2)

-- ============================================================================
-- STRATEGY 1: PRODUCTION (Conservative, Production-Safe)
-- ============================================================================

INSERT INTO adaptive_strategy_config (
    strategy_id,
    name,
    description,

    -- General Configuration
    execution_interval_ms,          -- 100ms for production stability
    error_backoff_duration_secs,    -- 1 second backoff on errors
    max_concurrent_operations,      -- Conservative concurrency
    strategy_timeout_secs,          -- 30 second timeout

    -- Ensemble Configuration
    max_parallel_models,            -- 4 models for stability
    rebalancing_interval_secs,      -- 5 minutes between rebalancing
    min_model_weight,               -- 1% minimum weight
    max_model_weight,               -- 50% maximum weight (diversification)

    -- Risk Configuration
    max_position_size,              -- 5% maximum position (conservative)
    max_leverage,                   -- 1.5x leverage (low risk)
    stop_loss_pct,                  -- 2% stop loss
    position_sizing_method,         -- Kelly Criterion
    max_portfolio_var,              -- 2% max VaR
    max_drawdown_threshold,         -- 5% max drawdown
    kelly_fraction,                 -- 25% Kelly fraction (fractional Kelly)

    -- Microstructure Configuration
    book_depth,                     -- 10 levels of order book
    vpin_window,                    -- 50 trades for VPIN
    trade_classification_threshold, -- 50% threshold
    trade_size_buckets,             -- Standard size buckets
    microstructure_features,        -- Core microstructure features

    -- Regime Configuration
    regime_detection_method,        -- HMM for regime detection
    regime_lookback_window,         -- 252 trading days (1 year)
    regime_transition_threshold,    -- 70% threshold
    regime_features,                -- Volatility, momentum, volume

    -- Execution Configuration
    execution_algorithm,            -- TWAP for stability
    max_order_size,                 -- $10,000 max order
    min_order_size,                 -- $100 min order
    order_timeout_secs,             -- 30 second timeout
    max_slippage_bps,               -- 10 bps max slippage
    smart_routing_enabled,          -- Enable smart routing
    dark_pool_preference,           -- 30% dark pool preference

    -- Audit Fields
    active,
    metadata
) VALUES (
    'default-production',
    'Production Default Strategy',
    'Conservative production configuration with risk controls and stable execution parameters. Designed for live trading with emphasis on risk management and regulatory compliance.',

    -- General: Conservative timing
    100,              -- execution_interval_ms
    1,                -- error_backoff_duration_secs
    10,               -- max_concurrent_operations
    30,               -- strategy_timeout_secs

    -- Ensemble: Balanced model coordination
    4,                -- max_parallel_models
    300,              -- rebalancing_interval_secs (5 minutes)
    0.01,             -- min_model_weight (1%)
    0.50,             -- max_model_weight (50%)

    -- Risk: Conservative position sizing
    0.05,             -- max_position_size (5% of portfolio)
    1.5,              -- max_leverage (low leverage)
    0.02,             -- stop_loss_pct (2%)
    'KELLY',          -- position_sizing_method
    0.02,             -- max_portfolio_var (2%)
    0.05,             -- max_drawdown_threshold (5%)
    0.25,             -- kelly_fraction (25% fractional Kelly)

    -- Microstructure: Standard analysis
    10,               -- book_depth
    50,               -- vpin_window
    0.5,              -- trade_classification_threshold
    ARRAY[10.0, 100.0, 1000.0, 10000.0], -- trade_size_buckets
    ARRAY['vpin', 'order_flow', 'bid_ask_spread'], -- microstructure_features

    -- Regime: HMM-based detection
    'HMM',            -- regime_detection_method
    252,              -- regime_lookback_window (1 year)
    0.7,              -- regime_transition_threshold
    ARRAY['volatility', 'momentum', 'volume'], -- regime_features

    -- Execution: TWAP for stability
    'TWAP',           -- execution_algorithm
    10000.0,          -- max_order_size
    100.0,            -- min_order_size
    30,               -- order_timeout_secs
    10.0,             -- max_slippage_bps
    true,             -- smart_routing_enabled
    0.3,              -- dark_pool_preference

    -- Audit
    true,             -- active
    jsonb_build_object(
        'environment', 'production',
        'risk_profile', 'conservative',
        'created_by_migration', '016_adaptive_strategy_seed_data.sql',
        'notes', 'Default production configuration - modify via TLI or direct database updates'
    )
);

-- ============================================================================
-- STRATEGY 2: DEVELOPMENT (Permissive for Testing)
-- ============================================================================

INSERT INTO adaptive_strategy_config (
    strategy_id,
    name,
    description,

    execution_interval_ms,
    error_backoff_duration_secs,
    max_concurrent_operations,
    strategy_timeout_secs,

    max_parallel_models,
    rebalancing_interval_secs,
    min_model_weight,
    max_model_weight,

    max_position_size,
    max_leverage,
    stop_loss_pct,
    position_sizing_method,
    max_portfolio_var,
    max_drawdown_threshold,
    kelly_fraction,

    book_depth,
    vpin_window,
    trade_classification_threshold,
    trade_size_buckets,
    microstructure_features,

    regime_detection_method,
    regime_lookback_window,
    regime_transition_threshold,
    regime_features,

    execution_algorithm,
    max_order_size,
    min_order_size,
    order_timeout_secs,
    max_slippage_bps,
    smart_routing_enabled,
    dark_pool_preference,

    active,
    metadata
) VALUES (
    'development',
    'Development Strategy',
    'Permissive development configuration for testing and experimentation. Higher risk limits, faster execution, more aggressive parameters.',

    -- General: Faster execution for testing
    50,               -- execution_interval_ms (50ms - faster)
    2,                -- error_backoff_duration_secs
    20,               -- max_concurrent_operations (higher concurrency)
    60,               -- strategy_timeout_secs (longer timeout)

    -- Ensemble: More models for testing
    8,                -- max_parallel_models (more models)
    60,               -- rebalancing_interval_secs (1 minute - faster)
    0.001,            -- min_model_weight (0.1% - allow smaller weights)
    0.70,             -- max_model_weight (70% - less diversification)

    -- Risk: Higher limits for development
    0.20,             -- max_position_size (20% - higher for testing)
    3.0,              -- max_leverage (higher leverage)
    0.05,             -- stop_loss_pct (5% - wider stops)
    'KELLY',          -- position_sizing_method
    0.05,             -- max_portfolio_var (5%)
    0.15,             -- max_drawdown_threshold (15%)
    0.50,             -- kelly_fraction (50% - more aggressive)

    -- Microstructure: Deeper analysis
    20,               -- book_depth (deeper for testing)
    100,              -- vpin_window (larger window)
    0.5,              -- trade_classification_threshold
    ARRAY[5.0, 50.0, 500.0, 5000.0, 50000.0], -- trade_size_buckets (more buckets)
    ARRAY['vpin', 'order_flow', 'bid_ask_spread', 'depth_imbalance'], -- microstructure_features

    -- Regime: Threshold-based for simplicity
    'THRESHOLD',      -- regime_detection_method (simpler for testing)
    100,              -- regime_lookback_window (shorter window)
    0.6,              -- regime_transition_threshold (easier transitions)
    ARRAY['volatility', 'momentum', 'volume', 'trend'], -- regime_features

    -- Execution: VWAP for development
    'VWAP',           -- execution_algorithm
    50000.0,          -- max_order_size (larger orders)
    10.0,             -- min_order_size (smaller minimum)
    60,               -- order_timeout_secs
    25.0,             -- max_slippage_bps (more permissive)
    true,             -- smart_routing_enabled
    0.5,              -- dark_pool_preference (higher for testing)

    -- Audit
    true,             -- active
    jsonb_build_object(
        'environment', 'development',
        'risk_profile', 'permissive',
        'created_by_migration', '016_adaptive_strategy_seed_data.sql',
        'notes', 'Development configuration - NOT for production use'
    )
);

-- ============================================================================
-- STRATEGY 3: AGGRESSIVE (High-Frequency, Low-Latency)
-- ============================================================================

INSERT INTO adaptive_strategy_config (
    strategy_id,
    name,
    description,

    execution_interval_ms,
    error_backoff_duration_secs,
    max_concurrent_operations,
    strategy_timeout_secs,

    max_parallel_models,
    rebalancing_interval_secs,
    min_model_weight,
    max_model_weight,

    max_position_size,
    max_leverage,
    stop_loss_pct,
    position_sizing_method,
    max_portfolio_var,
    max_drawdown_threshold,
    kelly_fraction,

    book_depth,
    vpin_window,
    trade_classification_threshold,
    trade_size_buckets,
    microstructure_features,

    regime_detection_method,
    regime_lookback_window,
    regime_transition_threshold,
    regime_features,

    execution_algorithm,
    max_order_size,
    min_order_size,
    order_timeout_secs,
    max_slippage_bps,
    smart_routing_enabled,
    dark_pool_preference,

    active,
    metadata
) VALUES (
    'aggressive',
    'Aggressive High-Frequency Strategy',
    'High-frequency configuration optimized for low-latency execution. Faster execution cycles, tighter risk controls, minimal slippage tolerance. For experienced traders only.',

    -- General: Ultra-fast execution
    10,               -- execution_interval_ms (10ms - HFT speed)
    0,                -- error_backoff_duration_secs (no backoff)
    50,               -- max_concurrent_operations (high concurrency)
    10,               -- strategy_timeout_secs (quick timeout)

    -- Ensemble: Fewer models for speed
    2,                -- max_parallel_models (fewer for latency)
    30,               -- rebalancing_interval_secs (30 seconds - frequent)
    0.10,             -- min_model_weight (10% - concentrated)
    0.90,             -- max_model_weight (90% - highly concentrated)

    -- Risk: Aggressive sizing with tight stops
    0.15,             -- max_position_size (15%)
    2.5,              -- max_leverage
    0.01,             -- stop_loss_pct (1% - tight stop)
    'PPO',            -- position_sizing_method (RL-based)
    0.03,             -- max_portfolio_var (3%)
    0.08,             -- max_drawdown_threshold (8%)
    0.40,             -- kelly_fraction (40%)

    -- Microstructure: Shallow but fast
    5,                -- book_depth (shallow for speed)
    20,               -- vpin_window (smaller window)
    0.6,              -- trade_classification_threshold
    ARRAY[100.0, 1000.0, 10000.0], -- trade_size_buckets (fewer buckets)
    ARRAY['vpin', 'bid_ask_spread'], -- microstructure_features (minimal)

    -- Regime: ML-based for accuracy
    'ML_CLASSIFIER',  -- regime_detection_method
    50,               -- regime_lookback_window (short window)
    0.8,              -- regime_transition_threshold (high confidence)
    ARRAY['volatility', 'momentum'], -- regime_features (minimal for speed)

    -- Execution: Implementation Shortfall for HFT
    'IS',             -- execution_algorithm (Implementation Shortfall)
    5000.0,           -- max_order_size (smaller orders)
    50.0,             -- min_order_size
    5,                -- order_timeout_secs (very short)
    5.0,              -- max_slippage_bps (tight slippage control)
    true,             -- smart_routing_enabled
    0.7,              -- dark_pool_preference (high for HFT)

    -- Audit
    false,            -- active (disabled by default - requires explicit activation)
    jsonb_build_object(
        'environment', 'production',
        'risk_profile', 'aggressive',
        'created_by_migration', '016_adaptive_strategy_seed_data.sql',
        'notes', 'AGGRESSIVE HFT configuration - requires explicit activation and experienced trader approval',
        'warning', 'High-frequency trading with elevated risk - monitor closely'
    )
);

-- ============================================================================
-- SEED MODELS FOR EACH STRATEGY
-- ============================================================================

-- Production Models (Conservative Ensemble)
INSERT INTO adaptive_strategy_models (
    strategy_config_id,
    model_id,
    model_name,
    model_type,
    parameters,
    initial_weight,
    enabled,
    display_order
)
SELECT
    id,
    'mamba2_prod',
    'MAMBA-2 Production Model',
    'mamba2',
    jsonb_build_object(
        'hidden_dim', 256,
        'state_size', 16,
        'num_layers', 4,
        'dropout', 0.1
    ),
    0.35,
    true,
    1
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'tlob_prod',
    'TLOB Production Model',
    'tlob',
    jsonb_build_object(
        'num_heads', 8,
        'num_layers', 6,
        'dropout', 0.1,
        'book_depth', 10
    ),
    0.35,
    true,
    2
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'lstm_prod',
    'LSTM Production Model',
    'lstm',
    jsonb_build_object(
        'hidden_size', 128,
        'num_layers', 3,
        'dropout', 0.2
    ),
    0.30,
    true,
    3
FROM adaptive_strategy_config WHERE strategy_id = 'default-production';

-- Development Models (Full Ensemble)
INSERT INTO adaptive_strategy_models (
    strategy_config_id,
    model_id,
    model_name,
    model_type,
    parameters,
    initial_weight,
    enabled,
    display_order
)
SELECT
    id,
    'mamba2_dev',
    'MAMBA-2 Development',
    'mamba2',
    jsonb_build_object(
        'hidden_dim', 512,
        'state_size', 32,
        'num_layers', 6
    ),
    0.20,
    true,
    1
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'tlob_dev',
    'TLOB Development',
    'tlob',
    jsonb_build_object(
        'num_heads', 12,
        'num_layers', 8,
        'dropout', 0.05
    ),
    0.20,
    true,
    2
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'dqn_dev',
    'DQN Development',
    'dqn',
    jsonb_build_object(
        'learning_rate', 0.001,
        'gamma', 0.99,
        'epsilon', 0.1,
        'buffer_size', 100000
    ),
    0.20,
    true,
    3
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'ppo_dev',
    'PPO Development',
    'ppo',
    jsonb_build_object(
        'learning_rate', 0.0003,
        'gamma', 0.99,
        'clip_epsilon', 0.2,
        'epochs', 10
    ),
    0.20,
    true,
    4
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'liquid_dev',
    'Liquid Networks Development',
    'liquid',
    jsonb_build_object(
        'hidden_size', 128,
        'num_layers', 4,
        'tau_min', 0.1,
        'tau_max', 10.0
    ),
    0.20,
    true,
    5
FROM adaptive_strategy_config WHERE strategy_id = 'development';

-- Aggressive Models (Minimal Ensemble for Speed)
INSERT INTO adaptive_strategy_models (
    strategy_config_id,
    model_id,
    model_name,
    model_type,
    parameters,
    initial_weight,
    enabled,
    display_order
)
SELECT
    id,
    'tlob_hft',
    'TLOB HFT Optimized',
    'tlob',
    jsonb_build_object(
        'num_heads', 4,
        'num_layers', 3,
        'dropout', 0.0,
        'book_depth', 5,
        'inference_batch_size', 1
    ),
    0.60,
    true,
    1
FROM adaptive_strategy_config WHERE strategy_id = 'aggressive'
UNION ALL
SELECT
    id,
    'ppo_hft',
    'PPO HFT Optimized',
    'ppo',
    jsonb_build_object(
        'learning_rate', 0.0001,
        'gamma', 0.95,
        'clip_epsilon', 0.1,
        'update_frequency', 100
    ),
    0.40,
    true,
    2
FROM adaptive_strategy_config WHERE strategy_id = 'aggressive';

-- ============================================================================
-- SEED FEATURES FOR EACH STRATEGY
-- ============================================================================

-- Production Features (Core Features)
INSERT INTO adaptive_strategy_features (
    strategy_config_id,
    feature_name,
    feature_type,
    parameters,
    enabled,
    required
)
SELECT
    id,
    'vpin',
    'orderbook',
    jsonb_build_object('window', 50),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'order_flow',
    'orderbook',
    jsonb_build_object('depth', 10),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'bid_ask_spread',
    'orderbook',
    jsonb_build_object(),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'volatility',
    'technical',
    jsonb_build_object('window', 20, 'method', 'parkinson'),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'default-production'
UNION ALL
SELECT
    id,
    'momentum',
    'technical',
    jsonb_build_object('window', 14),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'default-production';

-- Development Features (Extended Features)
INSERT INTO adaptive_strategy_features (
    strategy_config_id,
    feature_name,
    feature_type,
    parameters,
    enabled,
    required
)
SELECT
    id,
    'vpin',
    'orderbook',
    jsonb_build_object('window', 100),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'order_flow',
    'orderbook',
    jsonb_build_object('depth', 20),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'depth_imbalance',
    'orderbook',
    jsonb_build_object('levels', 10),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'volatility',
    'technical',
    jsonb_build_object('window', 50, 'method', 'garman_klass'),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'rsi',
    'technical',
    jsonb_build_object('window', 14),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'development'
UNION ALL
SELECT
    id,
    'macd',
    'technical',
    jsonb_build_object('fast', 12, 'slow', 26, 'signal', 9),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'development';

-- Aggressive Features (Minimal Features for Speed)
INSERT INTO adaptive_strategy_features (
    strategy_config_id,
    feature_name,
    feature_type,
    parameters,
    enabled,
    required
)
SELECT
    id,
    'vpin',
    'orderbook',
    jsonb_build_object('window', 20),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'aggressive'
UNION ALL
SELECT
    id,
    'bid_ask_spread',
    'orderbook',
    jsonb_build_object(),
    true,
    true
FROM adaptive_strategy_config WHERE strategy_id = 'aggressive'
UNION ALL
SELECT
    id,
    'volatility',
    'technical',
    jsonb_build_object('window', 10, 'method', 'simple'),
    true,
    false
FROM adaptive_strategy_config WHERE strategy_id = 'aggressive';

-- ============================================================================
-- VALIDATION AND COMMENTS
-- ============================================================================

-- Verify model weights sum to 1.0 (with tolerance)
DO $$
DECLARE
    strat RECORD;
    weight_sum DOUBLE PRECISION;
BEGIN
    FOR strat IN SELECT id, strategy_id FROM adaptive_strategy_config LOOP
        SELECT SUM(initial_weight) INTO weight_sum
        FROM adaptive_strategy_models
        WHERE strategy_config_id = strat.id AND enabled = true;

        IF ABS(weight_sum - 1.0) > 0.01 THEN
            RAISE WARNING 'Strategy % has model weights summing to % (expected 1.0)',
                strat.strategy_id, weight_sum;
        END IF;
    END LOOP;
END $$;

-- Add comments
COMMENT ON TABLE adaptive_strategy_config IS 'Adaptive strategy configurations - replaces hardcoded defaults. Updated by migration 016_adaptive_strategy_seed_data.sql';

-- Migration complete message
DO $$
BEGIN
    RAISE NOTICE '=================================================================';
    RAISE NOTICE 'Migration 016: Adaptive Strategy Seed Data - COMPLETE';
    RAISE NOTICE '=================================================================';
    RAISE NOTICE 'Configurations created:';
    RAISE NOTICE '  1. default-production (ACTIVE) - Conservative production config';
    RAISE NOTICE '  2. development (ACTIVE) - Permissive testing config';
    RAISE NOTICE '  3. aggressive (INACTIVE) - HFT config (requires activation)';
    RAISE NOTICE '';
    RAISE NOTICE 'Next steps:';
    RAISE NOTICE '  - Remove Default implementations from adaptive-strategy/src/config.rs';
    RAISE NOTICE '  - Update services to use DatabaseConfigLoader';
    RAISE NOTICE '  - Run integration tests';
    RAISE NOTICE '=================================================================';
END $$;
