-- Add Granular Contact Location Field Permissions
-- Run this script to add permissions for individual location fields (Country, Division, District, Thana)
-- for Present, Office, and Permanent Address sections

-- Insert contact location field permissions
INSERT IGNORE INTO `permissions` (`name`, `description`, `resource`, `action`, `is_active`, `created_at`, `updated_at`) VALUES
  -- Present Address - Country Permissions
  ('contact.address.present.country.view', 'View Present Address Country field', 'contact', 'address.present.country.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.present.country.edit', 'Edit Present Address Country field', 'contact', 'address.present.country.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Present Address - Division Permissions
  ('contact.address.present.division.view', 'View Present Address Division field', 'contact', 'address.present.division.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.present.division.edit', 'Edit Present Address Division field', 'contact', 'address.present.division.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Present Address - District Permissions
  ('contact.address.present.district.view', 'View Present Address District field', 'contact', 'address.present.district.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.present.district.edit', 'Edit Present Address District field', 'contact', 'address.present.district.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Present Address - Thana Permissions
  ('contact.address.present.thana.view', 'View Present Address Thana field', 'contact', 'address.present.thana.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.present.thana.edit', 'Edit Present Address Thana field', 'contact', 'address.present.thana.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Office Address - Country Permissions
  ('contact.address.office.country.view', 'View Office Address Country field', 'contact', 'address.office.country.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.office.country.edit', 'Edit Office Address Country field', 'contact', 'address.office.country.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Office Address - Division Permissions
  ('contact.address.office.division.view', 'View Office Address Division field', 'contact', 'address.office.division.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.office.division.edit', 'Edit Office Address Division field', 'contact', 'address.office.division.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Office Address - District Permissions
  ('contact.address.office.district.view', 'View Office Address District field', 'contact', 'address.office.district.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.office.district.edit', 'Edit Office Address District field', 'contact', 'address.office.district.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Office Address - Thana Permissions
  ('contact.address.office.thana.view', 'View Office Address Thana field', 'contact', 'address.office.thana.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.office.thana.edit', 'Edit Office Address Thana field', 'contact', 'address.office.thana.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permanent Address - Country Permissions
  ('contact.address.permanent.country.view', 'View Permanent Address Country field', 'contact', 'address.permanent.country.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.permanent.country.edit', 'Edit Permanent Address Country field', 'contact', 'address.permanent.country.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permanent Address - Division Permissions
  ('contact.address.permanent.division.view', 'View Permanent Address Division field', 'contact', 'address.permanent.division.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.permanent.division.edit', 'Edit Permanent Address Division field', 'contact', 'address.permanent.division.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permanent Address - District Permissions
  ('contact.address.permanent.district.view', 'View Permanent Address District field', 'contact', 'address.permanent.district.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.permanent.district.edit', 'Edit Permanent Address District field', 'contact', 'address.permanent.district.edit', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Permanent Address - Thana Permissions
  ('contact.address.permanent.thana.view', 'View Permanent Address Thana field', 'contact', 'address.permanent.thana.view', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('contact.address.permanent.thana.edit', 'Edit Permanent Address Thana field', 'contact', 'address.permanent.thana.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.action, p.id;

