-- Create a scheduled task to update the agg_play_info_hourly table
CREATE OR REPLACE TASK SUNO_PROD.PROD.AGG_PLAY_INFO_HOURLY_V0_INSERT
    WAREHOUSE = SUNO_PROD_AGG_HOURLY_LARGE
    after SUNO_PROD.PROD.RDS_DISCORD_INFO_HOURLY_REPLACE
AS declare
    p_date date;
    p_hour number;
    row_count number;
    start_time TIMESTAMP;
BEGIN
    start_time := CURRENT_TIMESTAMP();
    p_date := DATE(SYSDATE() - INTERVAL '2 HOUR');
    p_hour := HOUR(SYSDATE() - INTERVAL '2 HOUR');
    CALL agg_play_info_hourly_v0_proc(DATE(SYSDATE() - INTERVAL '1 HOUR'), HOUR(SYSDATE() - INTERVAL '1 HOUR'));
    SELECT COUNT(*)
        INTO :row_count
    FROM PROD.AGG_PLAY_INFO_HOURLY_V0
    WHERE p_date = :p_date and p_hour = :p_hour;
    call TASK_MONITOR_INSERT_PROC('AGG_PLAY_INFO_HOURLY_V0_INSERT', :start_time, :row_count, 'SUCCESS', 'HOURLY');
end;
