-- ============================================================================
-- Migration: Add Quick Quantity Entry to Inventory Management Module
-- ============================================================================
-- Description: Updates InventoryManagement module to include Quick Quantity Entry button
-- Author: AI Assistant
-- Date: November 24, 2025
-- ============================================================================

-- Update InventoryManagement module configuration
UPDATE modules 
SET config = '{
    "title": "Inventory Management",
    "header": [],
    "body": [
        {
            "type": "tabs",
            "tabs": [
                {
                    "label": "Receive",
                    "icon": "fa-arrow-down",
                    "body": [
                        {
                            "type": "button",
                            "label": "Receive Inventory",
                            "icon": "fa-plus",
                            "class": "btn btn-primary mb-4",
                            "formName": "inventory_receive_form"
                        },
                        {
                            "type": "table",
                            "options": {
                                "tableName": "inventory_receive",
                                "showGlobalSearch": true,
                                "addButton": false,
                                "columnsToShow": ["delivery_note_number", "vendor", "branch", "lpo_number", "invoice_number", "truck", "date"],
                                "rowActions": [
                                    {
                                        "label": "View",
                                        "icon": "fa-eye",
                                        "onClick": "viewInventoryReceive"
                                    },
                                    {
                                        "label": "Edit",
                                        "icon": "fa-edit",
                                        "onClick": "editInventoryReceive"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    "label": "Stocks",
                    "icon": "fa-warehouse",
                    "body": [
                        {
                            "type": "button",
                            "label": "Quick Quantity Entry",
                            "icon": "fa-bolt",
                            "class": "btn btn-primary",
                            "action": "inventory:quick-quantity"
                        },
                        {
                            "type": "button",
                            "label": "Adjust Stock",
                            "icon": "fa-edit",
                            "class": "btn btn-warning",
                            "formName": "inventory_adjustment_form"
                        },
                        {
                            "type": "button",
                            "label": "Transfer",
                            "icon": "fa-exchange-alt",
                            "class": "btn btn-secondary",
                            "formName": "inventory_transfer_form"
                        },
                        {
                            "type": "button",
                            "label": "Break Bulk",
                            "icon": "fa-cut",
                            "class": "btn btn-secondary",
                            "action": "inventory:break"
                        },
                        {
                            "type": "button",
                            "label": "Export",
                            "icon": "fa-download",
                            "class": "btn btn-secondary",
                            "onClick": "exportInventory"
                        },
                        {
                            "type": "table",
                            "options": {
                                "tableName": "product_quantity_view",
                                "showGlobalSearch": true,
                                "addButton": false,
                                "editFormName": "inventory_adjustment_form"
                            }
                        }
                    ]
                },
                {
                    "label": "Transfers",
                    "icon": "fa-exchange-alt",
                    "body": [
                        {
                            "type": "button",
                            "label": "New Transfer",
                            "icon": "fa-plus",
                            "class": "btn btn-primary mb-4",
                            "formName": "inventory_transfer_form"
                        },
                        {
                            "type": "table",
                            "options": {
                                "tableName": "inventory_transfers",
                                "showGlobalSearch": true,
                                "addButton": false,
                                "columnsToShow": ["reference_number", "transfer_date", "status"],
                                "rowActions": [
                                    {
                                        "label": "Complete Transfer",
                                        "icon": "fa-check",
                                        "confirm": "Complete this transfer? This will automatically move stock between branches.",
                                        "update": {
                                            "status": "completed"
                                        },
                                        "condition": "row.status === ''pending''"
                                    }
                                ]
                            }
                        }
                    ]
                },
                {
                    "label": "Warehouses",
                    "icon": "fa-building",
                    "body": [
                        {
                            "type": "button",
                            "label": "Add Warehouse",
                            "icon": "fa-plus",
                            "class": "btn btn-primary mb-4",
                            "formName": "warehouse_form"
                        },
                        {
                            "type": "table",
                            "options": {
                                "tableName": "branches",
                                "showGlobalSearch": true,
                                "addButton": false,
                                "editFormName": "warehouse_form",
                                "columnsToShow": ["name", "code", "type", "address"],
                                "rowActions": [
                                    {
                                        "label": "View Stock",
                                        "icon": "fa-boxes",
                                        "onClick": "viewWarehouseStock"
                                    },
                                    {
                                        "label": "Deactivate",
                                        "icon": "fa-ban",
                                        "onClick": "deactivateWarehouse"
                                    }
                                ]
                            }
                        }
                    ]
                }
            ]
        }
    ],
    "footer": [
        {
            "type": "button",
            "label": "Close",
            "onClick": "closeModal"
        }
    ]
}'::jsonb
WHERE module_id = '0194a8b2-0016-7123-8123-123456789abc'::uuid
  AND name = 'InventoryManagement';

-- Verify the update
DO $$
DECLARE
    v_config jsonb;
    v_has_quick_entry boolean;
BEGIN
    SELECT config INTO v_config
    FROM modules
    WHERE module_id = '0194a8b2-0016-7123-8123-123456789abc'::uuid;
    
    -- Check if Quick Quantity Entry button exists
    SELECT EXISTS (
        SELECT 1
        FROM jsonb_array_elements(v_config->'body'->0->'tabs') AS tab
        CROSS JOIN jsonb_array_elements(tab->'body') AS element
        WHERE element->>'action' = 'inventory:quick-quantity'
    ) INTO v_has_quick_entry;
    
    IF v_has_quick_entry THEN
        RAISE NOTICE 'SUCCESS: Quick Quantity Entry button added to Inventory Management module';
    ELSE
        RAISE WARNING 'WARNING: Quick Quantity Entry button not found in module config';
    END IF;
END $$;

COMMENT ON COLUMN modules.config IS 'Module configuration - Updated Nov 24, 2025: Added Quick Quantity Entry feature to InventoryManagement';
