-- Add Contact Location Field Permissions
-- Run this script to add permissions for Present, Office, and Permanent Address location fields

-- Insert contact location field permissions
INSERT IGNORE INTO `permissions` (`name`, `description`, `resource`, `action`, `is_active`, `created_at`, `updated_at`) VALUES
  -- Present Address Location Permissions
  ('contact.address.present.view', 'View Present Address location fields', 'contact', 'address.present.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.present.edit', 'Edit Present Address location fields', 'contact', 'address.present.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Office Address Location Permissions
  ('contact.address.office.view', 'View Office Address location fields', 'contact', 'address.office.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.office.edit', 'Edit Office Address location fields', 'contact', 'address.office.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permanent Address Location Permissions
  ('contact.address.permanent.view', 'View Permanent Address location fields', 'contact', 'address.permanent.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.permanent.edit', 'Edit Permanent Address location fields', 'contact', 'address.permanent.edit', 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 = 'contact' AND p.action LIKE 'address.%'
GROUP BY p.id, p.name, p.description, p.resource, p.action, p.is_active
ORDER BY p.id;

