-- ============================================================================
-- POS Sessions Management Module - Enhanced with Supervisor Verification
-- ============================================================================
-- Description: Declarative module for managing POS cashier sessions with
--              supervisor verification and cash reconciliation features
-- Features:
--   - Tabbed interface matching Case Management pattern
--   - Session verification workflow
--   - Cash reconciliation with variance tracking
--   - Payment breakdown display
--   - Verification audit trail
--   - Conditional row actions based on session status and verification state
-- Author: AI Assistant
-- Date: November 14, 2025
-- ============================================================================

UPDATE modules 
SET
    name = 'POS Sessions Management',
    config = '{
        "title": "POS Sessions Management",
        "header": [
            {
                "type": "button",
                "label": "Refresh",
                "onClick": "refreshData",
                "icon": "fa-refresh"
            },
            {
                "type": "button",
                "label": "Combine Sessions",
                "action": "pos:combineSessions",
                "icon": "fa-object-group",
                "requiresSelection": true,
                "minSelection": 2
            },
            {
                "type": "button",
                "label": "Verify Session",
                "onClick": "verifySelectedSession",
                "icon": "fa-check-circle"
            },
            {
                "type": "button",
                "label": "Export Report",
                "onClick": "exportSessionReport",
                "icon": "fa-download"
            }
        ],
        "body": [
            {
                "type": "tabs",
                "tabs": [
                    {
                        "label": "Sessions",
                        "elements": [
                            {
                                "type": "table",
                                "options": {
                                    "tableName": "pos_sessions",
                                    "showGlobalSearch": true,
                                    "addButton": false,
                                    "selectionMode": "multiple",
                                    "columnsToShow": [
                                        "cashier_name",
                                        "pos_number",
                                        "session_start",
                                        "session_end",
                                        "status",
                                        "opening_float",
                                        "closing_cash_expected",
                                        "supervisor_counted_cash",
                                        "adjusted_variance",
                                        "approval_status",
                                        "verified"
                                    ],
                                    "columnLabels": {
                                        "cashier_name": "Cashier",
                                        "pos_number": "POS",
                                        "session_start": "Start Time",
                                        "session_end": "End Time",
                                        "status": "Status",
                                        "opening_float": "Opening Float",
                                        "closing_cash_expected": "Expected Cash",
                                        "supervisor_counted_cash": "Supervisor Count",
                                        "adjusted_variance": "Variance",
                                        "approval_status": "Approval",
                                        "verified": "Verified"
                                    },
                                    "columnFormatters": {
                                        "opening_float": "currency",
                                        "closing_cash_expected": "currency",
                                        "supervisor_counted_cash": "currency",
                                        "adjusted_variance": "varianceCurrency",
                                        "status": "statusBadge",
                                        "approval_status": "approvalBadge",
                                        "verified": "boolean",
                                        "session_start": "datetime",
                                        "session_end": "datetime"
                                    },
                                    "filterField": "session_id",
                                    "listenColumn": "session_id",
                                    "primaryKeyField": "session_id",
                                    "rowActions": [
                                        {
                                            "label": "Select",
                                            "onClick": "selectSessionForManagement"
                                        },
                                        {
                                            "label": "Close Session",
                                            "icon": "fa-lock",
                                            "action": "pos:closeSession",
                                            "condition": "status == \"active\"",
                                            "confirm": "Are you sure you want to close this session?"
                                        },
                                        {
                                            "label": "Verify Session",
                                            "icon": "fa-check-circle",
                                            "action": "pos:verifySession",
                                            "condition": "status == \"closed\" && verified == false"
                                        },
                                        {
                                            "label": "Adjust Variance",
                                            "icon": "fa-edit",
                                            "action": "pos:adjustVariance",
                                            "condition": "verified == true && approval_status != \"approved\""
                                        },
                                        {
                                            "label": "Mark Discrepancy",
                                            "icon": "fa-exclamation-triangle",
                                            "action": "pos:markDiscrepancy",
                                            "condition": "status == \"closed\" && verified == false",
                                            "confirm": "Mark this session as having a discrepancy?"
                                        },
                                        {
                                            "label": "View Payments",
                                            "icon": "fa-money",
                                            "action": "pos:viewPayments",
                                            "condition": "status == \"closed\""
                                        },
                                        {
                                            "label": "View Audit Log",
                                            "icon": "fa-history",
                                            "action": "pos:viewVerificationLog",
                                            "condition": "verified == true"
                                        },
                                        {
                                            "label": "Export Report",
                                            "icon": "fa-download",
                                            "action": "pos:exportSessionReport",
                                            "condition": "status == \"closed\""
                                        },
                                        {
                                            "label": "Reopen Session",
                                            "icon": "fa-unlock",
                                            "action": "pos:reopenSession",
                                            "condition": "status == \"closed\" && verified == false",
                                            "confirm": "Are you sure you want to reopen this session?"
                                        }
                                    ],
                                    "bulkActions": [
                                        {
                                            "label": "Combine Sessions",
                                            "icon": "fa-object-group",
                                            "action": "pos:combineSessions",
                                            "confirm": "Combine {count} selected sessions into a single active session?",
                                            "minSelection": 2
                                        }
                                    ],
                                    "sortBy": "session_start",
                                    "sortOrder": "desc"
                                }
                            }
                        ]
                    },
                    {
                        "label": "Summary",
                        "elements": [
                            {
                                "type": "detail-view",
                                "viewId": 1,
                                "filterField": "session_id",
                                "tableName": "pos_sessions"
                            }
                        ]
                    },
                    {
                        "label": "Verification",
                        "elements": [
                            {
                                "type": "table",
                                "options": {
                                    "tableName": "pos_session_verification_log",
                                    "showGlobalSearch": false,
                                    "addButton": false,
                                    "columnsToShow": [
                                        "verified_at",
                                        "verified_by_name",
                                        "counted_cash_before",
                                        "counted_cash_after",
                                        "variance_before",
                                        "variance_after",
                                        "approval_status",
                                        "notes"
                                    ],
                                    "columnLabels": {
                                        "verified_at": "Verified At",
                                        "verified_by_name": "Verified By",
                                        "counted_cash_before": "Counted Cash (Before)",
                                        "counted_cash_after": "Counted Cash (After)",
                                        "variance_before": "Variance (Before)",
                                        "variance_after": "Variance (After)",
                                        "approval_status": "Status",
                                        "notes": "Notes"
                                    },
                                    "columnFormatters": {
                                        "verified_at": "datetime",
                                        "counted_cash_before": "currency",
                                        "counted_cash_after": "currency",
                                        "variance_before": "varianceCurrency",
                                        "variance_after": "varianceCurrency",
                                        "approval_status": "approvalBadge"
                                    },
                                    "filterField": "session_id",
                                    "listenColumn": "session_id"
                                }
                            }
                        ]
                    },
                    {
                        "label": "Payments",
                        "elements": [
                            {
                                "type": "html",
                                "content": "<div id=\"paymentBreakdownContainer\" class=\"p-4\"><p class=\"text-gray-500\">Select a session to view payment breakdown...</p></div>"
                            }
                        ]
                    },
                    {
                        "label": "Transactions",
                        "elements": [
                            {
                                "type": "table",
                                "options": {
                                    "tableName": "sales_receipts",
                                    "showGlobalSearch": false,
                                    "addButton": false,
                                    "columnsToShow": [
                                        "receipt_number",
                                        "date",
                                        "totalamount",
                                        "payment_method",
                                        "customer_name"
                                    ],
                                    "columnLabels": {
                                        "receipt_number": "Receipt #",
                                        "date": "Date/Time",
                                        "totalamount": "Total",
                                        "payment_method": "Payment Method",
                                        "customer_name": "Customer"
                                    },
                                    "columnFormatters": {
                                        "date": "datetime",
                                        "totalamount": "currency"
                                    },
                                    "filterField": "session_id",
                                    "listenColumn": "session_id"
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "onClick": "closeModal"
            }
        ]
    }'::jsonb,
    org_id = NULL,
    branch_id = NULL,
    icon = 'fa-cash-register',
    aria_label = 'POS Sessions Management',
    description = 'View and manage POS cashier sessions with supervisor verification',
    updatedAt = NOW()
WHERE module_id = '0194d8e0-7890-7abc-def0-123456789abc'::uuid;
