-- MARS-828: Add TRASH retention statuses to A_SOURCE_FILE_RECEIVED
-- Author: Grzegorz Michalski
-- Date: 2026-02-09
-- Description: Adds ARCHIVED_AND_TRASHED and ARCHIVED_AND_PURGED statuses to support TRASH retention feature

PROMPT ========================================
PROMPT MARS-828: Adding TRASH retention statuses
PROMPT ========================================

-- Drop old constraint
ALTER TABLE CT_MRDS.A_SOURCE_FILE_RECEIVED DROP CONSTRAINT A_SOURCE_FILE_RECEIVED_CHK;

-- Add new constraint with TRASH retention statuses
ALTER TABLE CT_MRDS.A_SOURCE_FILE_RECEIVED ADD CONSTRAINT A_SOURCE_FILE_RECEIVED_CHK 
    CHECK (PROCESSING_STATUS IN (
        'RECEIVED', 
        'VALIDATED',
        'VALIDATION_FAILED',
        'READY_FOR_INGESTION',
        'INGESTED', 
        'ARCHIVED',              -- Legacy status (backward compatibility)
        'ARCHIVED_AND_TRASHED',  -- Files archived to Parquet and kept in TRASH folder (DATA bucket subfolder)
        'ARCHIVED_AND_PURGED'    -- Files archived to Parquet and deleted from TRASH folder
    ));

-- Add comment
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.PROCESSING_STATUS IS 
    'File processing status: RECEIVED → VALIDATED → READY_FOR_INGESTION → INGESTED → ARCHIVED_AND_TRASHED → ARCHIVED_AND_PURGED (optional)';

-- Verify constraint
SELECT constraint_name, constraint_type, search_condition
FROM user_constraints
WHERE table_name = 'A_SOURCE_FILE_RECEIVED'
  AND constraint_name = 'A_SOURCE_FILE_RECEIVED_CHK';

-- Show current status distribution
SELECT PROCESSING_STATUS, COUNT(*) as FILE_COUNT
FROM CT_MRDS.A_SOURCE_FILE_RECEIVED
GROUP BY PROCESSING_STATUS
ORDER BY PROCESSING_STATUS;

PROMPT ========================================
PROMPT TRASH retention statuses added successfully
PROMPT Status flow: INGESTED → ARCHIVED_AND_TRASHED → ARCHIVED_AND_PURGED (optional)
PROMPT Legacy ARCHIVED status maintained for backward compatibility
PROMPT ========================================
