-- SAKEN Platform Phase 4.3 Static Sections No Payroll
-- Run once in Cloudflare D1 Console (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'))
);

-- Remove Payroll static permission from this no-payroll baseline
DELETE FROM permissions WHERE section_key = 'payroll_dashboard_v31';

-- Admin access to static sections and platform tools
INSERT INTO permissions (user_id, section_key, can_view, can_refresh, can_manage) VALUES
  (1, 'business_profitability', 1, 0, 0),
  (1, 'other_revenue_dashboard', 1, 0, 0),
  (1, 'restaurant_results_dashboard', 1, 0, 0),
  (1, 'section_builder', 1, 0, 1),
  (1, 'settings', 1, 0, 1),
  (1, 'users', 1, 0, 1)
ON CONFLICT(user_id, section_key)
DO UPDATE SET
  can_view = excluded.can_view,
  can_refresh = excluded.can_refresh,
  can_manage = excluded.can_manage;

SELECT * FROM permissions
WHERE section_key IN ('business_profitability','other_revenue_dashboard','restaurant_results_dashboard','section_builder','settings','users')
ORDER BY section_key;
