-- Migration 063: Add uvcCameraTab and rename cameraTab → phoneCameraTab in all policy tables
-- The Android app now has two independent camera tabs:
--   phoneCameraTab (Camera2/phone camera)
--   uvcCameraTab   (USB/UVC external camera)
-- The legacy cameraTab rows are renamed to phoneCameraTab for continuity.

-- ─── Rename legacy cameraTab → phoneCameraTab ───────────────────────────────

UPDATE app_tabs         SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';
UPDATE ftt_app_tabs     SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';
UPDATE ftt_app_feature_policy SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';
UPDATE app_feature_policy     SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';
UPDATE ftt_tab_section_policy SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';
UPDATE ftt_tab_feature_toggles SET tab_id = 'phoneCameraTab' WHERE tab_id = 'cameraTab';

-- ─── Register phoneCameraTab (INSERT IGNORE — may already exist after UPDATE) ─

INSERT IGNORE INTO app_tabs (tab_id, label, icon, sort_order, content_type, content_slug, requires_token, created_at, updated_at)
VALUES ('phoneCameraTab', 'Phone Camera', '📱', 5, 'native', NULL, 0, NOW(), NOW());

INSERT IGNORE INTO ftt_app_tabs (tab_id, label, icon, sort_order, content_type, content_slug, requires_token, created_at, updated_at)
VALUES ('phoneCameraTab', 'Phone Camera', '📱', 5, 'native', NULL, 0, NOW(), NOW());

INSERT IGNORE INTO app_feature_policy (tab_id, visibility, min_tier, allow_override_for_developer_admin)
VALUES ('phoneCameraTab', 'show', 'trial', 1);

INSERT IGNORE INTO ftt_app_feature_policy (tab_id, visibility, min_tier, allow_override_for_developer_admin)
VALUES ('phoneCameraTab', 'show', 'trial', 1);

INSERT IGNORE INTO app_admin_config (config_key, config_value)
VALUES ('phone_camera_enabled', '1');

INSERT IGNORE INTO ftt_app_admin_config (config_key, config_value)
VALUES ('phone_camera_enabled', '1');

-- ─── Register uvcCameraTab ────────────────────────────────────────────────────

INSERT IGNORE INTO app_tabs (tab_id, label, icon, sort_order, content_type, content_slug, requires_token, created_at, updated_at)
VALUES ('uvcCameraTab', 'UVC Camera', '📷', 6, 'native', NULL, 0, NOW(), NOW());

INSERT IGNORE INTO ftt_app_tabs (tab_id, label, icon, sort_order, content_type, content_slug, requires_token, created_at, updated_at)
VALUES ('uvcCameraTab', 'UVC Camera', '📷', 6, 'native', NULL, 0, NOW(), NOW());

INSERT IGNORE INTO app_feature_policy (tab_id, visibility, min_tier, allow_override_for_developer_admin)
VALUES ('uvcCameraTab', 'show', 'trial', 1);

INSERT IGNORE INTO ftt_app_feature_policy (tab_id, visibility, min_tier, allow_override_for_developer_admin)
VALUES ('uvcCameraTab', 'show', 'trial', 1);

INSERT IGNORE INTO app_admin_config (config_key, config_value)
VALUES ('uvc_camera_enabled', '1');

INSERT IGNORE INTO ftt_app_admin_config (config_key, config_value)
VALUES ('uvc_camera_enabled', '1');

-- ─── Seed section policies for phoneCameraTab ─────────────────────────────────

INSERT IGNORE INTO ftt_tab_section_policy (tab_id, section_id, section_label, visibility, min_tier) VALUES
  ('phoneCameraTab', 'phoneCameraPreview',  'Camera Preview',   'show', 'trial'),
  ('phoneCameraTab', 'phoneCameraCapture',  'Capture Controls', 'show', 'trial'),
  ('phoneCameraTab', 'phoneCameraSettings', 'Camera Settings',  'show', 'trial'),
  ('phoneCameraTab', 'phoneCapturedMedia',  'Captured Media',   'show', 'trial');

-- ─── Seed section policies for uvcCameraTab ──────────────────────────────────

INSERT IGNORE INTO ftt_tab_section_policy (tab_id, section_id, section_label, visibility, min_tier) VALUES
  ('uvcCameraTab', 'uvcPreview',    'UVC Preview',    'show', 'trial'),
  ('uvcCameraTab', 'uvcCapture',    'Capture Controls','show', 'trial'),
  ('uvcCameraTab', 'uvcSettings',   'UVC Settings',   'show', 'trial'),
  ('uvcCameraTab', 'uvcRecordings', 'Recordings',     'show', 'trial');

-- master_feature_toggles is not present on this server — skipped.
