# ============================================================================
# DATABASE_INVENTORY.yml - Trading Platform Trading Platform
# ============================================================================
# Proposito: Inventario consolidado de todos los objetos de base de datos
# Ultima actualizacion: 2026-01-25
# Version: 2.1.0
# ============================================================================

version: "2.1.0"
project: "Trading Platform"
database: "trading_platform"
engine: "PostgreSQL 16"
last_updated: "2026-01-25"

# ============================================================================
# DATA STATUS (2026-01-25)
# ============================================================================
data_status:
  polygon_api_key: "Configured in apps/data-service/.env"
  data_source: "Polygon.io"
  last_sync: "2026-01-25"
  tickers_loaded:
    - symbol: XAUUSD
      bars: 70071
      range: "2025-01-26 to 2026-01-23"
    - symbol: EURUSD
      bars: 73783
      range: "2025-01-26 to 2026-01-23"
    - symbol: BTCUSD
      bars: 105107
      range: "2025-01-24 to 2026-01-24"
    - symbol: GBPUSD
      bars: 73493
      range: "2025-01-26 to 2026-01-23"
    - symbol: USDJPY
      bars: 73587
      range: "2025-01-26 to 2026-01-23"
    - symbol: AUDUSD
      bars: 73176
      range: "2025-01-26 to 2026-01-23"
  total_bars: 469217

# ============================================================================
# RESUMEN EJECUTIVO
# ============================================================================
summary:
  total_schemas: 13  # +1 auth
  total_tables: 95   # +4 (notifications, user_push_tokens, distribution_history, distribution_runs)
  total_enums: 31
  total_indexes: 157  # +8 nuevos indices
  total_triggers: 37
  total_functions: 9
  total_views: 6

# ============================================================================
# EXTENSIONES
# ============================================================================
extensions:
  - name: uuid-ossp
    purpose: "Generacion de UUIDs"
  - name: pgcrypto
    purpose: "Funciones criptograficas"
  - name: pg_trgm
    purpose: "Busqueda de texto similar"

