-- Migration: Add permissions for Staff Contributions module
-- Purpose: Add new permissions and assign to Admin role
-- Date: 2026-02-16
-- Idempotent: safe to run more than once (INSERT IGNORE)

-- Add permissions (schema: code UNIQUE)
INSERT IGNORE INTO permissions (code, description) VALUES
('view_staff_contributions', 'View staff sales contribution reports'),
('view_own_contribution', 'View own sales contribution stats'),
('manage_staff_contributions', 'Manage and calculate staff contributions'),
('manage_bonus_pool', 'Manage bonus pools and distribution');

-- Assign to Admin role (role_id 1); PK (role_id, permission_id) prevents duplicates
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT 1, id FROM permissions WHERE code IN (
    'view_staff_contributions',
    'view_own_contribution',
    'manage_staff_contributions',
    'manage_bonus_pool'
);
