-- =============================================================================
-- DWAPA POS - platform (control-plane) database: complete one-time setup
--
-- Creates the database `dwapaDB`, all 17 tables, records the 7 schema
-- migrations as applied, and creates the first super-admin login.
--
-- Requires: MySQL 8.0 or newer (not MariaDB). The MySQL user running this
-- script needs the CREATE privilege.
-- Safe to run more than once: nothing is dropped, overwritten or duplicated.
--
-- Organisation (tenant) databases are NOT in this file. The server creates
-- and migrates one per organisation by itself when a super admin adds an
-- organisation in the app. For that, the DB user in the server's .env must
-- be allowed to CREATE DATABASE.
--
-- If you change the database name below, set PLATFORM_DB_NAME to match.
-- =============================================================================

CREATE DATABASE IF NOT EXISTS `dwapaDB` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `dwapaDB`;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;


/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `api_keys` (
  `id` char(36) NOT NULL,
  `name` varchar(120) NOT NULL,
  `prefix` varchar(20) NOT NULL,
  `key_hash` varchar(255) NOT NULL,
  `scopes` json DEFAULT NULL,
  `last_used_at` datetime(3) DEFAULT NULL,
  `revoked` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_api_keys_prefix` (`prefix`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Platform API keys';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `entitlements` (
  `id` char(36) NOT NULL,
  `organisation_id` char(36) NOT NULL,
  `device_id` char(36) DEFAULT NULL,
  `issued_at` datetime(3) NOT NULL,
  `expires_at` datetime(3) NOT NULL,
  `token_hash` varchar(255) NOT NULL,
  `revoked` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `idx_entitlements_expires_at` (`expires_at`),
  KEY `fk_entitlements_organisation_id` (`organisation_id`),
  CONSTRAINT `fk_entitlements_organisation_id` FOREIGN KEY (`organisation_id`) REFERENCES `organisations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Short-lived offline entitlement tokens issued per sync';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `migrations` (
  `id` int NOT NULL AUTO_INCREMENT,
  `timestamp` bigint NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `organisations` (
  `id` char(36) NOT NULL,
  `name` varchar(160) NOT NULL,
  `slug` varchar(80) NOT NULL,
  `owner_email` varchar(160) NOT NULL,
  `country` varchar(60) NOT NULL DEFAULT 'Ghana',
  `database_name` varchar(80) NOT NULL,
  `plan_id` char(36) DEFAULT NULL,
  `status` enum('provisioning','trial','active','suspended','churned','deactivated') NOT NULL DEFAULT 'provisioning',
  `mrr_minor` bigint NOT NULL DEFAULT '0',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_organisations_slug` (`slug`),
  UNIQUE KEY `uq_organisations_database_name` (`database_name`),
  KEY `fk_organisations_plan_id` (`plan_id`),
  CONSTRAINT `fk_organisations_plan_id` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Tenant registry — one row per merchant business';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `plans` (
  `id` char(36) NOT NULL,
  `name` varchar(80) NOT NULL,
  `code` varchar(40) NOT NULL,
  `price_minor` bigint NOT NULL,
  `currency` char(3) NOT NULL DEFAULT 'GHS',
  `billing_interval` enum('month','year') NOT NULL DEFAULT 'month',
  `features` json DEFAULT NULL,
  `is_active` tinyint NOT NULL DEFAULT '1',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_plans_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Subscription plans (codes: starter, growth, business, enterprise)';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_admin_preferences` (
  `id` char(36) NOT NULL,
  `platform_user_id` char(36) NOT NULL,
  `notification_preferences` json NOT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_platform_admin_preferences_user` (`platform_user_id`),
  CONSTRAINT `fk_platform_admin_preferences_user` FOREIGN KEY (`platform_user_id`) REFERENCES `platform_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Per-admin platform preferences';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_audit_logs` (
  `id` char(36) NOT NULL,
  `platform_user_id` char(36) DEFAULT NULL,
  `actor` varchar(160) DEFAULT NULL,
  `action` varchar(80) NOT NULL,
  `target` varchar(160) DEFAULT NULL,
  `category` enum('organisation','subscription','user','system','billing') DEFAULT NULL,
  `organisation_id` char(36) DEFAULT NULL,
  `detail` json DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `fk_platform_audit_logs_platform_user_id` (`platform_user_id`),
  CONSTRAINT `fk_platform_audit_logs_platform_user_id` FOREIGN KEY (`platform_user_id`) REFERENCES `platform_users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Platform activity feed / audit trail';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_notifications` (
  `id` char(36) NOT NULL,
  `event_key` varchar(160) NOT NULL,
  `title` varchar(160) NOT NULL,
  `description` text NOT NULL,
  `type` enum('info','success','warning','error') NOT NULL,
  `target_path` varchar(255) DEFAULT NULL,
  `read_at` datetime(3) DEFAULT NULL,
  `dismissed_at` datetime(3) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_platform_notifications_event_key` (`event_key`),
  KEY `idx_platform_notifications_dismissed_created` (`dismissed_at`,`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Platform admin notification inbox';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_sessions` (
  `id` char(36) NOT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `user_id` char(36) NOT NULL,
  `token_hash` char(64) NOT NULL,
  `expires_at` datetime(3) NOT NULL,
  `revoked_at` datetime(3) DEFAULT NULL,
  `replaced_by` char(36) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_platform_sessions_user` (`user_id`,`revoked_at`),
  CONSTRAINT `fk_platform_sessions_user` FOREIGN KEY (`user_id`) REFERENCES `platform_users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_settings` (
  `setting_key` varchar(100) NOT NULL,
  `setting_value` json NOT NULL,
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`setting_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Platform-wide configuration values';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `platform_users` (
  `id` char(36) NOT NULL,
  `name` varchar(120) NOT NULL,
  `email` varchar(160) NOT NULL,
  `password_hash` varchar(255) NOT NULL,
  `role` enum('super_admin','support','billing') NOT NULL DEFAULT 'support',
  `is_active` tinyint NOT NULL DEFAULT '1',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_platform_users_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='SleekTeq staff who administer the multi-tenant platform';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `push_subscriptions` (
  `id` char(36) NOT NULL,
  `plane` enum('platform','tenant') NOT NULL,
  `user_id` char(36) NOT NULL,
  `org_id` char(36) DEFAULT NULL,
  `role` varchar(40) DEFAULT NULL,
  `endpoint_hash` char(64) NOT NULL,
  `endpoint` text NOT NULL,
  `p256dh` varchar(255) NOT NULL,
  `auth` varchar(255) NOT NULL,
  `user_agent` varchar(255) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `uq_push_subscriptions_endpoint` (`endpoint_hash`),
  KEY `idx_push_subscriptions_user` (`plane`,`user_id`),
  KEY `idx_push_subscriptions_org` (`org_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Web Push subscriptions (one per browser/device)';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `subscription_payments` (
  `id` char(36) NOT NULL,
  `subscription_id` char(36) NOT NULL,
  `amount_minor` bigint NOT NULL,
  `currency` char(3) NOT NULL DEFAULT 'GHS',
  `status` enum('pending','paid','failed') NOT NULL DEFAULT 'pending',
  `provider_ref` varchar(120) DEFAULT NULL,
  `paid_at` datetime(3) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `fk_subscription_payments_subscription_id` (`subscription_id`),
  CONSTRAINT `fk_subscription_payments_subscription_id` FOREIGN KEY (`subscription_id`) REFERENCES `subscriptions` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Billing history for a subscription';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `subscriptions` (
  `id` char(36) NOT NULL,
  `organisation_id` char(36) NOT NULL,
  `plan_id` char(36) NOT NULL,
  `status` enum('trialing','active','past_due','grace','expired','cancelled') NOT NULL DEFAULT 'active',
  `amount_minor` bigint NOT NULL DEFAULT '0',
  `interval` enum('month','year') NOT NULL DEFAULT 'month',
  `current_period_start` datetime(3) NOT NULL,
  `current_period_end` datetime(3) NOT NULL,
  `grace_until` datetime(3) DEFAULT NULL,
  `auto_renew` tinyint(1) NOT NULL DEFAULT '1',
  `provider` enum('paystack','manual') NOT NULL DEFAULT 'paystack',
  `provider_ref` varchar(120) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `fk_subscriptions_organisation_id` (`organisation_id`),
  KEY `fk_subscriptions_plan_id` (`plan_id`),
  CONSTRAINT `fk_subscriptions_organisation_id` FOREIGN KEY (`organisation_id`) REFERENCES `organisations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_subscriptions_plan_id` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Active subscription per organisation';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `support_tickets` (
  `id` char(36) NOT NULL,
  `subject` varchar(200) NOT NULL,
  `organisation_id` char(36) DEFAULT NULL,
  `requester_name` varchar(160) NOT NULL,
  `requester_email` varchar(160) DEFAULT NULL,
  `category` varchar(40) DEFAULT NULL,
  `requester_user_id` char(36) DEFAULT NULL,
  `requester_role` varchar(40) DEFAULT NULL,
  `priority` enum('low','medium','high','urgent') NOT NULL DEFAULT 'medium',
  `status` enum('open','in_progress','resolved','closed') NOT NULL DEFAULT 'open',
  `last_reply_at` datetime(3) DEFAULT NULL,
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `updated_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `idx_support_tickets_last_reply_at` (`last_reply_at`),
  KEY `fk_support_tickets_organisation_id` (`organisation_id`),
  KEY `idx_support_tickets_requester` (`organisation_id`,`requester_user_id`),
  CONSTRAINT `fk_support_tickets_organisation_id` FOREIGN KEY (`organisation_id`) REFERENCES `organisations` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Platform support tickets';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `tenant_admin_delegations` (
  `id` char(36) NOT NULL,
  `organisation_id` char(36) NOT NULL,
  `platform_user_id` char(36) NOT NULL,
  `reason` varchar(255) DEFAULT NULL,
  `granted_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  `revoked_at` datetime(3) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_tenant_admin_delegations_organisation_id` (`organisation_id`),
  KEY `fk_tenant_admin_delegations_platform_user_id` (`platform_user_id`),
  CONSTRAINT `fk_tenant_admin_delegations_organisation_id` FOREIGN KEY (`organisation_id`) REFERENCES `organisations` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_tenant_admin_delegations_platform_user_id` FOREIGN KEY (`platform_user_id`) REFERENCES `platform_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Support impersonation / delegated admin grants';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE IF NOT EXISTS `ticket_replies` (
  `id` char(36) NOT NULL,
  `ticket_id` char(36) NOT NULL,
  `author` varchar(160) NOT NULL,
  `body` text NOT NULL,
  `is_staff` tinyint(1) NOT NULL DEFAULT '0',
  `created_at` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
  PRIMARY KEY (`id`),
  KEY `fk_ticket_replies_ticket_id` (`ticket_id`),
  CONSTRAINT `fk_ticket_replies_ticket_id` FOREIGN KEY (`ticket_id`) REFERENCES `support_tickets` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Replies on a support ticket';
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;


-- Record the schema migrations so the server and `npm run migrate` treat this
-- database as up to date. Skips any that are already recorded.
INSERT INTO `migrations` (`timestamp`, `name`)
SELECT v.ts, v.nm FROM (SELECT 1759490000000 AS ts, 'PlatformBaseline1759490000000' AS nm UNION ALL SELECT 1759490000001 AS ts, 'PlatformSessions1759490000001' AS nm UNION ALL SELECT 1759490000002 AS ts, 'AlignPlatformFlags1759490000002' AS nm UNION ALL SELECT 1760000000000 AS ts, 'PlatformNotifications1760000000000' AS nm UNION ALL SELECT 1760000000001 AS ts, 'PlatformAdminSettings1760000000001' AS nm UNION ALL SELECT 1760000000002 AS ts, 'PushSubscriptions1760000000002' AS nm UNION ALL SELECT 1760000000003 AS ts, 'TicketReporterContext1760000000003' AS nm) AS v
WHERE NOT EXISTS (SELECT 1 FROM `migrations` m WHERE m.`name` = v.nm);

-- First super-admin login (email below; password is supplied separately).
-- Leave "Organisation slug" blank when logging in. Change the password after
-- the first login. Skipped if this email already exists.
INSERT INTO `platform_users` (`id`, `name`, `email`, `password_hash`, `role`, `is_active`, `created_at`, `updated_at`)
SELECT UUID(), 'Super Admin', 'admin@sleekteq.com', '$2a$10$/9M2fo8B1u8IbFP2/WHvCOF2Tm2fmyB9weor31NfjTS/w7vM6TL0W', 'super_admin', 1, NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM `platform_users` WHERE `email` = 'admin@sleekteq.com');