# ============================================================================
# SCHEMAS
# ============================================================================
schemas:
  # --------------------------------------------------------------------------
  # PUBLIC - Usuarios y configuracion global
  # --------------------------------------------------------------------------
  - name: public
    description: "Usuarios, perfiles, configuracion global y autenticacion"
    epic: OQI-001
    tables:
      - name: users
        description: "Usuarios base del sistema"
        columns: 22
        indexes: 3
        rf: RF-AUTH-001
        key_columns:
          - id (UUID, PK)
          - email (VARCHAR, UNIQUE)
          - role (user_role_enum)
          - status (status_enum)
          - totp_enabled (BOOLEAN)

      - name: profiles
        description: "Informacion extendida del usuario"
        columns: 12
        indexes: 1
        rf: RF-AUTH-001
        key_columns:
          - user_id (UUID, FK -> users)
          - first_name, last_name
          - country_code, timezone

      - name: user_settings
        description: "Configuraciones y preferencias"
        columns: 14
        indexes: 0
        rf: RF-AUTH-001

      - name: kyc_verifications
        description: "Verificacion de identidad (KYC)"
        columns: 17
        indexes: 2
        rf: RF-INV-002

      - name: risk_profiles
        description: "Perfil de riesgo del usuario"
        columns: 10
        indexes: 1
        rf: RF-PFM-001

      - name: sessions
        description: "Sesiones activas de usuarios"
        columns: 9
        indexes: 3
        rf: RF-AUTH-004

      - name: notifications
        description: "Notificaciones del sistema"
        columns: 11
        indexes: 3
        rf: RF-AUTH-001

      - name: admin_roles
        description: "Roles administrativos especificos"
        columns: 12
        indexes: 2
        rf: RF-ADM-001

      - name: platform_analytics
        description: "Metricas agregadas de la plataforma"
        columns: 16
        indexes: 1
        rf: RF-ADM-002

      - name: api_keys
        description: "API keys para acceso programatico"
        columns: 12
        indexes: 2
        rf: RF-ADM-003

    enums:
      - name: status_enum
        values: [active, inactive, pending, suspended, deleted]

      - name: user_role_enum
        values: [investor, trader_pro, student, admin, risk_officer, support]

      - name: risk_profile_enum
        values: [conservative, moderate, aggressive]

      - name: trade_direction_enum
        values: [long, short]

      - name: order_status_enum
        values: [pending, open, filled, cancelled, expired]

      - name: transaction_type_enum
        values: [deposit, withdrawal, subscription_payment, course_purchase, investment_deposit, investment_withdrawal, profit_distribution, fee, refund]

      - name: kyc_status_enum
        values: [pending, submitted, approved, rejected, expired]

      - name: kyc_document_type
        values: [passport, national_id, drivers_license, residence_permit]

      - name: notification_type_enum
        values: [system, trading, investment, education, payment, security, marketing]

    functions:
      - name: update_updated_at_column()
        purpose: "Actualiza timestamp updated_at automaticamente"
        used_by: "Todos los triggers de updated_at"

    triggers:
      - name: update_users_updated_at
        table: users
      - name: update_profiles_updated_at
        table: profiles
      - name: update_user_settings_updated_at
        table: user_settings
      - name: update_kyc_updated_at
        table: kyc_verifications
      - name: update_risk_profiles_updated_at
        table: risk_profiles

  # --------------------------------------------------------------------------
  # AUTH - Autenticacion y notificaciones
  # --------------------------------------------------------------------------
  - name: auth
    description: "Autenticacion, tokens de acceso, notificaciones y push tokens"
    epic: OQI-001
    tables:
      - name: notifications
        description: "Notificaciones del sistema para usuarios"
        columns: 13
        indexes: 4
        rf: RF-AUTH-001
        key_columns:
          - id (UUID, PK)
          - user_id (UUID, FK -> auth.users)
          - type (VARCHAR) - alert_triggered, trade_executed, etc.
          - priority (VARCHAR) - low, normal, high, urgent
          - is_read (BOOLEAN)
        created: "2026-01-25"

      - name: user_push_tokens
        description: "Tokens FCM/APNs para push notifications"
        columns: 8
        indexes: 2
        rf: RF-AUTH-001
        key_columns:
          - id (UUID, PK)
          - user_id (UUID, FK -> auth.users)
          - token (TEXT, UNIQUE)
          - platform (VARCHAR) - web, ios, android
          - is_active (BOOLEAN)
        created: "2026-01-25"

    enums:
      - name: notification_priority
        values: [low, normal, high, urgent]

      - name: notification_type
        values: [alert_triggered, trade_executed, deposit_confirmed, withdrawal_completed, distribution_received, system_announcement, security_alert, account_update]

      - name: push_platform
        values: [web, ios, android]

  # --------------------------------------------------------------------------
  # EDUCATION - Cursos y contenido educativo
  # --------------------------------------------------------------------------
  - name: education
    description: "Cursos, lecciones, inscripciones y contenido educativo"
    epic: OQI-002
    tables:
      - name: categories
        description: "Categorias de cursos"
        columns: 8
        indexes: 2
        rf: RF-EDU-001

      - name: courses
        description: "Cursos de trading"
        columns: 24
        indexes: 5
        rf: RF-EDU-001

      - name: modules
        description: "Modulos/secciones de un curso"
        columns: 7
        indexes: 1
        rf: RF-EDU-002

      - name: lessons
        description: "Lecciones individuales"
        columns: 16
        indexes: 2
        rf: RF-EDU-002

      - name: quizzes
        description: "Cuestionarios y evaluaciones"
        columns: 11
        indexes: 2
        rf: RF-EDU-003

      - name: quiz_questions
        description: "Preguntas de cuestionarios"
        columns: 9
        indexes: 1
        rf: RF-EDU-003

      - name: enrollments
        description: "Inscripciones de usuarios a cursos"
        columns: 13
        indexes: 3
        rf: RF-EDU-004

      - name: lesson_progress
        description: "Progreso por leccion"
        columns: 10
        indexes: 3
        rf: RF-EDU-004

      - name: quiz_attempts
        description: "Intentos de cuestionarios"
        columns: 10
        indexes: 2
        rf: RF-EDU-003

      - name: course_reviews
        description: "Resenas de cursos"
        columns: 8
        indexes: 1
        rf: RF-EDU-005

      - name: ai_content_generations
        description: "Registro de contenido generado por IA"
        columns: 10
        indexes: 1
        rf: RF-EDU-006

      - name: certificates
        description: "Certificados de finalizacion de cursos"
        columns: 13
        indexes: 3
        rf: RF-EDU-007

    enums:
      - name: course_level_enum
        values: [beginner, intermediate, advanced, expert]

      - name: course_status_enum
        values: [draft, published, archived]

      - name: content_type_enum
        values: [video, text, quiz, exercise, resource]

      - name: enrollment_status_enum
        values: [active, completed, expired, cancelled]

      - name: question_type_enum
        values: [multiple_choice, true_false, multiple_answer, short_answer]

    triggers:
      - name: update_courses_updated_at
        table: courses
      - name: update_modules_updated_at
        table: modules
      - name: update_lessons_updated_at
        table: lessons
      - name: update_enrollments_updated_at
        table: enrollments
      - name: update_lesson_progress_updated_at
        table: lesson_progress
      - name: set_certificate_number
        table: certificates

    functions:
      - name: generate_certificate_number()
        purpose: "Genera numero de certificado OQ-CERT-XXXXXX"

  # --------------------------------------------------------------------------
  # TRADING - Bots, senales y operaciones
  # --------------------------------------------------------------------------
  - name: trading
    description: "Bots, senales, estrategias y operaciones de trading"
    epic: OQI-003
    tables:
      - name: symbols
        description: "Instrumentos financieros disponibles"
        columns: 16
        indexes: 2
        rf: RF-TRD-001

      - name: strategies
        description: "Estrategias de trading disponibles"
        columns: 16
        indexes: 1
        rf: RF-TRD-002

      - name: bots
        description: "Agentes de trading (Atlas, Orion, Nova)"
        columns: 25
        indexes: 3
        rf: RF-INV-005

      - name: signals
        description: "Senales de trading generadas"
        columns: 27
        indexes: 5
        rf: RF-ML-004

      - name: positions
        description: "Posiciones de trading"
        columns: 20
        indexes: 5
        rf: RF-TRD-004

      - name: price_alerts
        description: "Alertas de precio configuradas"
        columns: 13
        indexes: 3
        rf: RF-TRD-006

      - name: watchlists
        description: "Listas de seguimiento"
        columns: 5
        indexes: 1
        rf: RF-TRD-007

      - name: watchlist_items
        description: "Items en watchlists"
        columns: 5
        indexes: 1
        rf: RF-TRD-007

      - name: paper_trading_accounts
        description: "Cuentas de paper trading"
        columns: 12
        indexes: 1
        rf: RF-TRD-008

      - name: paper_trading_positions
        description: "Posiciones de paper trading"
        columns: 15
        indexes: 3
        rf: RF-TRD-008

    enums:
      - name: bot_status_enum
        values: [active, paused, stopped, error, maintenance]

      - name: signal_status_enum
        values: [pending, active, triggered, expired, cancelled]

      - name: position_status_enum
        values: [open, closed, pending]

      - name: timeframe_enum
        values: [1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w]

      - name: amd_phase_enum
        values: [accumulation, manipulation, distribution, unknown]

      - name: volatility_regime_enum
        values: [low, medium, high, extreme]

      - name: alert_condition_enum
        values: [above, below, crosses_above, crosses_below]

    triggers:
      - name: update_symbols_updated_at
        table: symbols
      - name: update_strategies_updated_at
        table: strategies
      - name: update_bots_updated_at
        table: bots
      - name: update_signals_updated_at
        table: signals
      - name: update_positions_updated_at
        table: positions

  # --------------------------------------------------------------------------
  # INVESTMENT - Cuentas de inversion
  # --------------------------------------------------------------------------
  - name: investment
    description: "Cuentas de inversion, productos y gestion de portafolios"
    epic: OQI-004
    tables:
      - name: products
        description: "Productos de inversion (Atlas, Orion, Nova)"
        columns: 22
        indexes: 3
        rf: RF-INV-001

      - name: accounts
        description: "Cuentas de inversion de usuarios"
        columns: 28
        indexes: 4
        rf: RF-INV-002

      - name: account_transactions
        description: "Movimientos en cuentas"
        columns: 13
        indexes: 3
        rf: RF-INV-002

      - name: performance_snapshots
        description: "Snapshots periodicos de rendimiento"
        columns: 15
        indexes: 2
        rf: RF-INV-006

      - name: profit_distributions
        description: "Distribucion de utilidades"
        columns: 15
        indexes: 3
        rf: RF-INV-006

      - name: deposit_requests
        description: "Solicitudes de deposito"
        columns: 14
        indexes: 3
        rf: RF-INV-003

      - name: withdrawal_requests
        description: "Solicitudes de retiro"
        columns: 16
        indexes: 3
        rf: RF-INV-004

      - name: bot_assignments
        description: "Asignacion de bots a cuentas"
        columns: 9
        indexes: 3
        rf: RF-INV-005

      - name: distribution_history
        description: "Historial de distribuciones diarias por cuenta"
        columns: 10
        indexes: 3
        rf: RF-INV-006
        key_columns:
          - id (UUID, PK)
          - account_id (UUID, FK -> investment.accounts)
          - product_id (UUID, FK -> investment.products)
          - distribution_date (DATE)
          - gross_amount, fee_amount, net_amount (DECIMAL)
          - balance_before, balance_after (DECIMAL)
        constraints:
          - UNIQUE (account_id, distribution_date)
          - CHECK (balance_after = balance_before + net_amount)
        created: "2026-01-25"

      - name: distribution_runs
        description: "Logs de ejecucion del job de distribucion"
        columns: 13
        indexes: 2
        rf: RF-INV-006
        key_columns:
          - id (UUID, PK)
          - run_date (DATE, UNIQUE)
          - total_accounts, successful_count, failed_count (INTEGER)
          - total_gross_amount, total_fee_amount, total_net_amount (DECIMAL)
          - started_at, completed_at (TIMESTAMPTZ)
          - duration_ms (INTEGER)
          - error_details (JSONB)
        constraints:
          - CHECK (total_accounts = successful_count + failed_count)
        created: "2026-01-25"

    enums:
      - name: product_type_enum
        values: [fixed_return, variable_return, long_term_portfolio]

      - name: account_status_enum
        values: [pending_kyc, pending_deposit, active, paused, suspended, closed]

      - name: fee_type_enum
        values: [management, performance, deposit, withdrawal, subscription]

      - name: account_transaction_type
        values: [deposit, withdrawal, profit, loss, fee, adjustment, transfer_in, transfer_out]

    functions:
      - name: generate_account_number()
        purpose: "Genera numero de cuenta OQ-INV-XXXXXX"

    triggers:
      - name: update_products_updated_at
        table: products
      - name: update_accounts_updated_at
        table: accounts
      - name: update_deposit_requests_updated_at
        table: deposit_requests
      - name: update_withdrawal_requests_updated_at
        table: withdrawal_requests
      - name: update_profit_distributions_updated_at
        table: profit_distributions
      - name: set_account_number
        table: accounts
        purpose: "Auto-genera numero de cuenta"

  # --------------------------------------------------------------------------
  # FINANCIAL - Pagos y suscripciones
  # --------------------------------------------------------------------------
  - name: financial
    description: "Pagos, suscripciones, wallets y transacciones"
    epic: OQI-005
    tables:
      - name: subscription_plans
        description: "Planes de suscripcion (Free, Basic, Pro, Premium)"
        columns: 19
        indexes: 1
        rf: RF-PAY-001

      - name: stripe_customers
        description: "Clientes de Stripe vinculados"
        columns: 7
        indexes: 2
        rf: RF-PAY-002

      - name: subscriptions
        description: "Suscripciones activas"
        columns: 17
        indexes: 4
        rf: RF-PAY-001

      - name: wallets
        description: "Wallets internos de usuarios"
        columns: 10
        indexes: 1
        rf: RF-PAY-003

      - name: wallet_transactions
        description: "Transacciones en wallets"
        columns: 14
        indexes: 4
        rf: RF-PAY-003

      - name: payments
        description: "Pagos procesados"
        columns: 21
        indexes: 5
        rf: RF-PAY-002

      - name: refunds
        description: "Reembolsos procesados"
        columns: 11
        indexes: 2
        rf: RF-PAY-004

      - name: invoices
        description: "Facturas generadas"
        columns: 17
        indexes: 3
        rf: RF-PAY-005

      - name: payout_requests
        description: "Solicitudes de retiro a cuenta externa"
        columns: 16
        indexes: 3
        rf: RF-PAY-004

      - name: promo_codes
        description: "Codigos promocionales"
        columns: 16
        indexes: 2
        rf: RF-PAY-006

      - name: promo_code_uses
        description: "Uso de codigos promocionales"
        columns: 5
        indexes: 2
        rf: RF-PAY-006

      - name: auto_investment_schedules
        description: "Programacion de inversiones automaticas recurrentes"
        columns: 14
        indexes: 2
        rf: RF-PAY-007

    enums:
      - name: subscription_status_enum
        values: [trialing, active, past_due, cancelled, unpaid, paused]

      - name: payment_status_enum
        values: [pending, processing, succeeded, failed, refunded, cancelled]

      - name: payment_method_enum
        values: [card, bank_transfer, paypal, crypto, wallet_balance]

    triggers:
      - name: update_plans_updated_at
        table: subscription_plans
      - name: update_stripe_customers_updated_at
        table: stripe_customers
      - name: update_subscriptions_updated_at
        table: subscriptions
      - name: update_wallets_updated_at
        table: wallets
      - name: update_payments_updated_at
        table: payments
      - name: update_invoices_updated_at
        table: invoices
      - name: update_payouts_updated_at
        table: payout_requests
      - name: update_auto_invest_updated_at
        table: auto_investment_schedules

  # --------------------------------------------------------------------------
  # ML - Machine Learning
  # --------------------------------------------------------------------------
  - name: ml
    description: "Modelos ML, predicciones, features y metricas"
    epic: OQI-006
    tables:
      - name: models
        description: "Registro de modelos ML"
        columns: 22
        indexes: 4
        rf: RF-ML-001

      - name: training_runs
        description: "Ejecuciones de entrenamiento"
        columns: 18
        indexes: 2
        rf: RF-ML-001

      - name: model_metrics
        description: "Metricas de rendimiento de modelos"
        columns: 21
        indexes: 2
        rf: RF-ML-002

      - name: predictions
        description: "Predicciones generadas"
        columns: 24
        indexes: 4
        rf: RF-ML-003

      - name: prediction_accuracy_daily
        description: "Precision de predicciones agregada por dia"
        columns: 13
        indexes: 2
        rf: RF-ML-003

      - name: feature_store
        description: "Features pre-calculadas"
        columns: 11
        indexes: 2
        rf: RF-ML-002

      - name: model_drift_alerts
        description: "Alertas de drift de modelo"
        columns: 13
        indexes: 2
        rf: RF-ML-005

      - name: ab_tests
        description: "Tests A/B de modelos"
        columns: 18
        indexes: 1
        rf: RF-ML-005

    enums:
      - name: model_type_enum
        values: [range_predictor, tpsl_classifier, signal_generator, regime_classifier, amd_detector, volatility_model, ensemble]

      - name: model_status_enum
        values: [training, validating, ready, deployed, deprecated, failed]

    functions:
      - name: set_model_as_latest()
        purpose: "Marca modelo como version latest"

    triggers:
      - name: update_models_updated_at
        table: models
      - name: update_ab_tests_updated_at
        table: ab_tests
      - name: ensure_single_latest_model
        table: models

  # --------------------------------------------------------------------------
  # AUDIT - Auditoria y seguridad
  # --------------------------------------------------------------------------
  - name: audit
    description: "Logs de auditoria, eventos del sistema y seguridad"
    epic: OQI-001
    tables:
      - name: audit_logs
        description: "Log general de acciones"
        columns: 12
        indexes: 5
        rf: RF-AUTH-005

      - name: security_events
        description: "Eventos de seguridad"
        columns: 14
        indexes: 5
        rf: RF-AUTH-005

      - name: system_events
        description: "Eventos del sistema"
        columns: 11
        indexes: 4
        rf: RF-AUTH-005

      - name: trading_audit
        description: "Auditoria de operaciones de trading"
        columns: 21
        indexes: 7
        rf: RF-TRD-005

      - name: api_request_logs
        description: "Logs de requests a la API"
        columns: 13
        indexes: 4
        rf: RF-AUTH-005

      - name: data_access_logs
        description: "Log de acceso a datos sensibles"
        columns: 9
        indexes: 4
        rf: RF-AUTH-005

      - name: compliance_logs
        description: "Logs de cumplimiento regulatorio"
        columns: 14
        indexes: 4
        rf: RF-AUTH-005

    enums:
      - name: security_event_type
        values: [login_success, login_failed, logout, password_changed, password_reset_requested, password_reset_completed, 2fa_enabled, 2fa_disabled, 2fa_failed, account_locked, account_unlocked, suspicious_activity, api_key_created, api_key_revoked, permission_denied, rate_limit_exceeded]

      - name: security_severity
        values: [info, warning, error, critical]

      - name: system_event_type
        values: [service_started, service_stopped, service_error, database_backup, database_restore, deployment, config_changed, scheduled_job_started, scheduled_job_completed, scheduled_job_failed, integration_connected, integration_disconnected, integration_error, alert_triggered, maintenance_started, maintenance_completed]

    functions:
      - name: log_changes()
        purpose: "Funcion de auditoria automatica"

      - name: cleanup_old_logs()
        purpose: "Limpieza de logs antiguos"

    views:
      - name: admin_user_activity
        purpose: "Vista de actividad reciente de usuarios"

      - name: admin_security_alerts
        purpose: "Vista de alertas de seguridad pendientes"

      - name: admin_bot_trading_summary
        purpose: "Resumen de trading por bot"

      - name: v_admin_dashboard
        purpose: "Dashboard administrativo general"

  # --------------------------------------------------------------------------
  # MARKET_DATA - Datos de mercado (migrados de TradingAgent)
  # --------------------------------------------------------------------------
  - name: market_data
    description: "Datos OHLCV, tickers, indicadores tecnicos y predicciones ML"
    epic: OQI-006
    migration: "Migrado desde MySQL (TradingAgent) - 13M+ registros"
    tables:
      - name: tickers
        description: "Instrumentos financieros disponibles"
        columns: 10
        indexes: 2
        rf: RF-ML-001
        key_columns:
          - id (INTEGER, PK)
          - symbol (VARCHAR, UNIQUE)
          - asset_type (asset_type_enum)
          - is_active (BOOLEAN)
        records: 25

      - name: ohlcv_5m
        description: "Datos OHLCV en timeframe 5 minutos (particionado por año)"
        columns: 8
        indexes: 4
        rf: RF-ML-002
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - timestamp (TIMESTAMPTZ)
          - open, high, low, close, volume
        records: "13,000,000+"
        partitioned_by: "year (2020-2026)"

      - name: technical_indicators
        description: "Indicadores tecnicos calculados"
        columns: 22
        indexes: 2
        rf: RF-ML-002
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - timestamp (TIMESTAMPTZ)
          - sma_*, ema_*, rsi, macd_*, atr
        records: "13,000,000+"
        partitioned_by: "year (2020-2026)"

      - name: ohlcv_5m_staging
        description: "Tabla temporal para carga de datos"
        columns: 8
        indexes: 0
        rf: RF-ML-002
        note: "Usado por Data Service para carga incremental"

    enums:
      - name: asset_type_enum
        values: [forex, crypto, index, commodity, stock]

    triggers:
      - name: update_tickers_updated_at
        table: tickers

  # --------------------------------------------------------------------------
  # ML_PREDICTIONS - Predicciones de Machine Learning
  # --------------------------------------------------------------------------
  - name: ml_predictions
    description: "Predicciones de rangos, señales de entrada y analisis de mercado"
    epic: OQI-006
    tables:
      - name: range_predictions
        description: "Predicciones de rango max/min para estrategia R:R"
        columns: 18
        indexes: 4
        rf: RF-ML-003
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - prediction_timestamp (TIMESTAMPTZ)
          - predicted_high, predicted_low
          - confidence_score
          - rr_ratio
        partitioned_by: "year (2020-2026)"

      - name: entry_signals
        description: "Señales de entrada con niveles TP/SL"
        columns: 22
        indexes: 4
        rf: RF-ML-004
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - signal_timestamp (TIMESTAMPTZ)
          - entry_price, stop_loss, take_profit
          - direction (trade_direction_enum)
          - expected_spread (NULLABLE - nuevo)
          - net_rr_ratio (NULLABLE - nuevo)

      - name: market_analysis
        description: "Analisis de mercado generado por ML"
        columns: 14
        indexes: 2
        rf: RF-ML-005
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - analysis_timestamp
          - amd_phase, volatility_regime

    triggers:
      - name: update_range_predictions_updated_at
        table: range_predictions

  # --------------------------------------------------------------------------
  # DATA_SOURCES - Proveedores de datos externos
  # --------------------------------------------------------------------------
  - name: data_sources
    description: "Configuracion de APIs externas y sincronizacion de datos"
    epic: OQI-006
    integration: INT-DATA-001
    tables:
      - name: api_providers
        description: "Proveedores de API registrados (Polygon, MetaAPI)"
        columns: 14
        indexes: 1
        rf: RF-ML-002
        key_columns:
          - id (SERIAL, PK)
          - name (VARCHAR, UNIQUE)
          - api_type (api_provider_type)
          - base_url, rate_limit_per_minute
          - is_active (BOOLEAN)

      - name: ticker_mapping
        description: "Mapeo de simbolos internos a simbolos de proveedores"
        columns: 9
        indexes: 3
        rf: RF-ML-002
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - provider_id (INTEGER, FK -> api_providers)
          - provider_symbol (VARCHAR)
          - multiplier (DECIMAL)

      - name: data_sync_status
        description: "Estado de sincronizacion por ticker/proveedor"
        columns: 11
        indexes: 2
        rf: RF-ML-002
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - provider_id (INTEGER, FK -> api_providers)
          - last_sync_at, next_sync_at
          - sync_status (sync_status_enum)
          - records_synced (INTEGER)

    enums:
      - name: api_provider_type
        values: [polygon, massive, metaapi, mt4_direct, binance, interactive_brokers]

      - name: sync_status_enum
        values: [pending, running, completed, failed, paused]

    triggers:
      - name: update_api_providers_updated_at
        table: api_providers
      - name: update_data_sync_status_updated_at
        table: data_sync_status

  # --------------------------------------------------------------------------
  # BROKER_INTEGRATION - Integracion con brokers (MT4/MT5)
  # --------------------------------------------------------------------------
  - name: broker_integration
    description: "Cuentas de broker, precios, spreads y ejecucion de trades"
    epic: OQI-004
    integration: INT-DATA-001
    tables:
      - name: broker_accounts
        description: "Cuentas de broker conectadas via MetaAPI"
        columns: 16
        indexes: 3
        rf: RF-INV-002
        key_columns:
          - id (SERIAL, PK)
          - user_id (UUID, FK -> users)
          - broker_name (VARCHAR)
          - account_number, account_type
          - metaapi_account_id (VARCHAR)
          - balance, equity, margin
          - is_active (BOOLEAN)

      - name: broker_prices
        description: "Precios bid/ask en tiempo real del broker"
        columns: 10
        indexes: 2
        rf: RF-TRD-001
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - timestamp (TIMESTAMPTZ)
          - bid, ask, spread
          - account_id (INTEGER, FK -> broker_accounts)

      - name: spread_statistics
        description: "Estadisticas de spread por sesion de trading"
        columns: 14
        indexes: 3
        rf: RF-TRD-001
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - date (DATE)
          - session (trading_session_enum)
          - avg_spread, min_spread, max_spread
          - sample_count (INTEGER)

      - name: price_adjustment_model
        description: "Modelo de ajuste de precios source vs broker"
        columns: 12
        indexes: 2
        rf: RF-ML-002
        key_columns:
          - ticker_id (INTEGER, FK -> tickers)
          - model_version (INTEGER)
          - offset_bid, offset_ask
          - volatility_factor
          - trained_at (TIMESTAMPTZ)

      - name: trade_execution
        description: "Historial de trades ejecutados via broker"
        columns: 24
        indexes: 5
        rf: RF-TRD-004
        key_columns:
          - id (SERIAL, PK)
          - signal_id (INTEGER, FK -> entry_signals)
          - broker_account_id (INTEGER, FK -> broker_accounts)
          - order_type, direction
          - entry_price, exit_price
          - spread_at_entry, slippage
          - pnl, pnl_pips

    enums:
      - name: trading_session_enum
        values: [asian, london, newyork, overlap_london_ny, weekend]

      - name: broker_account_type
        values: [demo, live, paper]

      - name: order_type_enum
        values: [market, limit, stop, stop_limit]

    functions:
      - name: calculate_spread_adjusted_entry()
        purpose: "Calcula entry price ajustado por spread esperado"
        returns: "RECORD(entry, sl, tp, net_rr)"

      - name: get_expected_spread()
        purpose: "Obtiene spread esperado por ticker y sesion"
        returns: "DECIMAL"

      - name: adjust_price_to_broker()
        purpose: "Ajusta precio de source a precio de broker"
        returns: "RECORD(adjusted_bid, adjusted_ask)"

    triggers:
      - name: update_broker_accounts_updated_at
        table: broker_accounts
      - name: update_trade_execution_updated_at
        table: trade_execution

  # --------------------------------------------------------------------------
  # LLM - LLM Agent (OQI-007)
  # --------------------------------------------------------------------------
  - name: llm
    description: "Agente conversacional LLM para asistencia de trading"
    epic: OQI-007
    tables:
      - name: conversations
        description: "Historial de conversaciones con el agente"
        columns: 11
        indexes: 2
        rf: RF-LLM-001

      - name: conversation_messages
        description: "Mensajes individuales en conversaciones"
        columns: 11
        indexes: 2
        rf: RF-LLM-001

      - name: llm_tools_usage
        description: "Log de uso de herramientas/funciones por el LLM"
        columns: 11
        indexes: 2
        rf: RF-LLM-002

      - name: llm_proactive_notifications
        description: "Notificaciones proactivas generadas por el agente"
        columns: 13
        indexes: 2
        rf: RF-LLM-003

      - name: llm_usage_limits
        description: "Limites de uso por usuario segun plan"
        columns: 11
        indexes: 1
        rf: RF-LLM-004

    triggers:
      - name: update_conversations_updated_at
        table: conversations
      - name: update_llm_limits_updated_at
        table: llm_usage_limits

  # --------------------------------------------------------------------------
  # PORTFOLIO_MANAGEMENT - Gestion Avanzada de Portfolios (OQI-008)
  # --------------------------------------------------------------------------
  - name: portfolio_management
    description: "Gestion avanzada de portfolios multi-cuenta y rebalanceo"
    epic: OQI-008
    tables:
      - name: portfolios
        description: "Portfolios multi-cuenta de usuarios"
        columns: 21
        indexes: 1
        rf: RF-PFM-001

      - name: portfolio_accounts
        description: "Cuentas asignadas a portfolios"
        columns: 9
        indexes: 2
        rf: RF-PFM-001

      - name: rebalance_suggestions
        description: "Sugerencias de rebalanceo automaticas"
        columns: 12
        indexes: 2
        rf: RF-PFM-002

      - name: investment_goals
        description: "Metas de inversion de usuarios"
        columns: 15
        indexes: 3
        rf: RF-PFM-003

      - name: portfolio_snapshots
        description: "Snapshots periodicos del portfolio completo"
        columns: 14
        indexes: 2
        rf: RF-PFM-004

      - name: monte_carlo_projections
        description: "Proyecciones Monte Carlo de portfolios"
        columns: 15
        indexes: 1
        rf: RF-PFM-005

    triggers:
      - name: update_portfolios_updated_at
        table: portfolios
      - name: update_portfolio_accounts_updated_at
        table: portfolio_accounts
      - name: update_rebalance_updated_at
        table: rebalance_suggestions
      - name: update_goals_updated_at
        table: investment_goals

    views:
      - name: v_portfolio_summary
        purpose: "Resumen de portfolios con todas las cuentas"

