-- =====================================================
-- ML SCHEMA - MODELS TABLE
-- =====================================================
-- Description: ML models registry
-- Schema: ml
-- Author: Database Agent
-- Date: 2025-12-06
-- =====================================================

CREATE TABLE ml.models (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Identificación
  name VARCHAR(100) NOT NULL UNIQUE,
  display_name VARCHAR(200) NOT NULL,
  description TEXT,

  -- Tipo y framework
  model_type ml.model_type NOT NULL,
  framework ml.framework NOT NULL,

  -- Categoría
  category VARCHAR(50) NOT NULL, -- sentiment, technical, fundamental, hybrid

  -- Alcance
  applies_to_symbols VARCHAR(20)[] DEFAULT '{}', -- ['BTCUSDT', 'ETHUSDT'] o [] para todos
  applies_to_timeframes VARCHAR(10)[] DEFAULT '{}', -- ['1h', '4h', '1d'] o [] para todos

  -- Estado
  status ml.model_status DEFAULT 'development',

  -- Versión actual en producción
  current_version_id UUID,

  -- Metadata
  owner VARCHAR(100) NOT NULL,
  repository_url VARCHAR(500),
  documentation_url VARCHAR(500),

  -- Métricas agregadas (de todas las versiones)
  total_predictions INTEGER DEFAULT 0,
  total_correct_predictions INTEGER DEFAULT 0,
  overall_accuracy DECIMAL(5,4),

  -- Fechas
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  deployed_at TIMESTAMPTZ,
  deprecated_at TIMESTAMPTZ
);

-- Índices
CREATE INDEX idx_models_name ON ml.models(name);
CREATE INDEX idx_models_status ON ml.models(status);
CREATE INDEX idx_models_type ON ml.models(model_type);
CREATE INDEX idx_models_category ON ml.models(category);
CREATE INDEX idx_models_production ON ml.models(status) WHERE status = 'production';

-- Comentarios
COMMENT ON TABLE ml.models IS 'Registry of ML models for trading signals';
COMMENT ON COLUMN ml.models.name IS 'Unique technical name (e.g., sentiment_analyzer_v1)';
COMMENT ON COLUMN ml.models.applies_to_symbols IS 'Symbols this model can analyze. Empty array = all symbols';
COMMENT ON COLUMN ml.models.applies_to_timeframes IS 'Timeframes this model supports. Empty array = all timeframes';
COMMENT ON COLUMN ml.models.current_version_id IS 'Reference to current production version';
COMMENT ON COLUMN ml.models.overall_accuracy IS 'Aggregated accuracy across all versions and predictions';
