-- Initial Configuration Data for Foxhunt HFT Trading System
-- =========================================================
-- This migration populates the configuration system with default values
-- for all services and environments.

-- === CONFIGURATION ENVIRONMENTS ===

INSERT INTO config_environments (environment_name, display_name, description, is_active, is_production, inherits_from, isolation_level, auto_sync) VALUES
('development', 'Development', 'Local development environment with relaxed security and verbose logging', true, false, NULL, 'permissive', false),
('test', 'Testing', 'Automated testing environment with isolated data and mock services', true, false, 'development', 'strict', false),
('staging', 'Staging', 'Pre-production environment mirroring production configuration', true, false, 'development', 'strict', true),
('production', 'Production', 'Live trading environment with strict security and performance optimization', true, true, NULL, 'strict', false);

-- === CONFIGURATION CATEGORIES ===

-- Root categories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order) VALUES
('system', NULL, 'system', 'Core system configuration', true, 1),
('database', NULL, 'database', 'Database connection and performance settings', true, 2),
('security', NULL, 'security', 'Authentication, authorization, and encryption settings', true, 3),
('trading', NULL, 'trading', 'Trading engine and order management configuration', false, 4),
('risk', NULL, 'risk', 'Risk management and compliance settings', false, 5),
('ml', NULL, 'ml', 'Machine learning model and inference configuration', false, 6),
('monitoring', NULL, 'monitoring', 'Logging, metrics, and alerting configuration', true, 7),
('performance', NULL, 'performance', 'Performance tuning and optimization settings', true, 8);

-- System subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order) 
SELECT 'logging', id, 'system.logging', 'Logging configuration and levels', true, 1
FROM config_categories WHERE category_path = 'system';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'service_discovery', id, 'system.service_discovery', 'Service discovery and health check settings', true, 2
FROM config_categories WHERE category_path = 'system';

-- Database subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'postgresql', id, 'database.postgresql', 'PostgreSQL connection and pool settings', true, 1
FROM config_categories WHERE category_path = 'database';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'redis', id, 'database.redis', 'Redis cache and session storage settings', true, 2
FROM config_categories WHERE category_path = 'database';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'influxdb', id, 'database.influxdb', 'InfluxDB time-series database settings', true, 3
FROM config_categories WHERE category_path = 'database';

-- Security subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'jwt', id, 'security.jwt', 'JWT token configuration and validation', true, 1
FROM config_categories WHERE category_path = 'security';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'tls', id, 'security.tls', 'TLS/SSL certificate and encryption settings', true, 2
FROM config_categories WHERE category_path = 'security';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'rate_limiting', id, 'security.rate_limiting', 'API rate limiting and DDoS protection', true, 3
FROM config_categories WHERE category_path = 'security';

-- Trading subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'order_management', id, 'trading.order_management', 'Order processing and execution settings', false, 1
FROM config_categories WHERE category_path = 'trading';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'market_data', id, 'trading.market_data', 'Market data feed and processing configuration', false, 2
FROM config_categories WHERE category_path = 'trading';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'brokers', id, 'trading.brokers', 'Broker connection and integration settings', false, 3
FROM config_categories WHERE category_path = 'trading';

-- Risk subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'limits', id, 'risk.limits', 'Risk limits and thresholds', false, 1
FROM config_categories WHERE category_path = 'risk';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'var_calculation', id, 'risk.var_calculation', 'Value at Risk calculation parameters', false, 2
FROM config_categories WHERE category_path = 'risk';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'circuit_breakers', id, 'risk.circuit_breakers', 'Automatic trading halt and circuit breaker settings', false, 3
FROM config_categories WHERE category_path = 'risk';

-- ML subcategories
INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'models', id, 'ml.models', 'Machine learning model configuration and paths', false, 1
FROM config_categories WHERE category_path = 'ml';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'inference', id, 'ml.inference', 'Model inference and prediction settings', false, 2
FROM config_categories WHERE category_path = 'ml';

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order)
SELECT 'training', id, 'ml.training', 'Model training and optimization parameters', false, 3
FROM config_categories WHERE category_path = 'ml';

