-- Tabla principal de landings
CREATE TABLE IF NOT EXISTS landing_pages (
  id            INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  titulo        VARCHAR(200) NOT NULL,
  slug          VARCHAR(160) NOT NULL UNIQUE,
  estado        ENUM('borrador','publicado') NOT NULL DEFAULT 'borrador',
  hero_image    VARCHAR(255) NULL,
  contenido_html MEDIUMTEXT NULL,      -- HTML renderizable
  css_inline    MEDIUMTEXT NULL,       -- CSS opcional por landing
  js_inline     MEDIUMTEXT NULL,       -- JS opcional por landing
  meta_title    VARCHAR(200) NULL,
  meta_desc     VARCHAR(255) NULL,
  canonical_url VARCHAR(255) NULL,
  created_at    TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updated_at    TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Captura de leads del formulario de la landing
CREATE TABLE IF NOT EXISTS landing_forms (
  id           BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  landing_id   INT UNSIGNED NOT NULL,
  nombre       VARCHAR(150) NULL,
  email        VARCHAR(180) NULL,
  telefono     VARCHAR(60) NULL,
  mensaje      TEXT NULL,
  ip_address   VARCHAR(64) NULL,
  user_agent   TEXT NULL,
  created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX (landing_id),
  CONSTRAINT fk_lf_lp FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

-- 1. Bloques reutilizables (biblioteca)
CREATE TABLE IF NOT EXISTS landing_blocks (
  id            INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  nombre        VARCHAR(160) NOT NULL,
  tipo          ENUM('html','css','js') NOT NULL DEFAULT 'html',
  contenido     MEDIUMTEXT NOT NULL,
  updated_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  created_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_nombre (nombre)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. Asignación de bloques a una landing (con orden)
CREATE TABLE IF NOT EXISTS landing_page_blocks (
  id            INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  landing_id    INT UNSIGNED NOT NULL,
  block_id      INT UNSIGNED NOT NULL,
  posicion      INT NOT NULL DEFAULT 1,     -- orden
  zona          VARCHAR(40) NOT NULL DEFAULT 'main',  -- main/header/footer/etc
  created_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_lp_block (landing_id, block_id, zona),
  KEY idx_landing_pos (landing_id, posicion),
  CONSTRAINT fk_lpb_lp  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  CONSTRAINT fk_lpb_blk FOREIGN KEY (block_id)  REFERENCES landing_blocks(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. Variantes A/B de una landing
CREATE TABLE IF NOT EXISTS landing_variants (
  id              INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  landing_id      INT UNSIGNED NOT NULL,
  nombre          VARCHAR(120) NOT NULL,
  peso            DECIMAL(6,3) NOT NULL DEFAULT 1.000,  -- tráfico relativo
  estado          ENUM('borrador','activo','pausado') NOT NULL DEFAULT 'activo',
  -- contenido específico de la variante (opcional); si NULL, toma el de landing_pages
  hero_image      VARCHAR(255) NULL,
  contenido_html  MEDIUMTEXT NULL,
  css_inline      MEDIUMTEXT NULL,
  js_inline       MEDIUMTEXT NULL,
  created_at      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at      TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  KEY idx_lv_landing (landing_id, estado),
  CONSTRAINT fk_lv_lp FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 4. Programación por fecha (ventanas de publicación)
CREATE TABLE IF NOT EXISTS landing_schedule (
  id           INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  landing_id   INT UNSIGNED NOT NULL,
  start_at     DATETIME NULL,
  end_at       DATETIME NULL,
  estado       ENUM('programada','activa','finalizada') NOT NULL DEFAULT 'programada',
  created_at   TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_sched (landing_id, start_at, end_at),
  CONSTRAINT fk_ls_lp FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 5. Tracking: impresiones y conversiones por variante
CREATE TABLE IF NOT EXISTS landing_variant_stats (
  id            BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  landing_id    INT UNSIGNED NOT NULL,
  variant_id    INT UNSIGNED NULL,      -- NULL = página base (sin variante)
  event_type    ENUM('impression','conversion') NOT NULL,
  session_id    VARCHAR(64) NULL,
  ip_address    VARCHAR(64) NULL,
  user_agent    TEXT NULL,
  created_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  KEY idx_lv_evt (landing_id, variant_id, event_type, created_at),
  CONSTRAINT fk_lvs_lp FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  CONSTRAINT fk_lvs_lv FOREIGN KEY (variant_id) REFERENCES landing_variants(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

---

## Estructura de Base de Datos

```sql
-- Landings principales
CREATE TABLE landing_pages (
  id INT AUTO_INCREMENT PRIMARY KEY,
  titulo VARCHAR(255) NOT NULL,
  slug VARCHAR(255) NOT NULL UNIQUE,
  descripcion TEXT,
  activo TINYINT(1) DEFAULT 1,
  fecha_creacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Bloques reutilizables
CREATE TABLE landing_blocks (
  id INT AUTO_INCREMENT PRIMARY KEY,
  nombre VARCHAR(255) NOT NULL,
  tipo ENUM('texto','html','imagen','video') DEFAULT 'texto',
  contenido TEXT,
  fecha_creacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Relación landing <-> bloques
CREATE TABLE landing_page_blocks (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  block_id INT NOT NULL,
  zona VARCHAR(40) DEFAULT 'main',
  posicion INT DEFAULT 1,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  FOREIGN KEY (block_id) REFERENCES landing_blocks(id) ON DELETE CASCADE
);

-- Variantes para A/B testing
CREATE TABLE landing_variants (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  nombre VARCHAR(255),
  porcentaje_traffic DECIMAL(5,2) DEFAULT 50.00,
  activo TINYINT(1) DEFAULT 1,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
);

-- Programación de publicaciones
CREATE TABLE landing_schedule (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  variant_id INT DEFAULT NULL,
  fecha_inicio DATETIME,
  fecha_fin DATETIME,
  activo TINYINT(1) DEFAULT 1,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  FOREIGN KEY (variant_id) REFERENCES landing_variants(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS landing_pages (
  id INT AUTO_INCREMENT PRIMARY KEY,
  titulo VARCHAR(255) NOT NULL,
  slug VARCHAR(255) NOT NULL UNIQUE,
  descripcion TEXT,
  activo TINYINT(1) DEFAULT 1,
  fecha_creacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS landing_variants (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  nombre VARCHAR(255),
  porcentaje_traffic DECIMAL(5,2) DEFAULT 50.00,
  activo TINYINT(1) DEFAULT 1,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS landing_blocks (
  id INT AUTO_INCREMENT PRIMARY KEY,
  nombre VARCHAR(255) NOT NULL,
  tipo ENUM('texto','html','imagen','video') DEFAULT 'texto',
  contenido TEXT,
  fecha_creacion TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS landing_page_blocks (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  block_id INT NOT NULL,
  zona VARCHAR(40) DEFAULT 'main',
  posicion INT DEFAULT 1,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  FOREIGN KEY (block_id) REFERENCES landing_blocks(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS landing_forms (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  nombre VARCHAR(200),
  email VARCHAR(200),
  telefono VARCHAR(50),
  mensaje TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  ip_address VARCHAR(100),
  user_agent TEXT,
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS landing_variant_stats (
  id INT AUTO_INCREMENT PRIMARY KEY,
  landing_id INT NOT NULL,
  variant_id INT NULL,
  event_type ENUM('impression','conversion') NOT NULL,
  session_id VARCHAR(100) NULL,
  ip_address VARCHAR(100) NULL,
  user_agent TEXT NULL,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX (landing_id),
  INDEX (variant_id),
  INDEX (event_type),
  INDEX (created_at),
  FOREIGN KEY (landing_id) REFERENCES landing_pages(id) ON DELETE CASCADE,
  FOREIGN KEY (variant_id) REFERENCES landing_variants(id) ON DELETE SET NULL
);


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


