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

        INSERT INTO SUNO_PROD.PROD.AF_WEBSITE_VISITS (
            ADVERTISING_ID,
            AF_WEB_ID,
            AMAZON_AID,
            APP_ID,
            APP_NAME,
            APP_VERSION,
            APPSFLYER_ID,
            ATTRIBUTED_TOUCH_TIME,
            ATTRIBUTED_TOUCH_TYPE,
            BUNDLE_ID,
            CAMPAIGN,
            CAMPAIGN_ID,
            CITY,
            COUNTRY_CODE,
            CUSTOMER_USER_ID,
            DEVICE_CATEGORY,
            DMA,
            DT,
            EVENT_NAME,
            EVENT_REVENUE,
            EVENT_REVENUE_USD,
            EVENT_SOURCE,
            EVENT_TIME,
            EVENT_TYPE,
            EVENT_URL,
            H,
            IDFV,
            INSTALL_TIME,
            IP,
            LANGUAGE,
            MEDIA_CHANNEL,
            MEDIA_SOURCE,
            MEDIA_TYPE,
            ORIGINAL_URL,
            OS_VERSION,
            PLATFORM,
            POSTAL_CODE,
            QUERY_PARAMS,
            QUERY_PARAMS_DEPRECATED,
            REFERRER,
            REGION,
            STATE,
            USER_AGENT,
            _INGESTION_TIME
        )
        SELECT
            ADVERTISING_ID,
            AF_WEB_ID,
            AMAZON_AID,
            APP_ID,
            APP_NAME,
            APP_VERSION,
            APPSFLYER_ID,
            ATTRIBUTED_TOUCH_TIME,
            ATTRIBUTED_TOUCH_TYPE,
            BUNDLE_ID,
            CAMPAIGN,
            CAMPAIGN_ID,
            CITY,
            COUNTRY_CODE,
            CUSTOMER_USER_ID,
            DEVICE_CATEGORY,
            DMA,
            DT,
            EVENT_NAME,
            EVENT_REVENUE,
            EVENT_REVENUE_USD,
            EVENT_SOURCE,
            EVENT_TIME,
            EVENT_TYPE,
            EVENT_URL,
            H,
            IDFV,
            INSTALL_TIME,
            IP,
            LANGUAGE,
            MEDIA_CHANNEL,
            MEDIA_SOURCE,
            MEDIA_TYPE,
            ORIGINAL_URL,
            OS_VERSION,
            PLATFORM,
            POSTAL_CODE,
            QUERY_PARAMS,
            QUERY_PARAMS_DEPRECATED,
            REFERRER,
            REGION,
            STATE,
            USER_AGENT,
            _INGESTION_TIME
        FROM APPSFLYER.SNOWFLAKE.WEBSITE_VISITS_VIEW
        WHERE _INGESTION_TIME > :max_ingestion_time;

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