INSERT INTO {{ fact_hook_play_table_name }} (
    -- Primary key and identifiers
    id
    , user_id
    , user_uid
    , hook_id
    , hook_session_id
    , clip_id
    -- Playback
    , play_start_ts_utc
    , play_end_ts_utc
    , hook_session_duration_seconds
    , play_duration_seconds
    , unique_play_duration_seconds
    , unique_play_percent
    , hook_start_reason
    , hook_end_reason
    , is_play_continuous
    , repeat_count
    , hook_player_events
    -- Hook info
    , hook_duration_seconds
    -- Clip info
    , clip_duration_seconds
    , is_user_song_owner
    -- Context info
    , previous_hook_session_id
    , previous_clip_session_id
    , context_type
    , context_id
    , recommendation_item_id
    , hook_player_metadata
    -- Device metadata
    , session_id
    , platform
    , app_version
    , is_mobile
    , country
    , city
    -- Partition fields
    , p_date
    , p_hour
    , is_high_intent_listening
)
SELECT
    -- Primary key and identifiers
    id
    , user_id
    , user_uid
    , hook_id
    , hook_session_id
    , clip_id
    -- Playback
    , play_start_ts_utc
    , play_end_ts_utc
    , hook_session_duration_seconds
    , play_duration_seconds
    , unique_play_duration_seconds
    , unique_play_percent
    , hook_start_reason
    , hook_end_reason
    , is_play_continuous
    , repeat_count
    , hook_player_events
    -- Hook info
    , hook_duration_seconds
    -- Clip info
    , clip_duration_seconds
    , is_user_song_owner
    -- Context info
    , previous_hook_session_id
    , previous_clip_session_id
    , context_type
    , context_id
    , recommendation_item_id
    , hook_player_metadata
    -- Device metadata
    , session_id
    , platform
    , app_version
    , is_mobile
    , country
    , city
    -- Partition fields
    , p_date
    , p_hour
    , is_high_intent_listening
FROM {{ stg_fact_hook_play_table_name }}
WHERE 1=1
    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 }})
        )
    )
