-- ============================================================================
-- Invoices Form Submission Workflow
-- ============================================================================
-- This workflow calls the same PHP function as quotations but for invoices
-- It uses the CustomFunctionStep to execute QuotationsProcessor::processQuotationSubmission
-- The processor will detect it's an invoice and send as 'normal' sale to TaxCore
-- ============================================================================

-- Step 1: Create the workflow
INSERT INTO workflows (
    name, 
    trigger_event, 
    definition, 
    is_active, 
    tenant_id
)
VALUES (
    'Process Invoice Submission',
    'invoices.updated',  -- Trigger when invoices 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
-- Note: Same function as quotations, but context will include table='invoices'
INSERT INTO workflow_steps (
    workflow_id, 
    step_order, 
    step_type, 
    config
)
VALUES (
    (SELECT id FROM workflows WHERE name = 'Process Invoice 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
);

-- ============================================================================
-- 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 '%Invoice%';

-- 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 '%Invoice%'
-- ORDER BY ws.step_order;

-- ============================================================================
-- Notes
-- ============================================================================
-- 1. The workflow will be triggered when an invoice record is updated in the database
-- 2. The same QuotationsProcessor function handles both quotations and invoices
-- 3. The processor detects the table from context and automatically sets:
--    - quotations -> invoice_type: 'proforma' (Proforma sale)
--    - invoices -> invoice_type: 'normal' (Normal sale)
-- 4. Check logs in /storage/invoices/logs/ for processing details
-- 5. If you need to trigger on INSERT instead, add another workflow with trigger 'invoices.created'

COMMENT ON TABLE workflows IS 'Invoice workflow added - calls QuotationsProcessor with normal sale type';
