-- Add Location Management CRUD Permissions
-- Run this script to add permissions for Countries, Divisions, Districts, and Thanas management

-- Insert location management CRUD permissions
INSERT IGNORE INTO `permissions` (`name`, `description`, `resource`, `action`, `is_active`, `created_at`, `updated_at`) VALUES
  -- Countries CRUD
  ('country.create', 'Create countries', 'location', 'country.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('country.read', 'View countries', 'location', 'country.read', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('country.update', 'Update countries', 'location', 'country.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('country.delete', 'Delete countries', 'location', 'country.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Divisions CRUD
  ('division.create', 'Create divisions', 'location', 'division.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('division.read', 'View divisions', 'location', 'division.read', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('division.update', 'Update divisions', 'location', 'division.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('division.delete', 'Delete divisions', 'location', 'division.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Districts CRUD
  ('district.create', 'Create districts', 'location', 'district.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('district.read', 'View districts', 'location', 'district.read', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('district.update', 'Update districts', 'location', 'district.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('district.delete', 'Delete districts', 'location', 'district.delete', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  
  -- Thanas CRUD
  ('thana.create', 'Create thanas', 'location', 'thana.create', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('thana.read', 'View thanas', 'location', 'thana.read', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('thana.update', 'Update thanas', 'location', 'thana.update', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
  ('thana.delete', 'Delete thanas', 'location', 'thana.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 = 'location'
GROUP BY p.id, p.name, p.description, p.resource, p.action, p.is_active
ORDER BY p.action, p.id;

