INSERT INTO roles (name, display_name) VALUES
('SUPER_ADMIN', 'Super Administrator'),
('ADMIN', 'Administrator'),
('THERAPIST', 'Therapist')
ON DUPLICATE KEY UPDATE display_name = VALUES(display_name);

INSERT INTO permissions (name, display_name) VALUES
('users.view', 'View users'),
('users.create', 'Create users'),
('users.edit', 'Edit users'),
('users.disable', 'Disable users'),
('patients.view', 'View patients'),
('patients.create', 'Create patients'),
('patients.edit', 'Edit patients'),
('patients.assign', 'Assign patients'),
('sessions.view', 'View sessions'),
('sessions.create', 'Create sessions'),
('sessions.edit', 'Edit sessions'),
('assessments.create', 'Create assessments'),
('assessments.edit', 'Edit assessments'),
('assessments.submit', 'Submit assessments'),
('reports.create', 'Create reports'),
('reports.edit', 'Edit reports'),
('reports.submit', 'Submit reports'),
('reports.approve', 'Approve reports'),
('reports.generate', 'Generate official reports'),
('billing.view', 'View billing'),
('invoice.create', 'Create invoices'),
('invoice.view', 'View invoices'),
('payment.create', 'Record payments'),
('receipt.generate', 'Generate receipts'),
('settings.manage', 'Manage settings'),
('audit.view', 'View audit logs')
ON DUPLICATE KEY UPDATE display_name = VALUES(display_name);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p
WHERE r.name = 'SUPER_ADMIN'
  AND p.name IN (
    'users.view', 'users.create', 'users.edit', 'users.disable',
    'settings.manage', 'audit.view'
  );

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p
WHERE r.name = 'ADMIN';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p
WHERE r.name = 'THERAPIST'
  AND p.name IN (
    'patients.view',
    'sessions.view', 'sessions.create', 'sessions.edit',
    'assessments.create', 'assessments.edit', 'assessments.submit',
    'reports.create', 'reports.edit', 'reports.submit'
  );

INSERT INTO settings (setting_key, setting_value, setting_type) VALUES
('business.country', 'Kenya', 'string'),
('business.currency_name', 'Kenyan Shilling', 'string'),
('business.currency_code', 'KES', 'string'),
('business.currency_symbol', 'KSh', 'string'),
('regional.timezone', 'Africa/Nairobi', 'string'),
('regional.language', 'English', 'string'),
('regional.date_format', 'DD/MM/YYYY', 'string'),
('regional.time_format', '12-hour', 'string'),
('regional.decimal_places', '2', 'integer'),
('tax.enabled', '0', 'boolean'),
('numbering.patient.prefix', 'PT-', 'string'),
('numbering.assessment.prefix', 'ASS-', 'string'),
('numbering.report.prefix', 'RPT-', 'string'),
('numbering.invoice.prefix', 'INV-', 'string'),
('numbering.receipt.prefix', 'RCT-', 'string'),
('numbering.quotation.prefix', 'QT-', 'string')
ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value), setting_type = VALUES(setting_type);

INSERT INTO numbering_sequences (sequence_key, prefix, next_number, padding) VALUES
('patient', 'PT-', 1, 6),
('assessment', 'ASS-', 1, 6),
('report', 'RPT-', 1, 6),
('invoice', 'INV-', 1, 6),
('receipt', 'RCT-', 1, 6),
('quotation', 'QT-', 1, 6)
ON DUPLICATE KEY UPDATE prefix = VALUES(prefix), padding = VALUES(padding);
