-- Fix the workflow step params to pass context as the second parameter
-- The processQuotationSubmission expects ($quotationId, $formData)
-- We need to pass the context object as the formData parameter

-- First, delete duplicate steps (keep only the first one)
DELETE FROM workflow_steps 
WHERE id IN (
    SELECT id 
    FROM workflow_steps 
    WHERE workflow_id = (SELECT id FROM workflows WHERE name = 'Process Quotation Submission')
    AND id NOT IN (
        SELECT MIN(id) 
        FROM workflow_steps 
        WHERE workflow_id = (SELECT id FROM workflows WHERE name = 'Process Quotation Submission')
    )
);

-- Update the remaining step with correct params
-- Cast config to jsonb, update it, then cast back to json
UPDATE workflow_steps 
SET config = (
    jsonb_set(
        config::jsonb, 
        '{params}', 
        '["{quotation_id}", "{context}"]'::jsonb
    )
)::json
WHERE workflow_id = (SELECT id FROM workflows WHERE name = 'Process Quotation Submission');

-- Verify the update
SELECT 
    ws.id, 
    ws.step_order, 
    ws.step_type, 
    ws.config,
    w.name as workflow_name
FROM workflow_steps ws
JOIN workflows w ON ws.workflow_id = w.id
WHERE w.name = 'Process Quotation Submission'
ORDER BY ws.step_order;
