create or replace task SUNO_PROD.PROD.AF_SKAD_INAPPS_DAILY_INSERT
    warehouse=SUNO_PROD_RDS_DAILY_X_SMALL
    schedule='USING CRON 0 11 * * * UTC'
as
    declare
        start_time TIMESTAMP;
        max_ingestion_time timestamp;
        row_count number;
    begin
        start_time := CURRENT_TIMESTAMP();
        SELECT COALESCE(MAX(_INGESTION_TIME), '0001-01-01')
            INTO :max_ingestion_time
        FROM SUNO_PROD.PROD.AF_SKAD_INAPPS;

        INSERT INTO SUNO_PROD.PROD.AF_SKAD_INAPPS (
            AD_NETWORK_ADSET_ID,
            AD_NETWORK_ADSET_NAME,
            AD_NETWORK_CAMPAIGN_ID,
            AD_NETWORK_CAMPAIGN_NAME,
            AD_NETWORK_TIMESTAMP,
            AF_ATTRIBUTION_FLAG,
            APP_ID,
            DT,
            EVENT_NAME,
            EVENT_UUID,
            H,
            INSTALL_DATE,
            INSTALL_TYPE,
            IP,
            MAX_EVENT_COUNTER,
            MAX_REVENUE,
            MAX_TIME_POST_INSTALL,
            MEASUREMENT_WINDOW,
            MEDIA_SOURCE,
            MIN_EVENT_COUNTER,
            MIN_REVENUE,
            MIN_TIME_POST_INSTALL,
            SKAD_AD_NETWORK_ID,
            SKAD_AMBIGUOUS_EVENT,
            SKAD_ATTRIBUTION_SIGNATURE,
            SKAD_CAMPAIGN_ID,
            SKAD_COARSE_CONVERSION_VALUE,
            SKAD_CONVERSION_VALUE,
            SKAD_DID_WIN,
            SKAD_FIDELITY_TYPE,
            SKAD_MODE,
            SKAD_POSTBACK_SEQUENCE_INDEX,
            SKAD_REDOWNLOAD,
            SKAD_REVENUE,
            SKAD_SOURCE_APP_ID,
            SKAD_SOURCE_DOMAIN,
            SKAD_SOURCE_IDENTIFIER,
            SKAD_TRANSACTION_ID,
            SKAD_VERSION,
            TIMESTAMP,
            _INGESTION_TIME
        )
        SELECT
            AD_NETWORK_ADSET_ID,
            AD_NETWORK_ADSET_NAME,
            AD_NETWORK_CAMPAIGN_ID,
            AD_NETWORK_CAMPAIGN_NAME,
            AD_NETWORK_TIMESTAMP,
            AF_ATTRIBUTION_FLAG,
            APP_ID,
            DT,
            EVENT_NAME,
            EVENT_UUID,
            H,
            INSTALL_DATE,
            INSTALL_TYPE,
            IP,
            MAX_EVENT_COUNTER,
            MAX_REVENUE,
            MAX_TIME_POST_INSTALL,
            MEASUREMENT_WINDOW,
            MEDIA_SOURCE,
            MIN_EVENT_COUNTER,
            MIN_REVENUE,
            MIN_TIME_POST_INSTALL,
            SKAD_AD_NETWORK_ID,
            SKAD_AMBIGUOUS_EVENT,
            SKAD_ATTRIBUTION_SIGNATURE,
            SKAD_CAMPAIGN_ID,
            SKAD_COARSE_CONVERSION_VALUE,
            SKAD_CONVERSION_VALUE,
            SKAD_DID_WIN,
            SKAD_FIDELITY_TYPE,
            SKAD_MODE,
            SKAD_POSTBACK_SEQUENCE_INDEX,
            SKAD_REDOWNLOAD,
            SKAD_REVENUE,
            SKAD_SOURCE_APP_ID,
            SKAD_SOURCE_DOMAIN,
            SKAD_SOURCE_IDENTIFIER,
            SKAD_TRANSACTION_ID,
            SKAD_VERSION,
            TIMESTAMP,
            _INGESTION_TIME
        FROM APPSFLYER.SNOWFLAKE.SKAD_INAPPS_VIEW
        WHERE _INGESTION_TIME > :max_ingestion_time;

        SELECT COUNT(*)
            INTO :row_count
        FROM SUNO_PROD.PROD.AF_SKAD_INAPPS
        WHERE _INGESTION_TIME > :max_ingestion_time;
        call TASK_MONITOR_INSERT_PROC('AF_SKAD_INAPPS_DAILY_INSERT', :start_time, :row_count, 'SUCCESS', 'DAILY');
    end
;