# ============================================================================
# RELACIONES ENTRE TABLAS (Claves Foraneas Principales)
# ============================================================================
relationships:
  - from: public.profiles
    to: public.users
    type: "1:1"

  - from: public.sessions
    to: public.users
    type: "N:1"

  - from: education.courses
    to: education.categories
    type: "N:1"

  - from: education.lessons
    to: education.modules
    type: "N:1"

  - from: education.enrollments
    to: public.users
    type: "N:1"

  - from: education.enrollments
    to: education.courses
    type: "N:1"

  - from: trading.signals
    to: trading.bots
    type: "N:1"

  - from: trading.positions
    to: trading.signals
    type: "N:1"

  - from: investment.accounts
    to: public.users
    type: "N:1"

  - from: investment.accounts
    to: investment.products
    type: "N:1"

  - from: investment.accounts
    to: trading.bots
    type: "N:1"

  - from: financial.subscriptions
    to: public.users
    type: "N:1"

  - from: financial.subscriptions
    to: financial.subscription_plans
    type: "N:1"

  - from: financial.wallets
    to: public.users
    type: "N:1"

  - from: ml.predictions
    to: ml.models
    type: "N:1"

  # Nuevas relaciones - Data Service (INT-DATA-001)
  - from: data_sources.ticker_mapping
    to: market_data.tickers
    type: "N:1"

  - from: data_sources.ticker_mapping
    to: data_sources.api_providers
    type: "N:1"

  - from: data_sources.data_sync_status
    to: market_data.tickers
    type: "N:1"

  - from: market_data.ohlcv_5m
    to: market_data.tickers
    type: "N:1"

  - from: ml_predictions.range_predictions
    to: market_data.tickers
    type: "N:1"

  - from: ml_predictions.entry_signals
    to: market_data.tickers
    type: "N:1"

  - from: broker_integration.broker_accounts
    to: public.users
    type: "N:1"

  - from: broker_integration.broker_prices
    to: market_data.tickers
    type: "N:1"

  - from: broker_integration.spread_statistics
    to: market_data.tickers
    type: "N:1"

  - from: broker_integration.trade_execution
    to: ml_predictions.entry_signals
    type: "N:1"

  - from: broker_integration.trade_execution
    to: broker_integration.broker_accounts
    type: "N:1"

