CREATE OR REPLACE PROCEDURE agg_user_platform_daily_proc(p_date VARCHAR)
    RETURNS STRING
    LANGUAGE SQL
    AS
    $$
    BEGIN
        -- Delete existing records for the date
        DELETE FROM agg_user_platform_daily
        WHERE p_date = :p_date;

        -- Insert aggregated records
        INSERT INTO agg_user_platform_daily
        SELECT
            user_id
            , user_uid
            , subscription_tier
            , platform
            , clip_total_cnt
            , gen_clip_total_cnt
            , play_cnt
            , play_cnt_own
            , play_cnt_others
            , play_duration_sec
            , play_duration_sec_own
            , play_duration_sec_others
            , total_follower_cnt
            , total_following_cnt
            , net_likes_received_cnt
            , is_active
            , is_active_create
            , is_active_listen
            , is_active_listen_own
            , is_active_listen_others
            , is_prev_day_active
            , is_prev_day_active_create
            , is_prev_day_active_listen
            , is_prev_day_active_listen_own
            , is_prev_day_active_listen_others
            , is_7d_active
            , is_7d_active_create
            , is_7d_active_listen
            , is_7d_active_listen_own
            , is_7d_active_listen_others
            , is_prev_7d_active
            , is_prev_7d_active_create
            , is_prev_7d_active_listen
            , is_prev_7d_active_listen_own
            , is_prev_7d_active_listen_others
            , is_28d_active
            , is_28d_active_create
            , is_28d_active_listen
            , is_28d_active_listen_own
            , is_28d_active_listen_others
            , is_prev_28d_active
            , is_prev_28d_active_create
            , is_prev_28d_active_listen
            , is_prev_28d_active_listen_own
            , is_prev_28d_active_listen_others
            , p_date
            , days_since_user_joined
        FROM
            agg_user_platform_daily_spine
        WHERE 1=1
            AND p_date = :p_date
            AND is_active;

        RETURN 'SUCCESS';
    END;
    $$;
