CREATE TABLE IF NOT EXISTS `wpjy_app_reminders` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_contrato` BIGINT UNSIGNED NULL,
  `context_type` VARCHAR(40) NOT NULL,
  `context_id` BIGINT UNSIGNED NOT NULL,
  `target_user_id` BIGINT UNSIGNED NOT NULL,
  `related_user_id` BIGINT UNSIGNED NULL,
  `area` VARCHAR(20) NOT NULL DEFAULT 'adm',
  `title` VARCHAR(160) NOT NULL,
  `description` TEXT NULL,
  `remind_at` DATETIME NOT NULL,
  `channels_json` JSON NULL,
  `status` VARCHAR(20) NOT NULL DEFAULT 'open',
  `notification_id` BIGINT UNSIGNED NULL,
  `notified_at` DATETIME NULL,
  `completed_at` DATETIME NULL,
  `cancelled_at` DATETIME NULL,
  `created_by` BIGINT UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_app_reminders_context` (`context_type`, `context_id`),
  KEY `idx_app_reminders_target` (`target_user_id`, `status`, `remind_at`),
  KEY `idx_app_reminders_due` (`status`, `notified_at`, `remind_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