-- === CORE SYSTEM CONFIGURATION ===

-- System Logging Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'log_level', id, 'system.logging', '"info"'::jsonb, 'string', 'development', 'Default logging level for all services', ARRAY['logging', 'debugging'], true, false
FROM config_categories WHERE category_path = 'system.logging';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'log_level', id, 'system.logging', '"warn"'::jsonb, 'string', 'production', 'Production logging level - warnings and errors only', ARRAY['logging', 'production'], true, false
FROM config_categories WHERE category_path = 'system.logging';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'log_format', id, 'system.logging', '"json"'::jsonb, 'string', 'production', 'Structured JSON logging for production', ARRAY['logging', 'format'], true, false
FROM config_categories WHERE category_path = 'system.logging';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'log_format', id, 'system.logging', '"pretty"'::jsonb, 'string', 'development', 'Human-readable logging for development', ARRAY['logging', 'format'], true, false
FROM config_categories WHERE category_path = 'system.logging';

-- Service Discovery Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'health_check_interval_ms', id, 'system.service_discovery', '30000'::jsonb, 'number', 'development', 'Health check interval in milliseconds', ARRAY['health', 'monitoring'], true, false
FROM config_categories WHERE category_path = 'system.service_discovery';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'health_check_timeout_ms', id, 'system.service_discovery', '5000'::jsonb, 'number', 'development', 'Health check timeout in milliseconds', ARRAY['health', 'monitoring'], true, false
FROM config_categories WHERE category_path = 'system.service_discovery';

-- === DATABASE CONFIGURATION ===

-- PostgreSQL Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_connections', id, 'database.postgresql', '10'::jsonb, 'number', 'development', 'Maximum database connections per service', ARRAY['database', 'performance'], false, true
FROM config_categories WHERE category_path = 'database.postgresql';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_connections', id, 'database.postgresql', '50'::jsonb, 'number', 'production', 'Production database connection pool size', ARRAY['database', 'performance'], false, true
FROM config_categories WHERE category_path = 'database.postgresql';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'connection_timeout_ms', id, 'database.postgresql', '30000'::jsonb, 'number', 'development', 'Database connection timeout in milliseconds', ARRAY['database', 'timeout'], false, true
FROM config_categories WHERE category_path = 'database.postgresql';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'statement_timeout_ms', id, 'database.postgresql', '60000'::jsonb, 'number', 'development', 'SQL statement execution timeout', ARRAY['database', 'timeout'], true, false
FROM config_categories WHERE category_path = 'database.postgresql';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'statement_timeout_ms', id, 'database.postgresql', '30000'::jsonb, 'number', 'production', 'Production SQL statement timeout - shorter for performance', ARRAY['database', 'timeout'], true, false
FROM config_categories WHERE category_path = 'database.postgresql';

-- Redis Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'default_ttl_seconds', id, 'database.redis', '3600'::jsonb, 'number', 'development', 'Default TTL for Redis cache entries', ARRAY['cache', 'ttl'], true, false
FROM config_categories WHERE category_path = 'database.redis';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_connections', id, 'database.redis', '20'::jsonb, 'number', 'development', 'Maximum Redis connections per service', ARRAY['cache', 'performance'], false, true
FROM config_categories WHERE category_path = 'database.redis';

-- === SECURITY CONFIGURATION ===

-- JWT Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, is_sensitive, hot_reload, restart_required)
SELECT 'token_expiry_minutes', id, 'security.jwt', '60'::jsonb, 'number', 'development', 'JWT token expiration time in minutes', ARRAY['security', 'jwt'], false, true, false
FROM config_categories WHERE category_path = 'security.jwt';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, is_sensitive, hot_reload, restart_required)
SELECT 'token_expiry_minutes', id, 'security.jwt', '15'::jsonb, 'number', 'production', 'Shorter JWT expiration for production security', ARRAY['security', 'jwt'], false, true, false
FROM config_categories WHERE category_path = 'security.jwt';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, is_sensitive, hot_reload, restart_required)
SELECT 'refresh_token_expiry_days', id, 'security.jwt', '7'::jsonb, 'number', 'development', 'Refresh token expiration time in days', ARRAY['security', 'jwt'], false, true, false
FROM config_categories WHERE category_path = 'security.jwt';

