CREATE OR REPLACE PROCEDURE STG.LOAD_ATTEMPT_LOGS(file_name VARCHAR)
RETURNS STRING
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
DECLARE
    procedure_name varchar;
BEGIN
    
    procedure_name := 'LOAD_ATTEMPT_LOGS';

    call stg.log_audit(:procedure_name, 'Section 1', 99, 'START');
    -- Create or replace stage with dynamic file name
    EXECUTE IMMEDIATE 'CREATE OR REPLACE STAGE STG.ATTEMPT_LOGS_STAGE
    STORAGE_INTEGRATION = dev_bucket_integration
    URL = ''s3://doczy-dev-infra-raw-data-ingestion/training_interface/' || :file_name || '''
    FILE_FORMAT = (FORMAT_NAME = ''STG.CSV_HEADER'');';

    call stg.log_audit(:procedure_name, 'Section 1', 99, 'END');

    call stg.log_audit(:procedure_name, 'Section 2', 99, 'START');
    
    -- Copy command to load data
    COPY INTO STG.TRAINING_ATTEMPT_LOGS FROM 
    (
        SELECT 
            NULLIF(TRIM($1), '') AS FIELD_NAME,
            NULLIF(TRIM($2), '') AS CONTRACTS_TESTED,
            NULLIF(TRIM($3), '') AS USERNAME,
            NULLIF(TRIM($4), '') AS DATE_TIME,
            NULLIF(TRIM($5), '') AS ACCURACY,
            NULLIF(TRIM($6), '') AS ATTEMPT_NUM,
            CURRENT_TIMESTAMP(2) AS LOAD_DT
        FROM @STG.ATTEMPT_LOGS_STAGE
    )
    FILE_FORMAT = (FORMAT_NAME = 'STG.CSV_HEADER')
    ON_ERROR = ABORT_STATEMENT;

    call stg.log_audit(:procedure_name, 'Section 2', 99, 'END');
 
    call stg.log_audit(:procedure_name, 'Section 3', 99, 'START');

    INSERT INTO STG.DIM_AUDIT (AUDIT_SID, TABLE_NAME, SOURCE_FILE_NAME, LOAD_DATE, SOURCE_COUNT)
    SELECT STG.AUDIT_SID.NEXTVAL, 'STG.TRAINING_ATTEMPT_LOGS',* 
    FROM 
    (SELECT DISTINCT METADATA$FILENAME, CURRENT_TIMESTAMP(), max(METADATA$FILE_ROW_NUMBER)  from @STG.ATTEMPT_LOGS_STAGE group by 1,2);
    
    call stg.log_audit(:procedure_name, 'Section 3', 99, 'END');

    RETURN 'Setup, Load, and Audit Complete';
END;
$$;

