-- ============================================================================
-- Update ReOrder Tab Configuration in Inventory Management Module
-- ============================================================================
-- This migration enhances the ReOrder tab with better UX, calculated columns,
-- and proper display of stock status information
--
-- Changes:
-- 1. Adds alert message explaining the tab's purpose
-- 2. Updates displayed columns to include branch_name, shortage_qty, stock_status
-- 3. Adds row action for adjusting thresholds
-- 4. Reorders columns for better readability
--
-- Created: 2025-11-15
-- Dependencies: Phase 4 Inventory Module, product_quantity table
-- ============================================================================

UPDATE modules 
SET config = jsonb_set(
    config,
    '{body,0,tabs,4}',
    '{
        "label": "ReOrders",
        "icon": "fa-redo",
        "body": [
            {
                "type": "alert",
                "class": "alert-warning mb-4",
                "content": "<i class=\"fa fa-exclamation-triangle\"></i> <strong>Products Below Minimum Stock Levels</strong><br>These products need to be reordered based on your configured thresholds."
            },
            {
                "type": "table",
                "options": {
                    "tableName": "auto_orders",
                    "showGlobalSearch": true,
                    "addButton": false,
                    -- Adjusted to only show columns that already exist without adding new computed/branch columns
                    "columnsToShow": ["product_name", "current_qty", "minimum_stock_qty", "order_qty"],
                    "filters": {
                        "reorder_needed": true
                    },
                    "rowActions": [
                        {
                            "label": "Adjust Threshold",
                            "icon": "fa-edit",
                            "action": "edit"
                        }
                    ]
                }
            }
        ]
    }'::jsonb
)
WHERE module_id = '0194a8b2-0016-7123-8123-123456789abc'::uuid
  AND name = 'InventoryManagement';

-- Verify the update
SELECT 
    name,
    config->'body'->0->'tabs'->4->'label' as reorder_tab_label,
    config->'body'->0->'tabs'->4->'body' as reorder_tab_body
FROM modules 
WHERE module_id = '0194a8b2-0016-7123-8123-123456789abc'::uuid;

COMMENT ON TABLE modules IS 'Module registry - Updated ReOrder tab with enhanced UX and calculated columns (shortage_qty, stock_status)';