-- TLS Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'min_tls_version', id, 'security.tls', '"1.2"'::jsonb, 'string', 'production', 'Minimum TLS version required', ARRAY['security', 'tls'], false, true
FROM config_categories WHERE category_path = 'security.tls';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'require_client_certs', id, 'security.tls', 'true'::jsonb, 'boolean', 'production', 'Require mutual TLS authentication', ARRAY['security', 'tls', 'mtls'], false, true
FROM config_categories WHERE category_path = 'security.tls';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'require_client_certs', id, 'security.tls', 'false'::jsonb, 'boolean', 'development', 'Relaxed TLS for development', ARRAY['security', 'tls'], false, true
FROM config_categories WHERE category_path = 'security.tls';

-- Rate Limiting Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT '[REDACTED], id, 'security.rate_limiting', '1000'::jsonb, 'number', 'development', 'API rate limit per minute per client', ARRAY['security', 'rate_limiting'], true, false
FROM config_categories WHERE category_path = 'security.rate_limiting';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT '[REDACTED], id, 'security.rate_limiting', '500'::jsonb, 'number', 'production', 'Production API rate limit', ARRAY['security', 'rate_limiting'], true, false
FROM config_categories WHERE category_path = 'security.rate_limiting';

-- === TRADING CONFIGURATION ===

-- Order Management Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_order_size_usd', id, 'trading.order_management', '100000'::jsonb, 'number', 'development', 'Maximum single order size in USD', ARRAY['trading', 'limits'], true, false
FROM config_categories WHERE category_path = 'trading.order_management';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_order_size_usd', id, 'trading.order_management', '1000000'::jsonb, 'number', 'production', 'Production maximum order size', ARRAY['trading', 'limits'], true, false
FROM config_categories WHERE category_path = 'trading.order_management';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'order_timeout_seconds', id, 'trading.order_management', '30'::jsonb, 'number', 'development', 'Order execution timeout in seconds', ARRAY['trading', 'timeout'], true, false
FROM config_categories WHERE category_path = 'trading.order_management';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_paper_trading', id, 'trading.order_management', 'true'::jsonb, 'boolean', 'development', 'Enable paper trading mode for testing', ARRAY['trading', 'simulation'], true, false
FROM config_categories WHERE category_path = 'trading.order_management';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_paper_trading', id, 'trading.order_management', 'false'::jsonb, 'boolean', 'production', 'Disable paper trading in production', ARRAY['trading', 'simulation'], true, false
FROM config_categories WHERE category_path = 'trading.order_management';

-- Market Data Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'market_data_feed_timeout_ms', id, 'trading.market_data', '5000'::jsonb, 'number', 'development', 'Market data feed timeout in milliseconds', ARRAY['trading', 'market_data'], true, false
FROM config_categories WHERE category_path = 'trading.market_data';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'market_data_buffer_size', id, 'trading.market_data', '10000'::jsonb, 'number', 'development', 'Market data buffer size for processing', ARRAY['trading', 'market_data', 'performance'], false, true
FROM config_categories WHERE category_path = 'trading.market_data';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'market_data_buffer_size', id, 'trading.market_data', '50000'::jsonb, 'number', 'production', 'Larger buffer for production throughput', ARRAY['trading', 'market_data', 'performance'], false, true
FROM config_categories WHERE category_path = 'trading.market_data';

-- Broker Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'default_broker', id, 'trading.brokers', '"simulation"'::jsonb, 'string', 'development', 'Default broker for development', ARRAY['trading', 'brokers'], true, false
FROM config_categories WHERE category_path = 'trading.brokers';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'connection_retry_attempts', id, 'trading.brokers', '3'::jsonb, 'number', 'development', 'Broker connection retry attempts', ARRAY['trading', 'brokers', 'reliability'], true, false
FROM config_categories WHERE category_path = 'trading.brokers';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'connection_retry_delay_ms', id, 'trading.brokers', '1000'::jsonb, 'number', 'development', 'Delay between connection retry attempts', ARRAY['trading', 'brokers', 'reliability'], true, false
FROM config_categories WHERE category_path = 'trading.brokers';

