-- Scheduled publishing
ALTER TABLE podcasts ADD COLUMN published_at timestamptz;

-- Update the SELECT policy: podcasts are visible if:
-- 1. Not scheduled (published_at IS NULL)
-- 2. Scheduled time has passed (published_at <= now())
-- 3. Current user is the creator
-- Note: drop the existing select policy and recreate it

-- First find and drop existing select policies on podcasts
DO $$
DECLARE
  pol RECORD;
BEGIN
  FOR pol IN
    SELECT policyname FROM pg_policies
    WHERE tablename = 'podcasts' AND cmd = 'SELECT'
  LOOP
    EXECUTE format('DROP POLICY %I ON podcasts', pol.policyname);
  END LOOP;
END $$;

CREATE POLICY "Podcasts visible when published or owned"
  ON podcasts FOR SELECT
  USING (
    published_at IS NULL
    OR published_at <= now()
    OR creator_id = auth.uid()
  );
