[
  {
    "type": "create_table",
    "description": "Create task_job_definitions table for job configuration",
    "database": "kiosk_buykiosk",
    "check_query": "SHOW TABLES LIKE 'task_job_definitions'",
    "sql": "CREATE TABLE `task_job_definitions` (\n  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,\n  `name` varchar(100) NOT NULL COMMENT 'Unique identifier e.g. event-phase-processor',\n  `displayName` varchar(200) NOT NULL COMMENT 'Human-readable name',\n  `className` varchar(255) NOT NULL COMMENT 'Informational FQCN (validated against JobRegistry)',\n  `schedule` varchar(100) DEFAULT NULL COMMENT 'Cron expression, NULL = manual only',\n  `queue` varchar(50) NOT NULL DEFAULT 'default' COMMENT 'Queue name',\n  `scope` enum('global','per_store') NOT NULL DEFAULT 'global',\n  `timeout` int(10) unsigned NOT NULL DEFAULT 300 COMMENT 'Max execution seconds',\n  `maxRetries` int(10) unsigned NOT NULL DEFAULT 3,\n  `retryBackoff` int(10) unsigned NOT NULL DEFAULT 60 COMMENT 'Base seconds for exponential backoff',\n  `config` json DEFAULT NULL COMMENT 'Job-specific configuration',\n  `isEnabled` tinyint(1) NOT NULL DEFAULT 1,\n  `notifyOnFailure` tinyint(1) NOT NULL DEFAULT 1,\n  `notifyEmails` varchar(500) DEFAULT NULL COMMENT 'Comma-separated emails',\n  `created_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `uk_name` (`name`),\n  KEY `idx_schedule` (`isEnabled`, `schedule`),\n  KEY `idx_queue` (`queue`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Task Engine job definitions'"
  },
  {
    "type": "create_table",
    "description": "Create task_executions table for execution history",
    "database": "kiosk_buykiosk",
    "check_query": "SHOW TABLES LIKE 'task_executions'",
    "sql": "CREATE TABLE `task_executions` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n  `jobDefinitionId` int(10) unsigned NOT NULL COMMENT 'FK to task_job_definitions',\n  `typeNum` varchar(10) DEFAULT NULL COMMENT 'Store context (NULL for global)',\n  `status` enum('pending','running','completed','failed','cancelled') NOT NULL DEFAULT 'pending',\n  `triggerType` enum('scheduled','manual','retry') NOT NULL DEFAULT 'scheduled',\n  `triggeredBy` int(10) unsigned DEFAULT NULL COMMENT 'User ID if manual',\n  `workerId` varchar(50) DEFAULT NULL COMMENT 'Worker that processed',\n  `attempt` int(10) unsigned NOT NULL DEFAULT 1,\n  `idempotencyKey` varchar(255) DEFAULT NULL COMMENT 'Stable key for dedupe/at-least-once safety',\n  `payload` json DEFAULT NULL COMMENT 'Input data',\n  `result` json DEFAULT NULL COMMENT 'Output/error data',\n  `progress` tinyint(3) unsigned NOT NULL DEFAULT 0 COMMENT '0-100',\n  `errorMessage` text DEFAULT NULL,\n  `errorTrace` text DEFAULT NULL,\n  `scheduled_fire_at` datetime DEFAULT NULL COMMENT 'Scheduled fire time (UTC)',\n  `queued_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `started_at` datetime DEFAULT NULL,\n  `completed_at` datetime DEFAULT NULL,\n  `durationMs` int(10) unsigned DEFAULT NULL,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `idx_idempotency` (`jobDefinitionId`, `idempotencyKey`),\n  KEY `idx_scheduled_fire` (`jobDefinitionId`, `scheduled_fire_at`),\n  KEY `idx_job_status` (`jobDefinitionId`, `status`),\n  KEY `idx_store` (`typeNum`, `status`),\n  KEY `idx_queued` (`status`, `queued_at`),\n  KEY `idx_worker` (`workerId`, `status`),\n  CONSTRAINT `fk_execution_job` FOREIGN KEY (`jobDefinitionId`) REFERENCES `task_job_definitions` (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Task Engine execution history'"
  },
  {
    "type": "create_table",
    "description": "Create task_execution_logs table for per-execution logs",
    "database": "kiosk_buykiosk",
    "check_query": "SHOW TABLES LIKE 'task_execution_logs'",
    "sql": "CREATE TABLE `task_execution_logs` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,\n  `executionId` bigint(20) unsigned NOT NULL COMMENT 'FK to task_executions',\n  `level` enum('debug','info','warning','error') NOT NULL DEFAULT 'info',\n  `message` text NOT NULL,\n  `context` json DEFAULT NULL,\n  `logged_at` datetime(3) NOT NULL DEFAULT current_timestamp(3),\n  PRIMARY KEY (`id`),\n  KEY `idx_execution` (`executionId`),\n  CONSTRAINT `fk_log_execution` FOREIGN KEY (`executionId`) REFERENCES `task_executions` (`id`) ON DELETE CASCADE\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Task Engine per-execution logs'"
  },
  {
    "type": "create_table",
    "description": "Create task_workers table for worker registration",
    "database": "kiosk_buykiosk",
    "check_query": "SHOW TABLES LIKE 'task_workers'",
    "sql": "CREATE TABLE `task_workers` (\n  `id` varchar(50) NOT NULL COMMENT 'UUID',\n  `hostname` varchar(100) NOT NULL,\n  `pid` int(10) unsigned NOT NULL,\n  `status` enum('idle','busy','stopped') NOT NULL DEFAULT 'idle',\n  `currentExecutionId` bigint(20) unsigned DEFAULT NULL,\n  `jobsProcessed` int(10) unsigned NOT NULL DEFAULT 0,\n  `jobsFailed` int(10) unsigned NOT NULL DEFAULT 0,\n  `started_at` datetime NOT NULL DEFAULT current_timestamp(),\n  `last_heartbeat` datetime NOT NULL DEFAULT current_timestamp(),\n  PRIMARY KEY (`id`),\n  KEY `idx_status` (`status`, `last_heartbeat`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Task Engine worker registration'"
  }
]
