


CREATE TABLE IF NOT EXISTS gallery_items (
  id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  gallery_post_id BIGINT UNSIGNED NOT NULL,
  media_id BIGINT UNSIGNED NULL,
  kind ENUM('photo','video') NOT NULL DEFAULT 'photo',
  video_url VARCHAR(500) NULL,
  caption VARCHAR(255) NULL,
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_gallery (gallery_post_id),
  INDEX idx_kind (kind),
  CONSTRAINT fk_gi_post FOREIGN KEY (gallery_post_id) REFERENCES posts(id) ON DELETE CASCADE,
  CONSTRAINT fk_gi_media FOREIGN KEY (media_id) REFERENCES media(id) ON DELETE SET NULL
) ENGINE=InnoDB;