-- ====================================================================
-- A_SOURCE_FILE_CONFIG Table
-- ====================================================================
-- Purpose: Store source file configuration and processing rules
-- MARS-1049: Added ENCODING column for CSV character set support
-- ====================================================================

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),
    ENCODING VARCHAR2(50) DEFAULT 'UTF8',
    CONSTRAINT A_SOURCE_FILE_CONFIG_PK PRIMARY KEY (A_SOURCE_FILE_CONFIG_KEY),
    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.ENCODING IS 'Oracle character set name for CSV files (e.g., UTF8, WE8MSWIN1252, EE8ISO8859P2). Added in MARS-1049';

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