-- === RISK MANAGEMENT CONFIGURATION ===

-- Risk Limits Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_daily_loss_usd', id, 'risk.limits', '10000'::jsonb, 'number', 'development', 'Maximum daily loss limit in USD', ARRAY['risk', 'limits'], true, false
FROM config_categories WHERE category_path = 'risk.limits';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_daily_loss_usd', id, 'risk.limits', '50000'::jsonb, 'number', 'production', 'Production daily loss limit', ARRAY['risk', 'limits'], true, false
FROM config_categories WHERE category_path = 'risk.limits';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_position_concentration_pct', id, 'risk.limits', '20'::jsonb, 'number', 'development', 'Maximum position concentration as percentage of portfolio', ARRAY['risk', 'limits', 'concentration'], true, false
FROM config_categories WHERE category_path = 'risk.limits';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_leverage_ratio', id, 'risk.limits', '2.0'::jsonb, 'number', 'development', 'Maximum leverage ratio allowed', ARRAY['risk', 'limits', 'leverage'], true, false
FROM config_categories WHERE category_path = 'risk.limits';

-- VaR Calculation Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'var_confidence_level', id, 'risk.var_calculation', '0.95'::jsonb, 'number', 'development', 'VaR confidence level (95%)', ARRAY['risk', 'var'], true, false
FROM config_categories WHERE category_path = 'risk.var_calculation';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'var_time_horizon_days', id, 'risk.var_calculation', '1'::jsonb, 'number', 'development', 'VaR time horizon in days', ARRAY['risk', 'var'], true, false
FROM config_categories WHERE category_path = 'risk.var_calculation';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'var_historical_window_days', id, 'risk.var_calculation', '252'::jsonb, 'number', 'development', 'Historical data window for VaR calculation (trading days)', ARRAY['risk', 'var'], true, false
FROM config_categories WHERE category_path = 'risk.var_calculation';

-- Circuit Breaker Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_circuit_breakers', id, 'risk.circuit_breakers', 'true'::jsonb, 'boolean', 'development', 'Enable automatic circuit breakers', ARRAY['risk', 'circuit_breakers'], true, false
FROM config_categories WHERE category_path = 'risk.circuit_breakers';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'circuit_breaker_loss_threshold_pct', id, 'risk.circuit_breakers', '5.0'::jsonb, 'number', 'development', 'Loss threshold to trigger circuit breaker (percentage)', ARRAY['risk', 'circuit_breakers'], true, false
FROM config_categories WHERE category_path = 'risk.circuit_breakers';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'circuit_breaker_cooldown_minutes', id, 'risk.circuit_breakers', '30'::jsonb, 'number', 'development', 'Circuit breaker cooldown period in minutes', ARRAY['risk', 'circuit_breakers'], true, false
FROM config_categories WHERE category_path = 'risk.circuit_breakers';

-- === MACHINE LEARNING CONFIGURATION ===

-- Model Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'default_model_type', id, 'ml.models', '"tlob_transformer"'::jsonb, 'string', 'development', 'Default ML model for predictions', ARRAY['ml', 'models'], true, false
FROM config_categories WHERE category_path = 'ml.models';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'model_cache_size', id, 'ml.models', '5'::jsonb, 'number', 'development', 'Number of models to keep in memory cache', ARRAY['ml', 'models', 'performance'], false, true
FROM config_categories WHERE category_path = 'ml.models';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_gpu_acceleration', id, 'ml.models', 'true'::jsonb, 'boolean', 'development', 'Enable GPU acceleration for ML inference', ARRAY['ml', 'gpu', 'performance'], false, true
FROM config_categories WHERE category_path = 'ml.models';

