-- ============================================================================
-- Schema: trading
-- Table: watchlists
-- Description: Listas de seguimiento personalizadas de usuarios
-- Dependencies: auth.users
-- ============================================================================

CREATE TABLE trading.watchlists (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Propietario
  user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,

  -- Configuración
  name VARCHAR(100) NOT NULL,
  description TEXT,

  -- Ordenamiento
  display_order INTEGER DEFAULT 0,

  -- Estado
  is_default BOOLEAN DEFAULT false,
  is_public BOOLEAN DEFAULT false,

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

  -- Constraint: nombre único por usuario
  CONSTRAINT uq_watchlists_user_name UNIQUE (user_id, name)
);

-- Índices
CREATE INDEX idx_watchlists_user ON trading.watchlists(user_id);
CREATE INDEX idx_watchlists_user_default ON trading.watchlists(user_id, is_default) WHERE is_default = true;

-- Comentarios
COMMENT ON TABLE trading.watchlists IS 'Listas de seguimiento personalizadas de símbolos';
COMMENT ON COLUMN trading.watchlists.is_default IS 'Indica si es la watchlist por defecto del usuario';
COMMENT ON COLUMN trading.watchlists.is_public IS 'Indica si la watchlist es visible públicamente';
