DO $$
BEGIN
  -- Check specifically in the 'public' schema
  IF EXISTS (
    SELECT 1 FROM information_schema.tables 
    WHERE table_schema = 'public' AND table_name = 'task_activity_logs'
  ) THEN
  
    -- Enable RLS (idempotent)
    EXECUTE 'ALTER TABLE IF EXISTS public.task_activity_logs ENABLE ROW LEVEL SECURITY';

    -- Drop any old policy and recreate permissive insert policy for authenticated role
    EXECUTE 'DROP POLICY IF EXISTS allow_authenticated_inserts ON public.task_activity_logs';
    
    -- CREATE POLICY for INSERT strictly uses WITH CHECK
    EXECUTE 'CREATE POLICY allow_authenticated_inserts ON public.task_activity_logs 
             FOR INSERT TO authenticated WITH CHECK (true)';
             
  END IF;
END
$$;