-- Add Management Permissions for Contacts and Department/Group
-- Run this script to add permissions for Management tabs

-- Insert management permissions
INSERT IGNORE INTO `permissions` (`name`, `description`, `resource`, `action`, `is_active`, `created_at`, `updated_at`) VALUES
  -- Contacts Management (in Admin page)
  ('management.contact.view', 'View contacts in Management', 'management', 'contact.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.contact.create', 'Create contacts in Management', 'management', 'contact.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.contact.update', 'Update contacts in Management', 'management', 'contact.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.contact.delete', 'Delete contacts in Management', 'management', 'contact.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.contact.export', 'Export contacts from Management', 'management', 'contact.export', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Department/Group Management
  ('management.department.view', 'View departments in Management', 'management', 'department.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.department.create', 'Create departments in Management', 'management', 'department.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.department.update', 'Update departments in Management', 'management', 'department.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.department.delete', 'Delete departments in Management', 'management', 'department.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Group Contact Mapping Management
  ('management.group-contact.view', 'View group contacts in Management', 'management', 'group-contact.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.group-contact.create', 'Create group contacts in Management', 'management', 'group-contact.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.group-contact.update', 'Update group contacts in Management', 'management', 'group-contact.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.group-contact.delete', 'Delete group contacts in Management', 'management', 'group-contact.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Designation/Position Management
  ('management.designation.view', 'View designations in Management', 'management', 'designation.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.designation.create', 'Create designations in Management', 'management', 'designation.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.designation.update', 'Update designations in Management', 'management', 'designation.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.designation.delete', 'Delete designations in Management', 'management', 'designation.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permission Management
  ('management.permission.view', 'View permissions in Management', 'management', 'permission.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.permission.create', 'Create permissions in Management', 'management', 'permission.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.permission.update', 'Update permissions in Management', 'management', 'permission.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('management.permission.delete', 'Delete permissions in Management', 'management', 'permission.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

-- Verify the permissions were added
SELECT 
  p.id,
  p.name,
  p.description,
  p.resource,
  p.action,
  p.is_active,
  COUNT(rp.role_id) as assigned_to_roles
FROM permissions p
LEFT JOIN role_permissions rp ON p.id = rp.permission_id
WHERE p.resource = 'management'
GROUP BY p.id, p.name, p.description, p.resource, p.action, p.is_active
ORDER BY p.action, p.id;

