-- ====================================================================
-- 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_PATH VARCHAR2(1000),
    PROCESS_NAME VARCHAR2(200),
    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'))
) 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);

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