-- Debug foreign key constraint and document existence

-- 1. Check if document exists (bypassing RLS with service role context)
SELECT id, user_id, status 
FROM documents 
WHERE id = '78359b58-762c-4a68-a8e4-17ce38580a8d'::uuid;

-- 2. Check foreign key constraint definition
SELECT
    tc.constraint_name,
    tc.table_name,
    kcu.column_name,
    ccu.table_name AS foreign_table_name,
    ccu.column_name AS foreign_column_name,
    tc.constraint_type
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
  ON tc.constraint_name = kcu.constraint_name
  AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
  ON ccu.constraint_name = tc.constraint_name
  AND ccu.table_schema = tc.table_schema
WHERE tc.constraint_type = 'FOREIGN KEY'
  AND tc.table_name = 'processing_jobs'
  AND kcu.column_name = 'document_id';

-- 3. Check if document exists in different ways
-- Direct query (should work with SECURITY DEFINER)
DO $$
DECLARE
  v_doc_id UUID := '78359b58-762c-4a68-a8e4-17ce38580a8d'::uuid;
  v_exists BOOLEAN;
BEGIN
  SELECT EXISTS(
    SELECT 1 FROM documents WHERE id = v_doc_id
  ) INTO v_exists;
  
  RAISE NOTICE 'Document exists: %', v_exists;
  
  IF NOT v_exists THEN
    RAISE NOTICE 'Document does not exist in database!';
    RAISE NOTICE 'This explains the foreign key constraint failure.';
  END IF;
END $$;

-- 4. Check table schema
SELECT 
  table_name,
  column_name,
  data_type,
  is_nullable
FROM information_schema.columns
WHERE table_name = 'documents'
  AND column_name = 'id'
ORDER BY ordinal_position;

