create or replace task SUNO_PROD.PROD.AF_INSTALLS_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_INSTALLS;

        INSERT INTO SUNO_PROD.PROD.AF_INSTALLS (
            ADVERTISING_ID,
            AF_AD,
            AF_AD_ID,
            AF_AD_TYPE,
            AF_ADSET,
            AF_ADSET_ID,
            AF_ATTRIBUTION_LOOKBACK,
            AF_C_ID,
            AF_CHANNEL,
            AF_COST_CURRENCY,
            AF_COST_VALUE,
            AF_KEYWORDS,
            AF_SITEID,
            AF_SUB1,
            AF_SUB2,
            AF_SUB5,
            AMAZON_AID,
            APP_ID,
            APP_NAME,
            APP_TYPE,
            APP_VERSION,
            APPSFLYER_ID,
            ATT,
            ATTRIBUTED_TOUCH_TIME,
            ATTRIBUTED_TOUCH_TYPE,
            BUNDLE_ID,
            CAMPAIGN,
            CAMPAIGN_TYPE,
            CARRIER,
            CITY,
            CONTRIBUTOR_1_CAMPAIGN,
            CONTRIBUTOR_1_ENGAGEMENT_TYPE,
            CONTRIBUTOR_1_MATCH_TYPE,
            CONTRIBUTOR_1_MEDIA_SOURCE,
            CONTRIBUTOR_1_TOUCH_TIME,
            CONTRIBUTOR_1_TOUCH_TYPE,
            CONTRIBUTOR_2_CAMPAIGN,
            CONTRIBUTOR_2_ENGAGEMENT_TYPE,
            CONTRIBUTOR_2_MATCH_TYPE,
            CONTRIBUTOR_2_MEDIA_SOURCE,
            CONTRIBUTOR_2_TOUCH_TIME,
            CONTRIBUTOR_2_TOUCH_TYPE,
            CONTRIBUTOR_3_CAMPAIGN,
            CONTRIBUTOR_3_ENGAGEMENT_TYPE,
            CONTRIBUTOR_3_MATCH_TYPE,
            CONTRIBUTOR_3_MEDIA_SOURCE,
            CONTRIBUTOR_3_TOUCH_TIME,
            CONTRIBUTOR_3_TOUCH_TYPE,
            CONVERSION_TYPE,
            COUNTRY_CODE,
            CUSTOMER_USER_ID,
            DEEPLINK_URL,
            DEVICE_CATEGORY,
            DEVICE_DOWNLOAD_TIME,
            DEVICE_MODEL,
            DMA,
            DT,
            ENGAGEMENT_TYPE,
            EVENT_NAME,
            EVENT_SOURCE,
            EVENT_TIME,
            GP_CLICK_TIME,
            GP_INSTALL_BEGIN,
            GP_REFERRER,
            H,
            HTTP_REFERRER,
            IDFV,
            INSTALL_TIME,
            IP,
            IS_LAT,
            IS_ORGANIC,
            IS_RETARGETING,
            KEYWORD_ID,
            LANGUAGE,
            MATCH_TYPE,
            MEDIA_SOURCE,
            NETWORK_ACCOUNT_ID,
            OPERATOR,
            ORIGINAL_URL,
            OS_VERSION,
            PLATFORM,
            POSTAL_CODE,
            REGION,
            SDK_VERSION,
            STATE,
            USER_AGENT,
            WIFI,
            _INGESTION_TIME
        )
        SELECT
            ADVERTISING_ID,
            AF_AD,
            AF_AD_ID,
            AF_AD_TYPE,
            AF_ADSET,
            AF_ADSET_ID,
            AF_ATTRIBUTION_LOOKBACK,
            AF_C_ID,
            AF_CHANNEL,
            AF_COST_CURRENCY,
            AF_COST_VALUE,
            AF_KEYWORDS,
            AF_SITEID,
            AF_SUB1,
            AF_SUB2,
            AF_SUB5,
            AMAZON_AID,
            APP_ID,
            APP_NAME,
            APP_TYPE,
            APP_VERSION,
            APPSFLYER_ID,
            ATT,
            ATTRIBUTED_TOUCH_TIME,
            ATTRIBUTED_TOUCH_TYPE,
            BUNDLE_ID,
            CAMPAIGN,
            CAMPAIGN_TYPE,
            CARRIER,
            CITY,
            CONTRIBUTOR_1_CAMPAIGN,
            CONTRIBUTOR_1_ENGAGEMENT_TYPE,
            CONTRIBUTOR_1_MATCH_TYPE,
            CONTRIBUTOR_1_MEDIA_SOURCE,
            CONTRIBUTOR_1_TOUCH_TIME,
            CONTRIBUTOR_1_TOUCH_TYPE,
            CONTRIBUTOR_2_CAMPAIGN,
            CONTRIBUTOR_2_ENGAGEMENT_TYPE,
            CONTRIBUTOR_2_MATCH_TYPE,
            CONTRIBUTOR_2_MEDIA_SOURCE,
            CONTRIBUTOR_2_TOUCH_TIME,
            CONTRIBUTOR_2_TOUCH_TYPE,
            CONTRIBUTOR_3_CAMPAIGN,
            CONTRIBUTOR_3_ENGAGEMENT_TYPE,
            CONTRIBUTOR_3_MATCH_TYPE,
            CONTRIBUTOR_3_MEDIA_SOURCE,
            CONTRIBUTOR_3_TOUCH_TIME,
            CONTRIBUTOR_3_TOUCH_TYPE,
            CONVERSION_TYPE,
            COUNTRY_CODE,
            CUSTOMER_USER_ID,
            DEEPLINK_URL,
            DEVICE_CATEGORY,
            DEVICE_DOWNLOAD_TIME,
            DEVICE_MODEL,
            DMA,
            DT,
            ENGAGEMENT_TYPE,
            EVENT_NAME,
            EVENT_SOURCE,
            EVENT_TIME,
            GP_CLICK_TIME,
            GP_INSTALL_BEGIN,
            GP_REFERRER,
            H,
            HTTP_REFERRER,
            IDFV,
            INSTALL_TIME,
            IP,
            IS_LAT,
            IS_ORGANIC,
            IS_RETARGETING,
            KEYWORD_ID,
            LANGUAGE,
            MATCH_TYPE,
            MEDIA_SOURCE,
            NETWORK_ACCOUNT_ID,
            OPERATOR,
            ORIGINAL_URL,
            OS_VERSION,
            PLATFORM,
            POSTAL_CODE,
            REGION,
            SDK_VERSION,
            STATE,
            USER_AGENT,
            WIFI,
            _INGESTION_TIME
        FROM APPSFLYER.SNOWFLAKE.INSTALLS_VIEW
        WHERE _INGESTION_TIME > :max_ingestion_time;

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