# (c) cavaliba.com - builtin pipelines

# --------------------------------------------------------------
# bulk_disable_out_of_sync
#
# Disable assets whose last_sync field is older than 3 months.
#
# Logic:
#   - default _action is noop (safety: no change unless stale)
#   - compute cutoff = now - 3 months
#   - discard entries with no last_sync field (not applicable)
#   - discard entries whose last_sync is recent (not stale)
#   - remaining entries are stale: set _action to disable
#
# Note: discarded entries are NOT written to the database.
# Only stale entries are updated.
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_disable_out_of_sync
  displayname: Bulk Disable Out-of-Sync
  run_permission: p_pipeline_run
  description: Disable assets whose last_sync field is older than 3 months. Assets without last_sync or recently synced are left untouched (discarded).
  content: |
      tasks:
          # default: noop (safe fallback - no change unless stale)
          - field_set: ['', _action, noop]

          # compute cutoff date: now minus 3 months
          - field_datetime: ['', _cutoff, 'now()', '-3month']

          # check if last_sync field exists
          - set_condition: [HAS_SYNC, exists, last_sync]

          # check if last_sync is older than cutoff (stale)
          - set_condition: [IS_STALE, lt, last_sync, _cutoff]

          # discard entries with no last_sync (leave them untouched in DB)
          - discard: ['!HAS_SYNC']

          # discard entries that are in sync (leave them untouched in DB)
          - discard: ['!IS_STALE']

          # stale entries: mark for disable
          - field_set: ['', _action, disable]

          # clean up temporary field
          - field_delete: ['', _cutoff]


# --------------------------------------------------------------
# bulk_delete_out_of_sync
#
# Delete assets that are already disabled AND whose last_sync
# field is older than 6 months.
#
# Logic:
#   - default _action is noop (safety: no change unless criteria met)
#   - compute cutoff = now - 6 months
#   - discard entries with no last_sync field (not applicable)
#   - discard entries that are still enabled (not yet disabled)
#   - discard entries whose last_sync is recent (not stale enough)
#   - remaining entries are disabled AND stale: set _action to delete
#
# Note: discarded entries are NOT written to the database.
# Only disabled+stale entries are deleted.
#
# Typical workflow:
#   1. Apply bulk_disable_out_of_sync  (disable after 3 months)
#   2. Apply bulk_delete_out_of_sync   (delete after 6 months)
#
# Usage (apply to a schema):
#   schema.apply_pipeline('bulk_delete_out_of_sync')
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_delete_out_of_sync
  displayname: Bulk Delete Disabled and Out-of-Sync 
  run_permission: p_pipeline_run
  description: Delete assets that are already disabled and whose last_sync is older than 6 months. Assets that are enabled, have no last_sync, or were synced recently are left untouched (discarded).
  content: |
      tasks:
          # default: noop (safe fallback - no change unless criteria met)
          - field_set: ['', _action, noop]

          # compute cutoff date: now minus 6 months
          - field_datetime: ['', _cutoff, 'now()', '-6month']

          # check if last_sync field exists
          - set_condition: [HAS_SYNC, exists, last_sync]

          # check if asset is disabled (is_enabled = False)
          - set_condition: [IS_DISABLED, is_false, is_enabled]

          # check if last_sync is older than cutoff (stale)
          - set_condition: [IS_STALE, lt, last_sync, _cutoff]

          # discard entries with no last_sync (leave them untouched in DB)
          - discard: ['!HAS_SYNC']

          # discard entries that are still enabled (not yet disabled)
          - discard: ['!IS_DISABLED']

          # discard entries that are not stale enough (leave them untouched in DB)
          - discard: ['!IS_STALE']

          # disabled + stale entries: mark for deletion
          - field_set: ['', _action, delete]

          # clean up temporary field
          - field_delete: ['', _cutoff]


# --------------------------------------------------------------
# bulk_delete_disabled
#
# Delete assets that are disabled (is_enabled = False), regardless
# of last_sync.
#
# Logic:
#   - default _action is noop (safety: no change unless disabled)
#   - discard entries that are still enabled
#   - remaining entries are disabled: set _action to delete
#
# Note: discarded entries are NOT written to the database.
# Only disabled entries are deleted.
#
# Usage (apply to a schema):
#   schema.apply_pipeline('bulk_delete_disabled')
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_delete_disabled
  displayname: Bulk Delete Disabled
  run_permission: p_pipeline_run
  description: Delete assets that are disabled (is_enabled=False). Enabled assets are left untouched (discarded).
  content: |
      tasks:
          # default: noop (safe fallback - no change unless disabled)
          - field_set: ['', _action, noop]

          # check if asset is disabled (is_enabled = False)
          - set_condition: [IS_DISABLED, is_false, is_enabled]

          # discard entries that are still enabled (leave them untouched in DB)
          - discard: ['!IS_DISABLED']

          # disabled entries: mark for deletion
          - field_set: ['', _action, delete]


# --------------------------------------------------------------
# bulk_delete_all
#
# Delete ALL assets of the target schema without any condition.
#
# WARNING: this is destructive and irreversible.
# Use with caution. Prefer apply_pipeline with dryrun=True
# first to verify scope.
#
# Usage (apply to a schema):
#   schema.apply_pipeline('bulk_delete_all')
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_delete_all
  displayname: Bulk Delete (_action=delete)
  description: Delete all assets of the target schema unconditionally. Use with caution.
  run_permission: p_pipeline_run
  content: |
      tasks:
          - field_set: ['', _action, delete]


# --------------------------------------------------------------
# bulk_disable_all  / bulk_enable_all
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_disable_all
  displayname: Bulk Disable (_action=disable)
  description: Disable all assets unconditionally. Use with caution.
  run_permission: p_pipeline_run
  content: |
      tasks:
          - field_set: ['', _action, disable]

- classname: _pipeline
  keyname: bulk_enable_all
  displayname: Bulk Enable (_action=enable)
  run_permission: p_pipeline_run
  description: Enable all assets unconditionally. Use with caution.
  content: |
      tasks:
          - field_set: ['', _action, enable]


# --------------------------------------------------------------
# bulk_refresh
#
# Re-save all assets of the target schema without any change.
# Triggers a read-then-write cycle for each instance, which
# refreshes the EAV cache and normalizes stored data.
#
# Usage (apply to a schema):
#   schema.apply_pipeline('bulk_refresh')
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_refresh
  displayname: Bulk Refresh (_action=update)
  description: Create/Update all assets to refresh cache and normalize stored data.
  run_permission: p_pipeline_run
  content: |
      tasks:
          - field_set: ['', _action, update]


- classname: _pipeline
  keyname: bulk_noop
  displaynale: Bulk Noop (task noop)
  description: nothing on all assets (task noop, load as-is, refresh existing)
  run_permission: p_pipeline_run
  content: |
      tasks:
          - noop: ['']


# --------------------------------------------------------------
# bulk_discard
# browse and skip  all assets ; no new save to DB
# for tests and perfs checkups
# --------------------------------------------------------------

- classname: _pipeline
  keyname: bulk_discard
  displayname: Bulk Ignore (task=discard)
  description: Browse and kip all assets (discard processing, don't save)
  run_permission: p_pipeline_run
  content: |
      tasks:
          - discard: ['']