-- ============================================================================
-- Quotations Form Submission Workflow
-- ============================================================================
-- This workflow calls a custom PHP function when the Quotations form is saved
-- It uses the CustomFunctionStep to execute QuotationsProcessor::processQuotationSubmission
-- ============================================================================

-- Step 1: Create the workflow
-- Note: The trigger_event should match the form submission event
-- For forms, this is typically 'form.{form_id}.updated' or 'quotations.updated'
-- Adjust the trigger_event based on your actual form ID or table event

INSERT INTO workflows (
    name, 
    trigger_event, 
    definition, 
    is_active, 
    tenant_id
)
VALUES (
    'Process Quotation Submission',
    'quotations.updated',  -- Trigger when quotations table is updated
    '{"conditions":[]}',
    TRUE,
    NULL  -- NULL for global workflow, or specify tenant_id for tenant-specific
)
ON CONFLICT (name) DO UPDATE SET
    trigger_event = EXCLUDED.trigger_event,
    definition = EXCLUDED.definition,
    is_active = EXCLUDED.is_active,
    updatedat = now();

-- Step 2: Create workflow step to call the custom function
INSERT INTO workflow_steps (
    workflow_id, 
    step_order, 
    step_type, 
    config
)
VALUES (
    (SELECT id FROM workflows WHERE name = 'Process Quotation Submission' LIMIT 1),
    1,
    'custom_function',
    '{
        "class": "App\\\\Services\\\\QuotationsProcessor",
        "function": "processQuotationSubmission",
        "file": "/var/www/html/TAF/api/services/QuotationsProcessor.php",
        "params": ["{id}", "{context}"],
        "result_key": "processing_result"
    }'::jsonb
);

-- ============================================================================
-- Alternative: If using form-based trigger
-- ============================================================================
-- If you want to trigger based on a specific form submission instead of table updates,
-- uncomment and adjust the following:

/*
-- First, find your quotations form ID:
-- SELECT id, name, target_table FROM forms WHERE name ILIKE '%quotation%' OR target_table = 'quotations';

-- Then create a form-specific workflow:
INSERT INTO workflows (
    name, 
    trigger_event, 
    definition, 
    is_active, 
    tenant_id
)
VALUES (
    'Process Quotation Form Submission',
    'form.YOUR_FORM_ID.updated',  -- Replace YOUR_FORM_ID with actual form ID
    '{"conditions":[]}',
    TRUE,
    NULL
)
ON CONFLICT (name) DO UPDATE SET
    trigger_event = EXCLUDED.trigger_event,
    definition = EXCLUDED.definition,
    is_active = EXCLUDED.is_active,
    updatedat = now();

-- Add the custom function step:
INSERT INTO workflow_steps (
    workflow_id, 
    step_order, 
    step_type, 
    config
)
VALUES (
    (SELECT id FROM workflows WHERE name = 'Process Quotation Form Submission' LIMIT 1),
    1,
    'custom_function',
    '{
        "class": "App\\\\Services\\\\QuotationsProcessor",
        "function": "processQuotationSubmission",
        "file": "/var/www/html/TAF/api/services/QuotationsProcessor.php",
        "params": ["{id}", "{form_data}"],
        "result_key": "processing_result"
    }'::jsonb
);

-- Link the workflow to the form (if applicable):
UPDATE forms
SET on_submit_workflow_id = (
    SELECT id FROM workflows WHERE name = 'Process Quotation Form Submission' LIMIT 1
)
WHERE id = YOUR_FORM_ID;  -- Replace YOUR_FORM_ID with actual form ID
*/

-- ============================================================================
-- Verification Queries
-- ============================================================================
-- Use these queries to verify the workflow was created correctly:

-- Check workflows:
-- SELECT id, name, trigger_event, is_active, description FROM workflows WHERE name LIKE '%Quotation%';

-- Check workflow steps:
-- 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 LIKE '%Quotation%'
-- ORDER BY ws.step_order;

-- ============================================================================
-- Notes
-- ============================================================================
-- 1. The workflow will be triggered when a quotation record is updated in the database
-- 2. The custom function will receive the quotation ID and context data
-- 3. Check logs for: "QuotationsProcessor::processQuotationSubmission called for quotation ID: X"
-- 4. Modify the processQuotationSubmission function in QuotationsProcessor.php to add your business logic
-- 5. If you need to trigger on INSERT instead, add another workflow with trigger 'quotations.created'

COMMENT ON TABLE workflows IS 'Quotations workflow added - calls custom PHP function on quotation save';
