-- ====================================================================
-- A_SOURCE_FILE_CONFIG Table (rollback_version - pre MARS-1409)
-- ====================================================================
-- Purpose: Store source file configuration and processing rules
-- MARS-1049: Added ENCODING column for CSV character set support
-- MARS-828: Added ARCHIVAL_STRATEGY and MINIMUM_AGE_MONTHS for archival automation
-- NOTE: IS_WORKFLOW_SUCCESS_REQUIRED column NOT included (added by MARS-1409)
-- ====================================================================

CREATE TABLE CT_MRDS.A_SOURCE_FILE_CONFIG (
    A_SOURCE_FILE_CONFIG_KEY NUMBER(38,0) NOT NULL ENABLE, 
    A_SOURCE_KEY VARCHAR2(30) NOT NULL ENABLE,
    SOURCE_FILE_TYPE VARCHAR2(200), -- Can be 'INPUT' or 'CONTAINER' or 'LOAD_CONFIG'
    SOURCE_FILE_ID VARCHAR2(200), 
    SOURCE_FILE_DESC VARCHAR2(2000), 
    SOURCE_FILE_NAME_PATTERN VARCHAR2(200), 
    TABLE_ID VARCHAR2(200), 
    TEMPLATE_TABLE_NAME VARCHAR2(200), 
    CONTAINER_FILE_KEY NUMBER(38,0),
    ARCHIVE_THRESHOLD_DAYS NUMBER(4,0), 
    ARCHIVE_THRESHOLD_FILES_COUNT NUMBER(38,0), 
    ARCHIVE_THRESHOLD_BYTES_SUM NUMBER(38,0), 
    ODS_SCHEMA_NAME VARCHAR2(100), 
    ARCHIVE_THRESHOLD_ROWS_COUNT NUMBER(38,0), 
    HOURS_TO_EXPIRE_STATISTICS NUMBER(38,3),
    ARCHIVAL_STRATEGY VARCHAR2(50),
    MINIMUM_AGE_MONTHS NUMBER(3,0),
    ENCODING VARCHAR2(50) DEFAULT 'UTF8',
    IS_ARCHIVE_ENABLED CHAR(1) DEFAULT 'N' NOT NULL,
    IS_KEEP_IN_TRASH CHAR(1) DEFAULT 'N' NOT NULL,
    CONSTRAINT A_SOURCE_FILE_CONFIG_PK PRIMARY KEY (A_SOURCE_FILE_CONFIG_KEY),
    CONSTRAINT CHK_IS_ARCHIVE_ENABLED CHECK (IS_ARCHIVE_ENABLED IN ('Y', 'N')),
    CONSTRAINT CHK_IS_KEEP_IN_TRASH CHECK (IS_KEEP_IN_TRASH IN ('Y', 'N')),
    CONSTRAINT SOURCE_FILE_TYPE_CHK CHECK (SOURCE_FILE_TYPE IN ('INPUT', 'CONTAINER', 'LOAD_CONFIG')),
    CONSTRAINT ASFC_A_SOURCE_KEY_FK FOREIGN KEY(A_SOURCE_KEY) REFERENCES CT_MRDS.A_SOURCE(A_SOURCE_KEY),
    CONSTRAINT ASFC_CONTAINER_FILE_KEY_FK FOREIGN KEY(CONTAINER_FILE_KEY) REFERENCES CT_MRDS.A_SOURCE_FILE_CONFIG(A_SOURCE_FILE_CONFIG_KEY),
    CONSTRAINT A_SOURCE_FILE_CONFIG_UQ1 UNIQUE(SOURCE_FILE_TYPE, SOURCE_FILE_ID, TABLE_ID)
) TABLESPACE "DATA";

-- Column comments
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.A_SOURCE_FILE_CONFIG_KEY IS 
    'Primary key - unique identifier for source file configuration record';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.A_SOURCE_KEY IS 
    'Foreign key to A_SOURCE table - identifies the source system (e.g., LM, C2D, CSDB)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.SOURCE_FILE_TYPE IS 
    'Type of file configuration: INPUT (data files), CONTAINER (xml files), or LOAD_CONFIG (configuration files)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.SOURCE_FILE_ID IS 
    'Unique identifier for the source file within the source system (e.g., UC_DISSEM, STANDING_FACILITIES)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.SOURCE_FILE_DESC IS 
    'Human-readable description of the source file and its purpose';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.SOURCE_FILE_NAME_PATTERN IS 
    'Filename pattern for matching incoming files (supports wildcards, e.g., UC_NMA_DISSEM-*.csv)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.TABLE_ID IS 
    'Identifier for the target table where data will be loaded (without schema prefix)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.TEMPLATE_TABLE_NAME IS 
    'Fully qualified name of template table in CT_ET_TEMPLATES schema used for external table creation';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.CONTAINER_FILE_KEY IS 
    'Foreign key to parent container configuration when this file is part of an xml (NULL for standalone files)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVE_THRESHOLD_DAYS IS 
    'Threshold for THRESHOLD_BASED strategy: archive data older than N days';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVE_THRESHOLD_FILES_COUNT IS 
    'Trigger archival when file count exceeds this threshold (used in THRESHOLD_BASED and HYBRID strategies)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVE_THRESHOLD_BYTES_SUM IS 
    'Trigger archival when total size in bytes exceeds this threshold (used in THRESHOLD_BASED and HYBRID strategies)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVE_THRESHOLD_ROWS_COUNT IS 
    'Trigger archival when total row count exceeds this threshold (used in THRESHOLD_BASED and HYBRID strategies)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ODS_SCHEMA_NAME IS 
    'Schema name where ODS external tables are created (typically ODS)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.HOURS_TO_EXPIRE_STATISTICS IS 
    'Number of hours before table statistics expire and need to be recalculated';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVAL_STRATEGY IS 
    'Archival strategy: THRESHOLD_BASED (days-based), MINIMUM_AGE_MONTHS (0=current month, N=retain N months), HYBRID (combination)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.MINIMUM_AGE_MONTHS IS 
    'Minimum age in months before archival (required for MINIMUM_AGE_MONTHS and HYBRID strategies, 0=current month only)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ENCODING IS 
    'Oracle character set name for CSV files (e.g., UTF8, WE8MSWIN1252, EE8ISO8859P2)';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.IS_ARCHIVE_ENABLED IS 
    'Y=Enable archiving, N=Skip archiving. Controls if table participates in archival process';

COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.IS_KEEP_IN_TRASH IS 
    'Y=Keep files in TRASH after archiving, N=Delete immediately. Controls TRASH retention policy';

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