-- ====================================================================
-- A_SOURCE_FILE_RECEIVED Table
-- ====================================================================
-- Purpose: Track received files and their processing status
-- ====================================================================

CREATE TABLE CT_MRDS.A_SOURCE_FILE_RECEIVED (
    A_SOURCE_FILE_RECEIVED_KEY NUMBER(38,0) NOT NULL ENABLE, 
    A_SOURCE_FILE_CONFIG_KEY NUMBER(38,0) NOT NULL ENABLE,
    SOURCE_FILE_NAME  VARCHAR2(1000) NOT NULL,
    CHECKSUM          VARCHAR2(128), 
    CREATED           TIMESTAMP(6) WITH TIME ZONE, 
    BYTES             NUMBER,
    RECEPTION_DATE DATE NOT NULL,
    PROCESSING_STATUS VARCHAR2(200),
    EXTERNAL_TABLE_NAME VARCHAR2(200),
    PARTITION_YEAR VARCHAR2(4), 
    PARTITION_MONTH VARCHAR2(2),
    ARCH_FILE_NAME VARCHAR2(1000),
    CONSTRAINT A_SOURCE_FILE_RECEIVED_PK PRIMARY KEY (A_SOURCE_FILE_RECEIVED_KEY),
    CONSTRAINT ASFR_A_SOURCE_FILE_CONFIG_KEY_FK FOREIGN KEY(A_SOURCE_FILE_CONFIG_KEY) REFERENCES CT_MRDS.A_SOURCE_FILE_CONFIG(A_SOURCE_FILE_CONFIG_KEY),
    CONSTRAINT A_SOURCE_FILE_RECEIVED_CHK CHECK (PROCESSING_STATUS IN ('RECEIVED', 'VALIDATED', 'READY_FOR_INGESTION', 'INGESTED', 'ARCHIVED', 'ARCHIVED_AND_TRASHED', 'ARCHIVED_AND_PURGED'))
) TABLESPACE "DATA";

-- Unique index for file identification (workaround for TIMESTAMP WITH TIMEZONE constraint limitation)
CREATE UNIQUE INDEX CT_MRDS.A_SOURCE_FILE_RECEIVED_UK1 
ON CT_MRDS.A_SOURCE_FILE_RECEIVED(CHECKSUM, CREATED, BYTES);

-- Column comments
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.A_SOURCE_FILE_RECEIVED_KEY IS 
    'Primary key - unique identifier for received file record';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.A_SOURCE_FILE_CONFIG_KEY IS 
    'Foreign key to A_SOURCE_FILE_CONFIG - links file to its configuration and processing rules';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.SOURCE_FILE_NAME IS 
    'Full object name/path of the received file in OCI Object Storage (includes INBOX prefix)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.CHECKSUM IS 
    'MD5 checksum of file content for integrity verification and duplicate detection';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.CREATED IS 
    'Timestamp with timezone when file was created/uploaded to Object Storage (from DBMS_CLOUD.LIST_OBJECTS)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.BYTES IS 
    'File size in bytes';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.RECEPTION_DATE IS 
    'Date when file was registered in the system (extracted from CREATED timestamp)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.PROCESSING_STATUS IS 
    'Current processing status: RECEIVED → VALIDATED → READY_FOR_INGESTION → INGESTED → ARCHIVED_AND_TRASHED → ARCHIVED_AND_PURGED';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.EXTERNAL_TABLE_NAME IS 
    'Name of temporary external table created for file validation (dropped after validation)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.PARTITION_YEAR IS 
    'Year partition value (YYYY format) when file was archived to ARCHIVE bucket with Hive-style partitioning';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.PARTITION_MONTH IS 
    'Month partition value (MM format) when file was archived to ARCHIVE bucket with Hive-style partitioning';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_RECEIVED.ARCH_FILE_NAME IS 
    'Archive directory prefix in ARCHIVE bucket containing archived Parquet files (supports multiple files from parallel DBMS_CLOUD.EXPORT_DATA)';

GRANT SELECT, INSERT, UPDATE, DELETE ON CT_MRDS.A_SOURCE_FILE_RECEIVED TO MRDS_LOADER_ROLE;