-- Fase 1: expiração + estatísticas diárias
ALTER TABLE products
  ADD COLUMN IF NOT EXISTS expires_at DATE NULL AFTER views;
ALTER TABLE products ADD INDEX IF NOT EXISTS idx_products_expires (expires_at);

CREATE TABLE IF NOT EXISTS product_views_daily (
  product_id INT NOT NULL,
  day DATE NOT NULL,
  views INT NOT NULL DEFAULT 0,
  PRIMARY KEY (product_id, day),
  INDEX idx_pvd_day (day),
  FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS product_leads_daily (
  product_id INT NOT NULL,
  day DATE NOT NULL,
  leads INT NOT NULL DEFAULT 0,
  PRIMARY KEY (product_id, day),
  INDEX idx_pld_day (day),
  FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
) ENGINE=InnoDB;