-- Inference Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'inference_timeout_ms', id, 'ml.inference', '100'::jsonb, 'number', 'development', 'ML inference timeout in milliseconds', ARRAY['ml', 'inference', 'timeout'], true, false
FROM config_categories WHERE category_path = 'ml.inference';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'inference_timeout_ms', id, 'ml.inference', '50'::jsonb, 'number', 'production', 'Shorter inference timeout for production latency', ARRAY['ml', 'inference', 'timeout'], true, false
FROM config_categories WHERE category_path = 'ml.inference';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'batch_size', id, 'ml.inference', '32'::jsonb, 'number', 'development', 'Batch size for ML inference', ARRAY['ml', 'inference', 'performance'], true, false
FROM config_categories WHERE category_path = 'ml.inference';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'feature_lookback_periods', id, 'ml.inference', '20'::jsonb, 'number', 'development', 'Number of historical periods for feature generation', ARRAY['ml', 'inference', 'features'], true, false
FROM config_categories WHERE category_path = 'ml.inference';

-- Training Configuration
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'auto_retrain_enabled', id, 'ml.training', 'false'::jsonb, 'boolean', 'development', 'Enable automatic model retraining', ARRAY['ml', 'training'], true, false
FROM config_categories WHERE category_path = 'ml.training';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'retrain_interval_hours', id, 'ml.training', '24'::jsonb, 'number', 'development', 'Hours between automatic retraining', ARRAY['ml', 'training'], true, false
FROM config_categories WHERE category_path = 'ml.training';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'training_data_window_days', id, 'ml.training', '30'::jsonb, 'number', 'development', 'Days of historical data for training', ARRAY['ml', 'training'], true, false
FROM config_categories WHERE category_path = 'ml.training';

-- === MONITORING CONFIGURATION ===

-- Performance Monitoring
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'metrics_collection_interval_ms', id, 'monitoring', '1000'::jsonb, 'number', 'development', 'Metrics collection interval in milliseconds', ARRAY['monitoring', 'metrics'], true, false
FROM config_categories WHERE category_path = 'monitoring';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_prometheus_metrics', id, 'monitoring', 'true'::jsonb, 'boolean', 'development', 'Enable Prometheus metrics collection', ARRAY['monitoring', 'prometheus'], false, true
FROM config_categories WHERE category_path = 'monitoring';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'alert_latency_threshold_ms', id, 'monitoring', '1000'::jsonb, 'number', 'development', 'Alert threshold for high latency in milliseconds', ARRAY['monitoring', 'alerts', 'latency'], true, false
FROM config_categories WHERE category_path = 'monitoring';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'alert_latency_threshold_ms', id, 'monitoring', '100'::jsonb, 'number', 'production', 'Production latency alert threshold - much lower', ARRAY['monitoring', 'alerts', 'latency'], true, false
FROM config_categories WHERE category_path = 'monitoring';

-- === PERFORMANCE TUNING CONFIGURATION ===

-- CPU and Memory Settings
INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'worker_threads', id, 'performance', '4'::jsonb, 'number', 'development', 'Number of worker threads per service', ARRAY['performance', 'threading'], false, true
FROM config_categories WHERE category_path = 'performance';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'worker_threads', id, 'performance', '16'::jsonb, 'number', 'production', 'Production worker thread count', ARRAY['performance', 'threading'], false, true
FROM config_categories WHERE category_path = 'performance';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_cpu_affinity', id, 'performance', 'false'::jsonb, 'boolean', 'development', 'Enable CPU affinity for performance', ARRAY['performance', 'cpu'], false, true
FROM config_categories WHERE category_path = 'performance';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_cpu_affinity', id, 'performance', 'true'::jsonb, 'boolean', 'production', 'Enable CPU affinity in production for low latency', ARRAY['performance', 'cpu'], false, true
FROM config_categories WHERE category_path = 'performance';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_simd_optimization', id, 'performance', 'true'::jsonb, 'boolean', 'development', 'Enable SIMD optimizations', ARRAY['performance', 'simd'], false, true
FROM config_categories WHERE category_path = 'performance';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'garbage_collection_frequency_ms', id, 'performance', '60000'::jsonb, 'number', 'development', 'Memory garbage collection frequency', ARRAY['performance', 'memory'], false, true
FROM config_categories WHERE category_path = 'performance';

-- === TLI-SPECIFIC CONFIGURATION ===

