-- ====================================================================
-- A_SOURCE_FILE_CONFIG Table
-- ====================================================================
-- 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
-- ====================================================================

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),
    DAYS_FOR_ARCHIVE_THRESHOLD NUMBER(4,0), 
    FILES_COUNT_OVER_ARCHIVE_THRESHOLD NUMBER(38,0), 
    BYTES_SUM_OVER_ARCHIVE_THRESHOLD NUMBER(38,0), 
    ODS_SCHEMA_NAME VARCHAR2(100), 
    ROWS_COUNT_OVER_ARCHIVE_THRESHOLD 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',
    ARCHIVE_ENABLED CHAR(1) DEFAULT 'Y' NOT NULL,
    KEEP_IN_TRASH CHAR(1) DEFAULT 'N' NOT NULL,
    CONSTRAINT A_SOURCE_FILE_CONFIG_PK PRIMARY KEY (A_SOURCE_FILE_CONFIG_KEY),
    CONSTRAINT CHK_ARCHIVE_ENABLED CHECK (ARCHIVE_ENABLED IN ('Y', 'N')),
    CONSTRAINT CHK_KEEP_IN_TRASH CHECK (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";

-- Primary key index (from production export)
CREATE UNIQUE INDEX "CT_MRDS"."A_SOURCE_FILE_CONFIG_PK" 
ON "CT_MRDS"."A_SOURCE_FILE_CONFIG" ("A_SOURCE_FILE_CONFIG_KEY") 
TABLESPACE "DATA";

-- Unique constraint index (from production export)
CREATE UNIQUE INDEX "CT_MRDS"."A_SOURCE_FILE_CONFIG_UQ1" 
ON "CT_MRDS"."A_SOURCE_FILE_CONFIG" ("SOURCE_FILE_TYPE", "SOURCE_FILE_ID", "TABLE_ID") 
TABLESPACE "DATA";

-- Column comments
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVAL_STRATEGY IS 'Archival strategy: THRESHOLD_BASED, CURRENT_MONTH_ONLY, MINIMUM_AGE_MONTHS, HYBRID. Added in MARS-828';
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.MINIMUM_AGE_MONTHS IS 'Minimum age in months before archival (required for MINIMUM_AGE_MONTHS strategy). Added in MARS-828';
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ENCODING IS 'Oracle character set name for CSV files (e.g., UTF8, WE8MSWIN1252, EE8ISO8859P2). Added in MARS-1049';
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.ARCHIVE_ENABLED IS 'Y=Enable archiving, N=Skip archiving. Controls if table participates in archival process. Added in MARS-828 v3.3.0';
COMMENT ON COLUMN CT_MRDS.A_SOURCE_FILE_CONFIG.KEEP_IN_TRASH IS 'Y=Keep files in TRASH after archiving, N=Delete immediately. Controls TRASH retention policy. Added in MARS-828 v3.3.0';

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