-- Complete RLS Policy Fix for All Tables
-- Run this in your Supabase SQL Editor

-- ============================================================
-- FIX 1: Allow INSERT to species table
-- ============================================================

-- Drop existing INSERT policies if any
DROP POLICY IF EXISTS "Allow service role to insert species" ON public.species;
DROP POLICY IF EXISTS "Allow inserts to species" ON public.species;
DROP POLICY IF EXISTS "Anyone can insert species" ON public.species;

-- Create new policy to allow INSERT operations
-- This allows the API (using anon key) to insert new species
CREATE POLICY "Allow authenticated inserts to species"
  ON public.species FOR INSERT
  WITH CHECK (true);

-- ============================================================
-- FIX 2: Allow INSERT to species_submissions table
-- ============================================================

-- Drop existing INSERT policies if any
DROP POLICY IF EXISTS "Allow inserts to submissions" ON public.species_submissions;

-- The schema already has this policy, but let's make sure it exists
-- CREATE POLICY "Anyone can submit new species"
--   ON public.species_submissions FOR INSERT
--   WITH CHECK (true);
-- (This should already exist from your schema)

-- ============================================================
-- Verify all policies
-- ============================================================

SELECT 
    schemaname, 
    tablename, 
    policyname, 
    permissive,
    cmd as operation,
    qual as using_expression,
    with_check
FROM pg_policies
WHERE tablename IN ('species', 'species_submissions', 'recognition_logs')
ORDER BY tablename, policyname;
