SET @outbox_archive_cutoff_ms := IFNULL(
  @outbox_archive_cutoff_ms,
  UNIX_TIMESTAMP(DATE_SUB(UTC_TIMESTAMP(3), INTERVAL 30 DAY)) * 1000
);

CREATE TABLE IF NOT EXISTS hyapp_wallet.wallet_outbox_archive LIKE hyapp_wallet.wallet_outbox;
INSERT IGNORE INTO hyapp_wallet.wallet_outbox_archive
SELECT * FROM hyapp_wallet.wallet_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_wallet.wallet_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_wallet.wallet_outbox_archive a
    WHERE a.app_code = wallet_outbox.app_code AND a.event_id = wallet_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_room.room_outbox_archive LIKE hyapp_room.room_outbox;
INSERT IGNORE INTO hyapp_room.room_outbox_archive
SELECT * FROM hyapp_room.room_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_room.room_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_room.room_outbox_archive a
    WHERE a.app_code = room_outbox.app_code AND a.event_id = room_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_activity.activity_outbox_archive LIKE hyapp_activity.activity_outbox;
INSERT IGNORE INTO hyapp_activity.activity_outbox_archive
SELECT * FROM hyapp_activity.activity_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_activity.activity_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_activity.activity_outbox_archive a
    WHERE a.app_code = activity_outbox.app_code AND a.outbox_id = activity_outbox.outbox_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_activity.im_broadcast_outbox_archive LIKE hyapp_activity.im_broadcast_outbox;
INSERT IGNORE INTO hyapp_activity.im_broadcast_outbox_archive
SELECT * FROM hyapp_activity.im_broadcast_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_activity.im_broadcast_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_activity.im_broadcast_outbox_archive a
    WHERE a.app_code = im_broadcast_outbox.app_code AND a.event_id = im_broadcast_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_game.game_outbox_archive LIKE hyapp_game.game_outbox;
INSERT IGNORE INTO hyapp_game.game_outbox_archive
SELECT * FROM hyapp_game.game_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_game.game_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_game.game_outbox_archive a
    WHERE a.app_code = game_outbox.app_code AND a.event_id = game_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_game.game_level_event_outbox_archive LIKE hyapp_game.game_level_event_outbox;
INSERT IGNORE INTO hyapp_game.game_level_event_outbox_archive
SELECT * FROM hyapp_game.game_level_event_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_game.game_level_event_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_game.game_level_event_outbox_archive a
    WHERE a.app_code = game_level_event_outbox.app_code AND a.event_id = game_level_event_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_user.user_outbox_archive LIKE hyapp_user.user_outbox;
INSERT IGNORE INTO hyapp_user.user_outbox_archive
SELECT * FROM hyapp_user.user_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_user.user_outbox
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_user.user_outbox_archive a
    WHERE a.app_code = user_outbox.app_code AND a.event_id = user_outbox.event_id
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;

CREATE TABLE IF NOT EXISTS hyapp_notice.notice_delivery_events_archive LIKE hyapp_notice.notice_delivery_events;
INSERT IGNORE INTO hyapp_notice.notice_delivery_events_archive
SELECT * FROM hyapp_notice.notice_delivery_events
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
ORDER BY updated_at_ms ASC
LIMIT 10000;
DELETE FROM hyapp_notice.notice_delivery_events
WHERE status IN ('delivered', 'done') AND updated_at_ms < @outbox_archive_cutoff_ms
  AND EXISTS (
    SELECT 1 FROM hyapp_notice.notice_delivery_events_archive a
    WHERE a.source_name = notice_delivery_events.source_name
      AND a.app_code = notice_delivery_events.app_code
      AND a.source_event_id = notice_delivery_events.source_event_id
      AND a.channel = notice_delivery_events.channel
  )
ORDER BY updated_at_ms ASC
LIMIT 10000;
