-- =====================================================
-- TABLE: education.modules
-- =====================================================
-- Proyecto: OrbiQuant IA (Trading Platform)
-- Módulo: OQI-002 - Education
-- Especificación: ET-EDU-001-database.md
-- =====================================================

CREATE TABLE education.modules (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Relación con curso
  course_id UUID NOT NULL REFERENCES education.courses(id) ON DELETE CASCADE,

  -- Información básica
  title VARCHAR(200) NOT NULL,
  description TEXT,

  -- Ordenamiento
  display_order INTEGER NOT NULL DEFAULT 0,

  -- Metadata
  duration_minutes INTEGER,

  -- Control de acceso
  is_locked BOOLEAN DEFAULT false, -- Requiere completar módulos anteriores
  unlock_after_module_id UUID REFERENCES education.modules(id) ON DELETE SET NULL,

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

  CONSTRAINT unique_course_order UNIQUE(course_id, display_order)
);

-- Índices
CREATE INDEX idx_modules_course ON education.modules(course_id);
CREATE INDEX idx_modules_order ON education.modules(course_id, display_order);

-- Comentarios
COMMENT ON TABLE education.modules IS 'Módulos que agrupan lecciones dentro de un curso';
COMMENT ON COLUMN education.modules.is_locked IS 'Si requiere completar módulos anteriores para desbloquearse';
COMMENT ON COLUMN education.modules.unlock_after_module_id IS 'Módulo que debe completarse antes de acceder a este';
