-- Consolidated schema (fresh install)
-- Includes all current tables, columns, indexes, and seed data.

CREATE TABLE IF NOT EXISTS `users` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` CHAR(36) NOT NULL,
  `first_name` VARCHAR(100) NOT NULL,
  `last_name` VARCHAR(100) NOT NULL,
  `display_name` VARCHAR(210) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `profile_description` TEXT NULL,
  `job_title` VARCHAR(160) NULL,
  `phone` VARCHAR(50) NULL,
  `mobile` VARCHAR(50) NULL,
  `short_dial` VARCHAR(20) NULL,
  `address` VARCHAR(255) NULL,
  `postal_code` VARCHAR(20) NULL,
  `city` VARCHAR(100) NULL,
  `country` VARCHAR(100) NULL,
  `region` VARCHAR(100) NULL,
  `hire_date` DATE NULL,
  `email_verified_at` DATETIME NULL DEFAULT NULL,
  `last_login_at` DATETIME NULL DEFAULT NULL,
  `password` VARCHAR(255) NOT NULL,
  `locale` VARCHAR(10) DEFAULT NULL,
  `totp_secret` VARCHAR(255) DEFAULT NULL,
  `theme` VARCHAR(10) NOT NULL DEFAULT 'light',
  `primary_tenant_id` INT NULL,
  `current_tenant_id` INT UNSIGNED NULL,
  `created_by` INT UNSIGNED DEFAULT NULL,
  `modified_by` INT UNSIGNED DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `active_changed_at` DATETIME NULL DEFAULT NULL,
  `active_changed_by` INT UNSIGNED DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_users_uuid` (`uuid`),
  UNIQUE KEY `uniq_users_email` (`email`),
  KEY `idx_users_primary_tenant_id` (`primary_tenant_id`),
  KEY `idx_users_current_tenant_id` (`current_tenant_id`),
  KEY `idx_users_created_by` (`created_by`),
  KEY `idx_users_modified_by` (`modified_by`),
  KEY `idx_users_active_changed_by` (`active_changed_by`),
  CONSTRAINT `fk_users_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_users_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_users_active_changed_by` FOREIGN KEY (`active_changed_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_users_current_tenant` FOREIGN KEY (`current_tenant_id`) REFERENCES `tenants` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `tenants` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` CHAR(36) NOT NULL,
  `description` VARCHAR(255) NOT NULL,
  `address` VARCHAR(255) NULL,
  `postal_code` VARCHAR(20) NULL,
  `city` VARCHAR(100) NULL,
  `country` VARCHAR(100) NULL,
  `region` VARCHAR(100) NULL,
  `vat_id` VARCHAR(50) NULL,
  `tax_number` VARCHAR(50) NULL,
  `phone` VARCHAR(50) NULL,
  `fax` VARCHAR(100) NULL,
  `email` VARCHAR(190) NULL,
  `support_email` VARCHAR(190) NULL,
  `support_phone` VARCHAR(50) NULL,
  `billing_email` VARCHAR(190) NULL,
  `website` VARCHAR(255) NULL,
  `privacy_url` VARCHAR(255) NULL,
  `imprint_url` VARCHAR(255) NULL,
  `primary_color` VARCHAR(7) NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'active',
  `status_changed_at` DATETIME NULL DEFAULT NULL,
  `status_changed_by` INT UNSIGNED DEFAULT NULL,
  `created_by` INT UNSIGNED DEFAULT NULL,
  `modified_by` INT UNSIGNED DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_tenants_uuid` (`uuid`),
  KEY `idx_tenants_created_by` (`created_by`),
  KEY `idx_tenants_modified_by` (`modified_by`),
  KEY `idx_tenants_status_changed_by` (`status_changed_by`),
  CONSTRAINT `fk_tenants_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_tenants_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_tenants_status_changed_by` FOREIGN KEY (`status_changed_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `roles` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` CHAR(36) NOT NULL,
  `description` VARCHAR(255) NOT NULL,
  `code` VARCHAR(50) DEFAULT NULL,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_by` INT UNSIGNED DEFAULT NULL,
  `modified_by` INT UNSIGNED DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_roles_uuid` (`uuid`),
  UNIQUE KEY `uniq_roles_code` (`code`),
  KEY `idx_roles_active` (`active`),
  KEY `idx_roles_created_by` (`created_by`),
  KEY `idx_roles_modified_by` (`modified_by`),
  CONSTRAINT `fk_roles_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_roles_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `permissions` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `key` VARCHAR(191) NOT NULL,
  `description` VARCHAR(255) DEFAULT NULL,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `is_system` TINYINT(1) NOT NULL DEFAULT 0,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_permissions_key` (`key`),
  KEY `idx_permissions_active` (`active`),
  KEY `idx_permissions_system` (`is_system`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `departments` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` CHAR(36) NOT NULL,
  `description` VARCHAR(255) NOT NULL,
  `code` VARCHAR(50) DEFAULT NULL,
  `cost_center` VARCHAR(50) DEFAULT NULL,
  `active` TINYINT(1) NOT NULL DEFAULT 1,
  `created_by` INT UNSIGNED DEFAULT NULL,
  `modified_by` INT UNSIGNED DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_departments_uuid` (`uuid`),
  KEY `idx_departments_active` (`active`),
  KEY `idx_departments_created_by` (`created_by`),
  KEY `idx_departments_modified_by` (`modified_by`),
  CONSTRAINT `fk_departments_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_departments_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `pages` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `uuid` CHAR(36) NOT NULL,
  `slug` VARCHAR(190) NOT NULL,
  `created_by` INT UNSIGNED NULL,
  `modified_by` INT UNSIGNED NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_pages_uuid` (`uuid`),
  UNIQUE KEY `uniq_pages_slug` (`slug`),
  KEY `idx_pages_created_by` (`created_by`),
  KEY `idx_pages_modified_by` (`modified_by`),
  CONSTRAINT `fk_pages_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_pages_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `page_contents` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `page_id` INT UNSIGNED NOT NULL,
  `locale` VARCHAR(10) NOT NULL,
  `content` LONGTEXT NULL,
  `created_by` INT UNSIGNED NULL,
  `modified_by` INT UNSIGNED NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `modified` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_page_locale` (`page_id`, `locale`),
  KEY `idx_page_id` (`page_id`),
  KEY `idx_page_contents_created_by` (`created_by`),
  KEY `idx_page_contents_modified_by` (`modified_by`),
  CONSTRAINT `fk_page_contents_page` FOREIGN KEY (`page_id`) REFERENCES `pages` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_page_contents_created_by` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL,
  CONSTRAINT `fk_page_contents_modified_by` FOREIGN KEY (`modified_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `settings` (
  `key` VARCHAR(64) NOT NULL,
  `value` TEXT NULL,
  `description` VARCHAR(255) NULL,
  `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

CREATE TABLE IF NOT EXISTS `user_tenants` (
  `user_id` INT UNSIGNED NOT NULL,
  `tenant_id` INT UNSIGNED NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`, `tenant_id`),
  KEY `idx_user_tenants_user` (`user_id`),
  KEY `idx_user_tenants_tenant` (`tenant_id`),
  CONSTRAINT `fk_user_tenants_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_user_tenants_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `user_roles` (
  `user_id` INT UNSIGNED NOT NULL,
  `role_id` INT UNSIGNED NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`, `role_id`),
  KEY `idx_user_roles_user` (`user_id`),
  KEY `idx_user_roles_role` (`role_id`),
  CONSTRAINT `fk_user_roles_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_user_roles_role` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `role_permissions` (
  `role_id` INT UNSIGNED NOT NULL,
  `permission_id` INT UNSIGNED NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`role_id`, `permission_id`),
  KEY `idx_role_permissions_permission` (`permission_id`),
  CONSTRAINT `fk_role_permissions_role` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_role_permissions_permission` FOREIGN KEY (`permission_id`) REFERENCES `permissions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `user_departments` (
  `user_id` INT UNSIGNED NOT NULL,
  `department_id` INT UNSIGNED NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`user_id`, `department_id`),
  KEY `idx_user_departments_user` (`user_id`),
  KEY `idx_user_departments_department` (`department_id`),
  CONSTRAINT `fk_user_departments_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_user_departments_department` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `tenant_departments` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `tenant_id` INT UNSIGNED NOT NULL,
  `department_id` INT UNSIGNED NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_tenant_department` (`tenant_id`, `department_id`),
  KEY `idx_tenant_departments_tenant` (`tenant_id`),
  KEY `idx_tenant_departments_department` (`department_id`),
  CONSTRAINT `fk_tenant_departments_tenant` FOREIGN KEY (`tenant_id`) REFERENCES `tenants` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_tenant_departments_department` FOREIGN KEY (`department_id`) REFERENCES `departments` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `mail_log` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `to_email` VARCHAR(255) NOT NULL,
  `subject` VARCHAR(255) NOT NULL,
  `template` VARCHAR(100) NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'queued',
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `sent_at` DATETIME NULL DEFAULT NULL,
  `error_message` TEXT NULL,
  `provider_message_id` VARCHAR(255) NULL,
  PRIMARY KEY (`id`),
  KEY `idx_mail_log_status` (`status`),
  KEY `idx_mail_log_to_email` (`to_email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `password_resets` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INT UNSIGNED NOT NULL,
  `code_hash` VARCHAR(255) NOT NULL,
  `expires_at` DATETIME NOT NULL,
  `attempts` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Failed verification attempts',
  `used_at` DATETIME NULL DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_password_resets_user` (`user_id`),
  KEY `idx_password_resets_expires` (`expires_at`),
  CONSTRAINT `fk_password_resets_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `email_verifications` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INT UNSIGNED NOT NULL,
  `code_hash` VARCHAR(255) NOT NULL,
  `expires_at` DATETIME NOT NULL,
  `attempts` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Failed verification attempts',
  `used_at` DATETIME NULL DEFAULT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_email_verifications_user` (`user_id`),
  KEY `idx_email_verifications_expires` (`expires_at`),
  CONSTRAINT `fk_email_verifications_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `user_remember_tokens` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INT UNSIGNED NOT NULL,
  `selector` CHAR(24) NOT NULL,
  `token_hash` CHAR(64) NOT NULL,
  `expires_at` DATETIME NOT NULL,
  `expired_by_admin_at` DATETIME NULL DEFAULT NULL COMMENT 'Expired by admin action',
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_used` DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_user_remember_selector` (`selector`),
  KEY `idx_user_remember_user` (`user_id`),
  KEY `idx_user_remember_expires` (`expires_at`),
  CONSTRAINT `fk_user_remember_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `tenants` (`uuid`, `description`, `created`)
SELECT UUID(), 'Cronus', NOW()
WHERE NOT EXISTS (SELECT 1 FROM tenants WHERE description = 'Cronus');

INSERT INTO `departments` (`uuid`, `description`, `code`, `active`, `created`)
SELECT UUID(), 'IT', 'IT', 1, NOW()
WHERE NOT EXISTS (SELECT 1 FROM departments WHERE description = 'IT');

INSERT INTO `roles` (`uuid`, `description`, `code`, `active`, `created`)
SELECT UUID(), 'Admin', 'ADMIN', 1, NOW()
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE description = 'Admin');

INSERT INTO `users` (
  `uuid`,
  `first_name`,
  `last_name`,
  `display_name`,
  `email`,
  `password`,
  `locale`,
  `totp_secret`,
  `theme`,
  `primary_tenant_id`,
  `created_by`,
  `created`,
  `active`,
  `active_changed_at`,
  `active_changed_by`
)
SELECT
  UUID(),
  'Detlef',
  'Demo',
  'Detlef Demo',
  'demo@user.com',
  '$2y$12$KVCQvuy4Pl1aySBuzSpc7ehpZhAzYZkndDI9OaMi05E2P/Mhob5HO',
  'de',
  NULL,
  'light',
  (SELECT id FROM tenants WHERE description = 'Cronus' LIMIT 1),
  NULL,
  NOW(),
  1,
  NOW(),
  NULL
WHERE NOT EXISTS (SELECT 1 FROM users WHERE email = 'demo@user.com');

INSERT INTO `user_tenants` (`user_id`, `tenant_id`, `created`)
SELECT u.id, t.id, NOW()
FROM users u
JOIN tenants t ON t.description = 'Cronus'
WHERE u.email = 'demo@user.com'
  AND NOT EXISTS (
    SELECT 1 FROM user_tenants ut WHERE ut.user_id = u.id AND ut.tenant_id = t.id
  );

INSERT INTO `user_departments` (`user_id`, `department_id`, `created`)
SELECT u.id, d.id, NOW()
FROM users u
JOIN departments d ON d.description = 'IT'
WHERE u.email = 'demo@user.com'
  AND NOT EXISTS (
    SELECT 1 FROM user_departments ud WHERE ud.user_id = u.id AND ud.department_id = d.id
  );

INSERT INTO `tenant_departments` (`tenant_id`, `department_id`, `created`)
SELECT t.id, d.id, NOW()
FROM tenants t
JOIN departments d ON d.description = 'IT'
WHERE t.description = 'Cronus'
  AND NOT EXISTS (
    SELECT 1 FROM tenant_departments td WHERE td.tenant_id = t.id AND td.department_id = d.id
  );

INSERT INTO `user_roles` (`user_id`, `role_id`, `created`)
SELECT u.id, r.id, NOW()
FROM users u
JOIN roles r ON r.description = 'Admin'
WHERE u.email = 'demo@user.com'
  AND NOT EXISTS (
    SELECT 1 FROM user_roles ur WHERE ur.user_id = u.id AND ur.role_id = r.id
  );

INSERT INTO `pages` (`uuid`, `slug`, `created`)
SELECT UUID(), 'impressum', NOW()
WHERE NOT EXISTS (SELECT 1 FROM pages WHERE slug = 'impressum');

INSERT INTO `page_contents` (`page_id`, `locale`, `content`, `created`)
SELECT p.id, 'de', NULL, p.created
FROM pages p
WHERE NOT EXISTS (
  SELECT 1 FROM page_contents pc WHERE pc.page_id = p.id AND pc.locale = 'de'
);

INSERT INTO `permissions` (`key`, `description`, `active`, `is_system`)
VALUES
  ('users.view', 'Can view users', 1, 1),
  ('users.view_meta', 'Can view user status and meta', 1, 1),
  ('users.view_audit', 'Can view user audit log', 1, 1),
  ('users.create', 'Can create users', 1, 1),
  ('users.update', 'Can update users', 1, 1),
  ('users.delete', 'Can delete users', 1, 1),
  ('users.self_update', 'Can update own user', 1, 1),
  ('users.update_assignments', 'Can update user assignments', 1, 1),
  ('address_book.view', 'Can view address book', 1, 1),
  ('tenants.view', 'Can view tenants', 1, 1),
  ('tenants.create', 'Can create tenants', 1, 1),
  ('tenants.update', 'Can update tenants', 1, 1),
  ('tenants.delete', 'Can delete tenants', 1, 1),
  ('departments.view', 'Can view departments', 1, 1),
  ('departments.create', 'Can create departments', 1, 1),
  ('departments.update', 'Can update departments', 1, 1),
  ('departments.delete', 'Can delete departments', 1, 1),
  ('roles.view', 'Can view roles', 1, 1),
  ('roles.create', 'Can create roles', 1, 1),
  ('roles.update', 'Can update roles', 1, 1),
  ('roles.delete', 'Can delete roles', 1, 1),
  ('permissions.view', 'Can view permissions', 1, 1),
  ('permissions.create', 'Can create permissions', 1, 1),
  ('permissions.update', 'Can update permissions', 1, 1),
  ('permissions.delete', 'Can delete permissions', 1, 1),
  ('settings.view', 'Can view settings', 1, 1),
  ('settings.update', 'Can update settings', 1, 1),
  ('mail_log.view', 'Can view mail logs', 1, 1),
  ('stats.view', 'Can view statistics', 1, 1)
ON DUPLICATE KEY UPDATE
  `description` = VALUES(`description`),
  `active` = VALUES(`active`),
  `is_system` = VALUES(`is_system`);

INSERT INTO `role_permissions` (`role_id`, `permission_id`, `created`)
SELECT r.id, p.id, NOW()
FROM roles r
JOIN permissions p ON p.`key` IN (
  'users.view', 'users.view_meta', 'users.view_audit', 'users.create', 'users.update', 'users.delete',
  'users.self_update', 'users.update_assignments',
  'address_book.view',
  'tenants.view', 'tenants.create', 'tenants.update', 'tenants.delete',
  'departments.view', 'departments.create', 'departments.update', 'departments.delete',
  'roles.view', 'roles.create', 'roles.update', 'roles.delete',
  'permissions.view', 'permissions.create', 'permissions.update', 'permissions.delete',
  'settings.view', 'settings.update',
  'mail_log.view',
  'stats.view'
)
WHERE r.description IN ('Admin', 'Administrator') OR r.id = 1
ON DUPLICATE KEY UPDATE role_id = role_id;