# ============================================================================
# MAPEO A EPICAS
# ============================================================================
epic_mapping:
  OQI-001:
    schemas: [public, audit]
    tables: 14
    description: "Autenticacion, usuarios y auditoria"

  OQI-002:
    schemas: [education]
    tables: 11
    description: "Modulo educativo completo"

  OQI-003:
    schemas: [trading]
    tables: 10
    description: "Trading, charts, paper trading"

  OQI-004:
    schemas: [investment]
    tables: 8
    description: "Cuentas de inversion y productos"

  OQI-005:
    schemas: [financial]
    tables: 11
    description: "Pagos, suscripciones, wallets"

  OQI-006:
    schemas: [ml, market_data, ml_predictions, data_sources]
    tables: 17
    description: "Machine Learning, datos de mercado y predicciones"
    integrations: [INT-DATA-001]

  OQI-007:
    schemas: [llm]
    tables: 5
    description: "LLM Agent - Conversational AI for trading"
    note: "Schema llm implementado en 003_complete_model.sql"

  OQI-008:
    schemas: [broker_integration, portfolio_management]
    tables: 11
    description: "Portfolio Manager - integracion con brokers y gestion avanzada de portfolios"
    integrations: [INT-DATA-001]

# ============================================================================
# ARCHIVOS SQL
# ============================================================================
sql_files:
  - path: apps/database/schemas/00_init_schemas.sql
    purpose: "Creacion de schemas y extensiones base"

  - path: apps/database/schemas/01_public_schema.sql
    purpose: "Usuarios, perfiles, autenticacion"

  - path: apps/database/schemas/01b_oauth_providers.sql
    purpose: "Proveedores OAuth"

  - path: apps/database/schemas/02_education_schema.sql
    purpose: "Cursos, lecciones, inscripciones"

  - path: apps/database/schemas/03_trading_schema.sql
    purpose: "Bots, senales, estrategias"

  - path: apps/database/schemas/04_investment_schema.sql
    purpose: "Cuentas de inversion, productos"

  - path: apps/database/schemas/05_financial_schema.sql
    purpose: "Pagos, suscripciones, wallets"

  - path: apps/database/schemas/06_ml_schema.sql
    purpose: "Modelos ML, predicciones, features"

  - path: apps/database/schemas/07_audit_schema.sql
    purpose: "Logs de auditoria y seguridad"

  - path: apps/database/migrations/001_mysql_to_postgres.sql
    purpose: "Migracion de datos desde MySQL (TradingAgent)"
    note: "13M+ registros, 18 tickers originales"

  - path: apps/database/migrations/002_add_indexes_and_integrations.sql
    purpose: "Indices de performance y schemas de integracion"
    note: "Data Service - Polygon/MetaAPI"

  - path: apps/database/migrations/003_complete_model.sql
    purpose: "Completar modelo para todas las funcionalidades SaaS"
    note: "LLM Agent, Portfolio Manager, Admin, Certificates, API Keys"

