INSERT INTO {{ rec_run_table_name }} (
    REC_RUN_ID, USER_ID, USER_UID, CREATED_AT, VERSION, SURFACE, EXPERIMENT_NAME,
    NUM_RECOMMENDATIONS_REQUESTED, NUM_FOLLOWED_CREATORS, ELC_MODE,
    WEIGHT_FOLLOW, WEIGHT_AUDIO_SIMILARITY, WEIGHT_VIDEO_SIMILARITY, WEIGHT_GENRE_SIMILARITY,
    WEIGHT_FRESH, WEIGHT_POPULAR, WEIGHT_MANUAL_CHAMPION, WEIGHT_CREATOR_CHAMPION, 
    WEIGHT_COLLABORATIVE_FILTERING, CREATOR_MIN_DISTANCE, MAX_VIDEO_COVER_RATIO,
    RERANK_ENABLED_BUCKETS, LIKED_HOOKS_COUNT, LIKED_CLIPS_COUNT, LISTENING_HISTORY_CLIPS_COUNT,
    CREATION_TAGS_COUNT, WATCH_HISTORY_COUNT, NUM_HIDDEN_CREATORS,
    VIDEO_SEEDS_TOTAL, VIDEO_SEEDS_SELECTED, AUDIO_SEEDS_TOTAL, AUDIO_SEEDS_SELECTED,
    TOTAL_RETRIEVED, TOTAL_UNIQUE, RETRIEVED_POPULAR, RETRIEVED_CREATOR_CHAMPION,
    RETRIEVED_GENRE_SIMILARITY, RETRIEVED_MANUAL_CHAMPION, RETRIEVED_COLLABORATIVE_FILTERING,
    RETRIEVED_FOLLOW, RETRIEVED_FRESH, RETRIEVED_VIDEO_SIMILARITY, RETRIEVED_AUDIO_SIMILARITY,
    TOTAL_CANDIDATES, TOTAL_FILTERED, TOTAL_REMAINING, FILTERED_BY_WATCHED,
    FILTERED_BY_LIKED, FILTERED_BY_HIDDEN_CREATOR, FILTERED_BY_OTHER,
    RERANK_ENABLED, FUSION_METHOD, NUM_RECOMMENDATIONS_RETURNED, UNIQUE_CREATORS, UNIQUE_SONGS,
    CONTENT_SOURCE_USER_UPLOAD, CONTENT_SOURCE_BACKFILL_VIDEO_COVER, CONTENT_SOURCE_OTHER,
    PERF_TOTAL_MS, AVG_GEMINI_RATING, GEMINI_RATING_0_COUNT, GEMINI_RATING_1_COUNT,
    GEMINI_RATING_2_COUNT, GEMINI_RATING_3_COUNT, GEMINI_RATING_4_COUNT,
    AGE_P50_HOURS, AGE_LAST_24H_COUNT, AVG_VIEW_COUNT, AVG_LIKE_COUNT,
    FINAL_POPULAR_COUNT, FINAL_CREATOR_CHAMPION_COUNT, FINAL_FOLLOW_COUNT,
    FINAL_MANUAL_CHAMPION_COUNT, FINAL_AUDIO_SIMILARITY_COUNT, FINAL_VIDEO_SIMILARITY_COUNT,
    FINAL_FRESH_COUNT, FINAL_GENRE_SIMILARITY_COUNT, FINAL_COLLABORATIVE_FILTERING_COUNT,
    WARNINGS_COUNT, ERRORS_COUNT, HOOK_SIGNALS_LIKED_COUNT, HOOK_SIGNALS_OMNIPLAY_COUNT,
    HOOK_SIGNALS_LONG_WATCH_COUNT, HOOK_SIGNALS_COMMENT_COUNT, HOOK_SIGNALS_COMMENT_VIEW_COUNT,
    HOOK_SIGNALS_PROFILE_VIEW_COUNT, WEIGHT_HOOK_SIGNAL_LIKED, WEIGHT_HOOK_SIGNAL_OMNIPLAY,
    WEIGHT_HOOK_SIGNAL_LONG_WATCH, WEIGHT_HOOK_SIGNAL_COMMENT, WEIGHT_HOOK_SIGNAL_COMMENT_VIEW,
    WEIGHT_HOOK_SIGNAL_PROFILE_VIEW, P_DATE, P_HOUR
)
SELECT 
    -- Core Identifiers
    properties:recommendation_run_id as REC_RUN_ID,
    properties:user_id as USER_ID,
    properties:user_uuid as USER_UID,
    timestamp as CREATED_AT,
    
    -- Configuration
    properties:version as VERSION,
    properties:surface as SURFACE,
    properties:experiment_name as EXPERIMENT_NAME,
    
    -- Request Parameters
    properties:num_recommendations_requested as NUM_RECOMMENDATIONS_REQUESTED,
    properties:num_followed_creators as NUM_FOLLOWED_CREATORS,
    properties:elc_mode as ELC_MODE,
    
    -- Algorithm Weights
    properties:weight_follow as WEIGHT_FOLLOW,
    properties:weight_audio_similarity as WEIGHT_AUDIO_SIMILARITY,
    properties:weight_video_similarity as WEIGHT_VIDEO_SIMILARITY,
    properties:weight_genre_similarity as WEIGHT_GENRE_SIMILARITY,
    properties:weight_fresh as WEIGHT_FRESH,
    properties:weight_popular as WEIGHT_POPULAR,
    properties:weight_manual_champion as WEIGHT_MANUAL_CHAMPION,
    properties:weight_creator_champion as WEIGHT_CREATOR_CHAMPION,
    properties:weight_collaborative_filtering as WEIGHT_COLLABORATIVE_FILTERING,
    
    -- Algorithm Parameters
    properties:creator_min_distance as CREATOR_MIN_DISTANCE,
    properties:max_video_cover_ratio as MAX_VIDEO_COVER_RATIO,
    properties:rerank_enabled_buckets as RERANK_ENABLED_BUCKETS,
    
    -- User History Counts
    properties:liked_hooks_count as LIKED_HOOKS_COUNT,
    properties:liked_clips_count as LIKED_CLIPS_COUNT,
    properties:listening_history_clips_count as LISTENING_HISTORY_CLIPS_COUNT,
    properties:creation_tags_count as CREATION_TAGS_COUNT,
    properties:watch_history_count as WATCH_HISTORY_COUNT,
    properties:num_hidden_creators as NUM_HIDDEN_CREATORS,
    
    -- Seeds Information
    properties:video_seeds_total as VIDEO_SEEDS_TOTAL,
    properties:video_seeds_selected as VIDEO_SEEDS_SELECTED,
    properties:audio_seeds_total as AUDIO_SEEDS_TOTAL,
    properties:audio_seeds_selected as AUDIO_SEEDS_SELECTED,
    
    -- Retrieval Stats
    properties:total_retrieved as TOTAL_RETRIEVED,
    properties:total_unique as TOTAL_UNIQUE,
    properties:retrieved_popular as RETRIEVED_POPULAR,
    properties:retrieved_creator_champion as RETRIEVED_CREATOR_CHAMPION,
    properties:retrieved_genre_similarity as RETRIEVED_GENRE_SIMILARITY,
    properties:retrieved_manual_champion as RETRIEVED_MANUAL_CHAMPION,
    properties:retrieved_collaborative_filtering as RETRIEVED_COLLABORATIVE_FILTERING,
    properties:retrieved_follow as RETRIEVED_FOLLOW,
    properties:retrieved_fresh as RETRIEVED_FRESH,
    properties:retrieved_video_similarity as RETRIEVED_VIDEO_SIMILARITY,
    properties:retrieved_audio_similarity as RETRIEVED_AUDIO_SIMILARITY,
    
    -- Filtering Stats
    properties:total_candidates as TOTAL_CANDIDATES,
    properties:total_filtered as TOTAL_FILTERED,
    properties:total_remaining as TOTAL_REMAINING,
    properties:filtered_by_watched as FILTERED_BY_WATCHED,
    properties:filtered_by_liked as FILTERED_BY_LIKED,
    properties:filtered_by_hidden_creator as FILTERED_BY_HIDDEN_CREATOR,
    properties:filtered_by_other as FILTERED_BY_OTHER,
    
    -- Reranking and Fusion
    properties:rerank_enabled as RERANK_ENABLED,
    properties:fusion_method as FUSION_METHOD,
    
    -- Final Results
    properties:num_recommendations_returned as NUM_RECOMMENDATIONS_RETURNED,
    properties:unique_creators as UNIQUE_CREATORS,
    properties:unique_songs as UNIQUE_SONGS,
    
    -- Content Source Distribution
    properties:content_source_user_upload as CONTENT_SOURCE_USER_UPLOAD,
    properties:content_source_backfill_video_cover as CONTENT_SOURCE_BACKFILL_VIDEO_COVER,
    properties:content_source_other as CONTENT_SOURCE_OTHER,
    
    -- Performance Metrics
    properties:perf_total_ms as PERF_TOTAL_MS,
    
    -- Quality Metrics
    properties:avg_gemini_rating as AVG_GEMINI_RATING,
    properties:gemini_rating_0_count as GEMINI_RATING_0_COUNT,
    properties:gemini_rating_1_count as GEMINI_RATING_1_COUNT,
    properties:gemini_rating_2_count as GEMINI_RATING_2_COUNT,
    properties:gemini_rating_3_count as GEMINI_RATING_3_COUNT,
    properties:gemini_rating_4_count as GEMINI_RATING_4_COUNT,
    
    -- Content Age and Engagement
    properties:age_p50_hours as AGE_P50_HOURS,
    properties:age_last_24h_count as AGE_LAST_24H_COUNT,
    properties:avg_view_count as AVG_VIEW_COUNT,
    properties:avg_like_count as AVG_LIKE_COUNT,
    
    -- Final Algorithm Distribution
    properties:final_popular_count as FINAL_POPULAR_COUNT,
    properties:final_creator_champion_count as FINAL_CREATOR_CHAMPION_COUNT,
    properties:final_follow_count as FINAL_FOLLOW_COUNT,
    properties:final_manual_champion_count as FINAL_MANUAL_CHAMPION_COUNT,
    properties:final_audio_similarity_count as FINAL_AUDIO_SIMILARITY_COUNT,
    properties:final_video_similarity_count as FINAL_VIDEO_SIMILARITY_COUNT,
    properties:final_fresh_count as FINAL_FRESH_COUNT,
    properties:final_genre_similarity_count as FINAL_GENRE_SIMILARITY_COUNT,
    properties:final_collaborative_filtering_count as FINAL_COLLABORATIVE_FILTERING_COUNT,
    
    -- Error and Warning Tracking
    properties:warnings_count as WARNINGS_COUNT,
    properties:errors_count as ERRORS_COUNT,
    
    -- Hook Signals
    properties:hook_signals_liked_count as HOOK_SIGNALS_LIKED_COUNT,
    properties:hook_signals_omniplay_count as HOOK_SIGNALS_OMNIPLAY_COUNT,
    properties:hook_signals_long_watch_count as HOOK_SIGNALS_LONG_WATCH_COUNT,
    properties:hook_signals_comment_count as HOOK_SIGNALS_COMMENT_COUNT,
    properties:hook_signals_comment_view_count as HOOK_SIGNALS_COMMENT_VIEW_COUNT,
    properties:hook_signals_profile_view_count as HOOK_SIGNALS_PROFILE_VIEW_COUNT,
    
    -- Hook Signal Weights
    properties:weight_hook_signal_liked as WEIGHT_HOOK_SIGNAL_LIKED,
    properties:weight_hook_signal_omniplay as WEIGHT_HOOK_SIGNAL_OMNIPLAY,
    properties:weight_hook_signal_long_watch as WEIGHT_HOOK_SIGNAL_LONG_WATCH,
    properties:weight_hook_signal_comment as WEIGHT_HOOK_SIGNAL_COMMENT,
    properties:weight_hook_signal_comment_view as WEIGHT_HOOK_SIGNAL_COMMENT_VIEW,
    properties:weight_hook_signal_profile_view as WEIGHT_HOOK_SIGNAL_PROFILE_VIEW,
    
    -- Partitioning columns
    p_date as P_DATE,
    p_hour as P_HOUR
    
FROM bk_nonrealtime_event
WHERE name = 'RecommendationRunComputed'
    AND IFF(
        '{{ partition_start_date }}' = '{{ partition_end_date }}',
        p_date = '{{ partition_start_date }}' AND p_hour >= {{ partition_start_hour }} AND p_hour < {{ partition_end_hour }},
        (
            (p_date = '{{ partition_start_date }}' AND p_hour >= {{ partition_start_hour }}) OR
            (p_date > '{{ partition_start_date }}' AND p_date < '{{ partition_end_date }}') OR
            (p_date = '{{ partition_end_date }}' AND p_hour < {{ partition_end_hour }})
        )
    );
