-- language_module_seed.sql
-- 1) Register language module routes if missing
INSERT INTO mk_sys_routes (path, method, module_code, action)
VALUES
  ('/api/language/translations', 'GET', 'language', 'update'),
  ('/api/language/translations/{id}', 'PUT', 'language', 'update'),
  ('/api/language/translations/upsert-missing', 'POST', 'language', 'update'),
  ('/api/language/translations/sync-sources', 'POST', 'language', 'update'),
  ('/api/language/translations/translate-selected', 'POST', 'language', 'update'),
  ('/api/language/translations/bulk-approve', 'POST', 'language', 'update'),
  ('/api/language/translations/bulk-update', 'POST', 'language', 'update')
ON CONFLICT (path, method) DO UPDATE
SET
  module_code = EXCLUDED.module_code,
  action = EXCLUDED.action;

-- 2) Remove legacy system translation routes (optional cleanup)
DELETE FROM mk_sys_routes
WHERE path LIKE '/api/system/translations%';

-- 3) Seed role permissions for language module by cloning system perms
INSERT INTO mk_sys_role_permissions (role_id, module_code, action, allowed)
SELECT rp.role_id, 'language', rp.action, rp.allowed
FROM mk_sys_role_permissions rp
WHERE rp.module_code = 'system'
  AND rp.action IN ('view', 'read', 'insert', 'update', 'delete', 'export')
ON CONFLICT DO NOTHING;

-- 4) Ensure admin update access
INSERT INTO mk_sys_role_permissions (role_id, module_code, action, allowed)
SELECT r.id, 'language', 'update', true
FROM dfrole r
WHERE r.id = 3
ON CONFLICT DO NOTHING;

-- 5) Seed role+department permissions for language module by cloning system perms
INSERT INTO mk_sys_role_department_permissions
  (role_id, department_code, module_code, action, allowed)
SELECT DISTINCT
  rdp.role_id,
  rdp.department_code,
  'language',
  rdp.action,
  rdp.allowed
FROM mk_sys_role_department_permissions rdp
WHERE rdp.module_code = 'system'
  AND rdp.action IN ('view', 'read', 'insert', 'update', 'delete', 'export')
ON CONFLICT DO NOTHING;
