-- Clean up ALL consulting_slots RLS policies and recreate with simple working policies
-- This fixes the 400 error caused by conflicting policies using has_role() function

-- Drop ALL existing policies (including the problematic ones with has_role)
DROP POLICY IF EXISTS "Users see own slots" ON consulting_slots;
DROP POLICY IF EXISTS "Admin manage slots" ON consulting_slots;
DROP POLICY IF EXISTS "Users create own slots" ON consulting_slots;
DROP POLICY IF EXISTS "consulting_slots_select_own" ON consulting_slots;
DROP POLICY IF EXISTS "consulting_slots_insert_own" ON consulting_slots;
DROP POLICY IF EXISTS "consulting_slots_update_own" ON consulting_slots;
DROP POLICY IF EXISTS "consulting_slots_select_all" ON consulting_slots;
DROP POLICY IF EXISTS "consulting_slots_service_role" ON consulting_slots;

-- Create simple, working policies
-- Users can see their own consulting slots
CREATE POLICY "Users can view own consulting slots"
ON consulting_slots
FOR SELECT
TO authenticated
USING (auth.uid() = user_id);

-- Users can insert their own consulting slots
CREATE POLICY "Users can insert own consulting slots"
ON consulting_slots
FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);

-- Users can update their own consulting slots
CREATE POLICY "Users can update own consulting slots"
ON consulting_slots
FOR UPDATE
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);

-- Users can delete their own consulting slots
CREATE POLICY "Users can delete own consulting slots"
ON consulting_slots
FOR DELETE
TO authenticated
USING (auth.uid() = user_id);

-- Service role (for edge functions and admin operations) can do everything
CREATE POLICY "Service role full access"
ON consulting_slots
FOR ALL
TO service_role
USING (true)
WITH CHECK (true);
