-- Migration 056: Add issues:widget permission
--
-- Adds the widget permission for the Issue Quick Add dashboard widget.
-- dashboard_admin gets it automatically via the permission cache (loadCache
-- populates it with every permission in the system).

-- 1. Insert the new widget permission
INSERT INTO permissions (id, feature_group_id, label, description, sort_order)
VALUES ('issues:widget', 'issues', 'Widget', 'Issue-Schnelleingabe auf dem Dashboard', 8)
ON CONFLICT (id) DO NOTHING;

-- 2. Grant issues:widget to every group that already has issues:create
INSERT INTO group_permissions (authentik_group, permission_id)
  SELECT authentik_group, 'issues:widget'
  FROM group_permissions
  WHERE permission_id = 'issues:create'
ON CONFLICT DO NOTHING;

-- 3. Add dependency: issues:widget requires issues:create
UPDATE app_settings
SET value = value || '{"issues:widget": ["issues:create"]}'::jsonb
WHERE key = 'permission_deps';
