-- Remove priority system entirely

-- Drop the priority calculation function
DROP FUNCTION IF EXISTS public.calculate_submission_priority(UUID);

-- Drop the existing claim_next_submission function completely
DROP FUNCTION IF EXISTS public.claim_next_submission(UUID, INTERVAL);

-- Recreate claim_next_submission without priority logic
CREATE OR REPLACE FUNCTION public.claim_next_submission(
  moderator_id UUID,
  lock_duration INTERVAL DEFAULT '15 minutes'
)
RETURNS TABLE (
  submission_id UUID,
  submission_type TEXT,
  waiting_time INTERVAL
)
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public
AS $$
BEGIN
  -- Simple FIFO with escalation priority
  RETURN QUERY
  UPDATE content_submissions
  SET 
    assigned_to = moderator_id,
    assigned_at = NOW(),
    locked_until = NOW() + lock_duration,
    first_reviewed_at = COALESCE(first_reviewed_at, NOW())
  WHERE id = (
    SELECT cs.id FROM content_submissions cs
    WHERE cs.status IN ('pending', 'partially_approved')
      AND (cs.assigned_to IS NULL OR cs.locked_until < NOW())
    ORDER BY 
      cs.escalated DESC,        -- Escalated items first
      cs.submitted_at ASC        -- Then oldest first
    LIMIT 1
    FOR UPDATE SKIP LOCKED
  )
  RETURNING 
    content_submissions.id,
    content_submissions.submission_type,
    NOW() - content_submissions.submitted_at;
END;
$$;

-- Drop the priority column from content_submissions
ALTER TABLE public.content_submissions DROP COLUMN IF EXISTS priority;