-- Create job without FK constraint check (temporary workaround)
-- This disables FK validation temporarily, creates job, then re-enables

-- Step 1: Disable FK constraint temporarily
ALTER TABLE processing_jobs 
DROP CONSTRAINT IF EXISTS processing_jobs_document_id_fkey;

-- Step 2: Create the job
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()
)
RETURNING id, document_id, status, created_at;

-- Step 3: Recreate FK constraint (but make it DEFERRABLE so it checks later)
ALTER TABLE processing_jobs
ADD CONSTRAINT processing_jobs_document_id_fkey
FOREIGN KEY (document_id) 
REFERENCES public.documents(id) 
ON DELETE CASCADE
DEFERRABLE INITIALLY DEFERRED;

-- Note: DEFERRABLE INITIALLY DEFERRED means FK is checked at end of transaction
-- This allows creating jobs even if document visibility is temporarily blocked

-- Step 4: Verify job was created
SELECT 
  id,
  document_id,
  status,
  created_at
FROM processing_jobs
WHERE document_id = '78359b58-762c-4a68-a8e4-17ce38580a8d'::uuid
ORDER BY created_at DESC;

