-- Upsert data from RDS to Snowflake
-- This query inserts new records and updates existing ones based on ID and partition

merge into {label_maker_annotations_table_name} as target
using 
(
    select 
        $1:id as id,
        $1:created_at as created_at,
        $1:updated_at as updated_at,
        $1:user_id as user_id,
        $1:task_id as task_id,
        $1:preferences as preferences,
        $1:feedback as feedback,
        $1:listen_time_seconds as listen_time_seconds,
        $1:total_listen_time_seconds as total_listen_time_seconds,
        $1:skipped as skipped,
        $1:credits_earned as credits_earned,
        $1:credits_processed as credits_processed,
        date(to_timestamp(created_at)) as p_date,
        hour(to_timestamp(created_at)) as p_hour
    from {stage_path} (file_format => 'BASIC_PARQUET_LOADER', pattern => ('.*.parquet'))
) as source
on (target.id = source.id and target.p_date = source.p_date and target.p_hour = source.p_hour)
when matched then
    update set 
        target.created_at = source.created_at,
        target.updated_at = source.updated_at,
        target.user_id = source.user_id,
        target.task_id = source.task_id,
        target.preferences = source.preferences,
        target.feedback = source.feedback,
        target.listen_time_seconds = source.listen_time_seconds,
        target.total_listen_time_seconds = source.total_listen_time_seconds,
        target.skipped = source.skipped,
        target.credits_earned = source.credits_earned,
        target.credits_processed = source.credits_processed,
        target.p_date = source.p_date,
        target.p_hour = source.p_hour
when not matched then
    insert (id, created_at, updated_at, user_id, task_id, preferences, feedback, listen_time_seconds, total_listen_time_seconds, skipped, credits_earned, credits_processed, p_date, p_hour)
    values (source.id, source.created_at, source.updated_at, source.user_id, source.task_id, source.preferences, source.feedback, source.listen_time_seconds, source.total_listen_time_seconds, source.skipped, source.credits_earned, source.credits_processed, source.p_date, source.p_hour);
