-- Fix foreign key constraint issue
-- If document doesn't exist, we need to either:
-- 1. Create the document (if it was deleted)
-- 2. Remove the foreign key constraint temporarily
-- 3. Use a different approach

-- Option 1: Check if we should drop and recreate FK constraint
-- (This allows creating jobs even if document doesn't exist - useful for testing)

-- First, let's see the constraint
SELECT
    conname as constraint_name,
    conrelid::regclass as table_name,
    confrelid::regclass as foreign_table_name
FROM pg_constraint
WHERE conname = 'processing_jobs_document_id_fkey';

-- Option 2: Temporarily disable FK constraint (for testing only)
-- WARNING: Only do this if you understand the implications
-- ALTER TABLE processing_jobs DROP CONSTRAINT IF EXISTS processing_jobs_document_id_fkey;
-- Then recreate later with:
-- ALTER TABLE processing_jobs ADD CONSTRAINT processing_jobs_document_id_fkey 
--   FOREIGN KEY (document_id) REFERENCES documents(id) ON DELETE CASCADE;

-- Option 3: Create job without FK constraint (if document truly doesn't exist)
-- This is a workaround - the real fix is to ensure documents exist
INSERT INTO processing_jobs (
  document_id,
  user_id,
  status,
  attempts,
  max_attempts,
  options,
  created_at
) VALUES (
  '78359b58-762c-4a68-a8e4-17ce38580a8d'::uuid,
  'B00HiMnleGhGdJgQwbX2Ume01Z53',
  'pending',
  0,
  3,
  '{"strategy": "document_ai_agentic_rag"}'::jsonb,
  NOW()
)
ON CONFLICT DO NOTHING;