# ============================================================================
# INTEGRACIONES EXTERNAS
# ============================================================================
integrations:
  INT-DATA-001:
    name: "Data Service"
    description: "Integracion con Polygon/Massive API y MetaTrader 4"
    schemas_created: [data_sources, broker_integration]
    tables_created: 8
    functions_created: 3
    documentation: "docs/90-transversal/integraciones/INT-DATA-001-data-service.md"

# ============================================================================
# NOTAS Y PENDIENTES
# ============================================================================
notes:
  - "Schema 'llm' implementado completamente en 003_complete_model.sql"
  - "Schema 'portfolio_management' implementado para gestion avanzada"
  - "Particionamiento activo en market_data.ohlcv_5m y ml_predictions.* por año"
  - "RLS policies pendientes de definir"
  - "Data Service requiere configuracion de API keys (Polygon, MetaAPI)"
  - "Modelo de ajuste de precios requiere entrenamiento inicial"
  - "Certificados educativos listos para emision automatica"
  - "Sistema de API keys implementado para acceso programatico"
  - "Portfolio Manager soporta rebalanceo automatico y proyecciones Monte Carlo"
  - "LLM Agent con soporte completo para conversaciones y herramientas"

# ============================================================================
# FIN DEL INVENTARIO
# ============================================================================
