-- 'NEW' in this context is a segment row.
-- we update this function to also set the vod status to 'recording' if applicable.

CREATE OR REPLACE FUNCTION update_vod_on_segment_update()
RETURNS TRIGGER AS $$
BEGIN
  UPDATE api.vods
  SET 
    updated_at = NOW(),
    status = CASE 
      WHEN NEW.filesize > OLD.filesize THEN 'recording'
      ELSE status
    END
  WHERE id IN (
    SELECT vod_id 
    FROM segments_vod_links 
    WHERE segment_id = NEW.id
  );
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;