INSERT INTO config_categories (category_name, parent_id, category_path, description, is_system, display_order) VALUES
('tli', NULL, 'tli', 'Terminal interface and dashboard configuration', false, 9);

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'grpc_port', id, 'tli', '50051'::jsonb, 'number', 'development', 'gRPC server port for TLI service', ARRAY['tli', 'grpc'], false, true
FROM config_categories WHERE category_path = 'tli';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'web_dashboard_port', id, 'tli', '8080'::jsonb, 'number', 'development', 'Web dashboard port', ARRAY['tli', 'dashboard'], false, true
FROM config_categories WHERE category_path = 'tli';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'enable_real_time_streaming', id, 'tli', 'true'::jsonb, 'boolean', 'development', 'Enable real-time data streaming to dashboard', ARRAY['tli', 'streaming'], true, false
FROM config_categories WHERE category_path = 'tli';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'max_concurrent_sessions', id, 'tli', '10'::jsonb, 'number', 'development', 'Maximum concurrent TLI sessions', ARRAY['tli', 'sessions'], true, false
FROM config_categories WHERE category_path = 'tli';

INSERT INTO config_settings (config_key, category_id, category_path, config_value, value_type, environment, description, tags, hot_reload, restart_required)
SELECT 'session_timeout_minutes', id, 'tli', '60'::jsonb, 'number', 'development', 'TLI session timeout in minutes', ARRAY['tli', 'sessions'], true, false
FROM config_categories WHERE category_path = 'tli';

-- === INITIAL CONFIGURATION SUBSCRIPTIONS ===

-- TLI Service subscribes to all configuration changes
INSERT INTO config_subscriptions (service_name, config_pattern, category_pattern, environment, subscription_type, is_active) VALUES
('tli', '*', '*', 'development', 'notify', true),
('tli', '*', '*', 'production', 'notify', true);

-- Trading service subscribes to trading and risk configurations
INSERT INTO config_subscriptions (service_name, config_pattern, category_pattern, environment, subscription_type, is_active) VALUES
('trading_engine', 'trading.*', 'trading.*', 'development', 'notify', true),
('trading_engine', 'risk.*', 'risk.*', 'development', 'notify', true),
('trading_engine', 'trading.*', 'trading.*', 'production', 'notify', true),
('trading_engine', 'risk.*', 'risk.*', 'production', 'notify', true);

-- ML service subscribes to ML configurations
INSERT INTO config_subscriptions (service_name, config_pattern, category_pattern, environment, subscription_type, is_active) VALUES
('ml_service', 'ml.*', 'ml.*', 'development', 'notify', true),
('ml_service', 'ml.*', 'ml.*', 'production', 'notify', true);

-- All services subscribe to system configurations
INSERT INTO config_subscriptions (service_name, config_pattern, category_pattern, environment, subscription_type, is_active) VALUES
('*', 'system.*', 'system.*', 'development', 'notify', true),
('*', 'system.*', 'system.*', 'production', 'notify', true);

-- === UPDATE STATISTICS ===
ANALYZE config_categories;
ANALYZE config_settings;
ANALYZE config_history;
ANALYZE config_environments;
ANALYZE config_environment_overrides;
ANALYZE config_subscriptions;
ANALYZE config_locks;

-- Initial configuration data loaded successfully!
-- 
-- Created:
-- - 4 environments: development, test, staging, production
-- - 24 configuration categories with hierarchical organization
-- - 67 configuration settings with environment-specific values
-- - Initial service subscriptions for hot-reload notifications
--
-- Key Features:
-- - Environment inheritance (staging inherits from development)
-- - Hot-reload enabled for most operational settings
-- - Restart required only for core infrastructure changes
-- - Sensitive settings marked appropriately
-- - Performance-optimized values for production vs development
-- - Complete subscription setup for real-time configuration updates
--
-- Next Steps:
-- 1. Services can start listening to 'foxhunt_config_changes' channel
-- 2. Use get_config_value('key', 'environment') to retrieve configuration
-- 3. Use set_config_value('key', value, 'environment') to update configuration
-- 4. TLI dashboard can manage all configurations through PostgreSQL