-- SAKEN Platform Phase 4 - Custom HTML Sections
-- Run this once in Cloudflare D1 Console for saken_platform_db.

CREATE TABLE IF NOT EXISTS custom_sections (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  section_key TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL,
  tag TEXT DEFAULT 'HTML',
  description TEXT DEFAULT '',
  html_content TEXT NOT NULL,
  active INTEGER NOT NULL DEFAULT 1,
  sort_order INTEGER NOT NULL DEFAULT 100,
  created_by INTEGER,
  created_at TEXT DEFAULT (datetime('now')),
  updated_at TEXT DEFAULT (datetime('now'))
);

-- Give the original Admin user permission to use the upload section builder.
INSERT INTO permissions (user_id, section_key, can_view, can_refresh, can_manage)
VALUES (1, 'section_builder', 1, 0, 1)
ON CONFLICT(user_id, section_key)
DO UPDATE SET can_view = 1, can_manage = 1;

SELECT 'Phase 4 custom_sections table ready' AS status;
