-- RBAC permission catalog
-- Import once into itimiumc_mgt_cms.
-- Safe to run repeatedly.

START TRANSACTION;

CREATE TEMPORARY TABLE tmp_rbac_permissions (
 module_name VARCHAR(100) NOT NULL,
 permission_code VARCHAR(150) NOT NULL,
 permission_name VARCHAR(150) NOT NULL,
 description VARCHAR(255) NULL
);

INSERT INTO tmp_rbac_permissions
(module_name,permission_code,permission_name,description)
VALUES
('dashboard','dashboard.view','View Dashboard','Access the assigned dashboard'),('students','students.view','View Students','View student records'),('students','students.create','Create Students','Add student records'),('students','students.update','Update Students','Edit student records'),('students','students.delete','Delete Students','Remove student records'),('guardians','guardians.view','View Guardians','View parents and guardians'),('guardians','guardians.manage','Manage Guardians','Add and update guardians'),('staff','staff.view','View Staff','View staff records'),('staff','staff.create','Create Staff','Add staff records'),('staff','staff.update','Update Staff','Edit staff records'),('staff','staff.delete','Delete Staff','Remove staff records'),('users','users.view','View Users','View school users'),('users','users.create','Create Users','Create branch users'),('users','users.update','Update Users','Edit user accounts'),('users','users.delete','Delete Users','Remove user accounts'),('roles','roles.manage','Manage Roles','Create roles and assign permissions'),('academics','academics.view','View Academics','View classes, sections and subjects'),('academics','academics.manage','Manage Academics','Manage academic structure'),('timetable','timetable.view','View Timetable','View timetable entries'),('timetable','timetable.manage','Manage Timetable','Create and update timetables'),('attendance','attendance.view','View Attendance','View attendance records'),('attendance','attendance.manage','Manage Attendance','Record and update attendance'),('exams','exams.view','View Exams','View exams and results'),('exams','exams.manage','Manage Exams','Create exams and publish results'),('fees','fees.view','View Fees','View invoices and payments'),('fees','fees.manage','Manage Fees','Create invoices and receive payments'),('accounting','accounting.view','View Accounting','View journals and ledgers'),('accounting','accounting.manage','Manage Accounting','Post accounting transactions'),('payroll','payroll.view','View Payroll','View payroll records'),('payroll','payroll.manage','Manage Payroll','Process payroll'),('library','library.view','View Library','View library records'),('library','library.manage','Manage Library','Manage books and circulation'),('transport','transport.view','View Transport','View routes and vehicles'),('transport','transport.manage','Manage Transport','Manage routes and assignments'),('hostel','hostel.view','View Hostel','View hostel records'),('hostel','hostel.manage','Manage Hostel','Manage rooms and allocations'),('inventory','inventory.view','View Inventory','View inventory records'),('inventory','inventory.manage','Manage Inventory','Manage stock and procurement'),('admissions','admissions.view','View Admissions','View admission applications'),('admissions','admissions.manage','Manage Admissions','Process admissions and leads'),('homework','homework.view','View Homework','View homework and submissions'),('homework','homework.manage','Manage Homework','Create and assess homework'),('lms','lms.view','View LMS','View learning content'),('lms','lms.manage','Manage LMS','Manage courses and content'),('communication','communication.view','View Communication','View notices and messages'),('communication','communication.manage','Manage Communication','Send notices and messages'),('reports','reports.view','View Reports','Access school reports'),('audit','audit.view','View Audit Logs','View activity and security logs'),('settings','settings.view','View Settings','View school settings'),('settings','settings.manage','Manage Settings','Update school settings');

INSERT INTO permissions
(school_id,branch_id,user_id,module_name,permission_code,
 permission_name,description)
SELECT 0,0,0,t.module_name,t.permission_code,
 t.permission_name,t.description
FROM tmp_rbac_permissions t
WHERE NOT EXISTS(
 SELECT 1 FROM permissions p
 WHERE p.permission_code=t.permission_code
);

UPDATE permissions p
JOIN tmp_rbac_permissions t
 ON t.permission_code=p.permission_code
SET p.module_name=t.module_name,
    p.permission_name=t.permission_name,
    p.description=t.description
WHERE p.school_id=0;

DROP TEMPORARY TABLE tmp_rbac_permissions;

COMMIT;

SELECT module_name,permission_code,permission_name
FROM permissions
WHERE school_id=0
ORDER BY module_name,permission_name;
