-- ============================================================================
-- POS Sessions Management Module - Enhanced with Supervisor Verification
-- ============================================================================
-- Description: Declarative module for managing POS cashier sessions with
--              supervisor verification and cash reconciliation features
-- Features:
--   - Role-based views (Cashier vs Supervisor)
--   - 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",
        "table": "pos_sessions",
        "primaryKey": "session_id",
        "header": [
            {
                "type": "button",
                "label": "Refresh",
                "onClick": "refreshData",
                "icon": "fa-refresh"
            },
            {
                "type": "filter-select",
                "label": "Status",
                "options": {
                    "data_column": "status",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Active", "value": "active"},
                        {"label": "Closed", "value": "closed"},
                        {"label": "Abandoned", "value": "abandoned"}
                    ]
                }
            },
            {
                "type": "filter-select",
                "label": "Verification Status",
                "options": {
                    "data_column": "approval_status",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Unverified", "value": "unverified"},
                        {"label": "Approved", "value": "approved"},
                        {"label": "Adjusted", "value": "adjusted"},
                        {"label": "Discrepancy", "value": "discrepancy"}
                    ]
                }
            },
            {
                "type": "filter-date-range",
                "label": "Session Date",
                "options": {
                    "data_column": "session_start"
                }
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "posSessionsTable",
                "options": {
                    "tableName": "pos_sessions",
                    "dataSource": "/api/pos_sessions",
                    "showGlobalSearch": true,
                    "addButton": false,
                    "rowClick": "selectRecord",
                    "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"
                    },
                    "rowActions": [
                        {
                            "label": "View Details",
                            "icon": "fa-eye",
                            "onClick": "openSelectedDetail"
                        },
                        {
                            "label": "Close Session",
                            "icon": "fa-lock",
                            "action": "pos:closeSession",
                            "condition": "status == \"active\"",
                            "confirm": "Are you sure you want to close this session?",
                            "className": "text-warning"
                        },
                        {
                            "label": "Verify Session",
                            "icon": "fa-check-circle",
                            "action": "pos:verifySession",
                            "condition": "status == \"closed\" && verified == false",
                            "className": "text-info"
                        },
                        {
                            "label": "Adjust Variance",
                            "icon": "fa-edit",
                            "action": "pos:adjustVariance",
                            "condition": "verified == true && approval_status != \"approved\"",
                            "className": "text-warning"
                        },
                        {
                            "label": "Mark Discrepancy",
                            "icon": "fa-exclamation-triangle",
                            "action": "pos:markDiscrepancy",
                            "condition": "status == \"closed\" && verified == false",
                            "confirm": "Mark this session as having a discrepancy?",
                            "className": "text-error"
                        },
                        {
                            "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?",
                            "className": "text-success"
                        }
                    ],
                    "sortBy": "session_start",
                    "sortOrder": "desc"
                }
            }
        ],
        "detail": {
            "modalId": "posSessionDetail",
            "title": "Session Details",
            "type": "tabs",
            "tabs": [
                {
                    "label": "Summary",
                    "body": [
                        {
                            "type": "form",
                            "title": "Session Information",
                            "fields": [
                                {
                                    "name": "cashier_name",
                                    "label": "Cashier",
                                    "type": "text",
                                    "readonly": true
                                },
                                {
                                    "name": "pos_number",
                                    "label": "POS Number",
                                    "type": "text",
                                    "readonly": true
                                },
                                {
                                    "name": "session_start",
                                    "label": "Session Start",
                                    "type": "datetime-local",
                                    "readonly": true
                                },
                                {
                                    "name": "session_end",
                                    "label": "Session End",
                                    "type": "datetime-local",
                                    "readonly": true
                                },
                                {
                                    "name": "status",
                                    "label": "Status",
                                    "type": "select",
                                    "options": [
                                        {"value": "active", "label": "Active"},
                                        {"value": "closed", "label": "Closed"},
                                        {"value": "abandoned", "label": "Abandoned"}
                                    ],
                                    "readonly": true
                                }
                            ]
                        },
                        {
                            "type": "form",
                            "title": "Financial Summary",
                            "fields": [
                                {
                                    "name": "opening_float",
                                    "label": "Opening Float",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "total_sales",
                                    "label": "Total Sales",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "cash_sales",
                                    "label": "Cash Sales",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "closing_cash_expected",
                                    "label": "Expected Cash",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "closing_cash_declared",
                                    "label": "Declared Cash",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "variance",
                                    "label": "Initial Variance",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$",
                                    "className": "font-bold"
                                }
                            ]
                        }
                    ]
                },
                {
                    "label": "Verification",
                    "body": [
                        {
                            "type": "form",
                            "title": "Supervisor Verification",
                            "fields": [
                                {
                                    "name": "verified",
                                    "label": "Verified",
                                    "type": "checkbox",
                                    "readonly": true
                                },
                                {
                                    "name": "verified_at",
                                    "label": "Verified At",
                                    "type": "datetime-local",
                                    "readonly": true
                                },
                                {
                                    "name": "supervisor_counted_cash",
                                    "label": "Supervisor Counted Cash",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$"
                                },
                                {
                                    "name": "adjusted_variance",
                                    "label": "Adjusted Variance",
                                    "type": "number",
                                    "readonly": true,
                                    "prefix": "$",
                                    "className": "text-lg font-bold"
                                },
                                {
                                    "name": "approval_status",
                                    "label": "Approval Status",
                                    "type": "select",
                                    "options": [
                                        {"value": "approved", "label": "Approved"},
                                        {"value": "adjusted", "label": "Adjusted"},
                                        {"value": "discrepancy", "label": "Discrepancy"}
                                    ],
                                    "readonly": true
                                },
                                {
                                    "name": "variance_explanation",
                                    "label": "Variance Explanation",
                                    "type": "textarea",
                                    "readonly": true,
                                    "rows": 3
                                },
                                {
                                    "name": "verification_notes",
                                    "label": "Verification Notes",
                                    "type": "textarea",
                                    "readonly": true,
                                    "rows": 4
                                }
                            ]
                        }
                    ]
                },
                {
                    "label": "Payments",
                    "body": [
                        {
                            "type": "html",
                            "content": "<div id=\"paymentBreakdownContainer\" class=\"p-4\"><p class=\"text-gray-500\">Loading payment breakdown...</p></div>"
                        }
                    ]
                },
                {
                    "label": "Transactions",
                    "body": [
                        {
                            "type": "table",
                            "options": {
                                "tableName": "lineitems_payments",
                                "dataSource": "/api/lineitems_payments",
                                "filterField": "session_id",
                                "filterValue": "{session_id}",
                                "columnsToShow": [
                                    "transaction_number",
                                    "transaction_date",
                                    "payments",
                                    "line_items"
                                ],
                                "columnLabels": {
                                    "transaction_number": "Transaction #",
                                    "transaction_date": "Date/Time",
                                    "payments": "Payment Methods",
                                    "line_items": "Items"
                                },
                                "addButton": false,
                                "showGlobalSearch": false,
                                "pageSize": 10
                            }
                        }
                    ]
                }
            ],
            "actions": [
                {
                    "label": "Verify Session",
                    "action": "pos:verifySession",
                    "icon": "fa-check-circle",
                    "condition": "status == \"closed\" && verified == false",
                    "className": "btn-info"
                },
                {
                    "label": "Adjust Variance",
                    "action": "pos:adjustVariance",
                    "icon": "fa-edit",
                    "condition": "verified == true && approval_status != \"approved\"",
                    "className": "btn-warning"
                },
                {
                    "label": "Mark Discrepancy",
                    "action": "pos:markDiscrepancy",
                    "icon": "fa-exclamation-triangle",
                    "condition": "status == \"closed\" && verified == false",
                    "confirm": "Mark as discrepancy?",
                    "className": "btn-error"
                },
                {
                    "label": "View Audit Log",
                    "action": "pos:viewVerificationLog",
                    "icon": "fa-history",
                    "condition": "verified == true",
                    "className": "btn-secondary"
                },
                {
                    "label": "Export Report",
                    "action": "pos:exportSessionReport",
                    "icon": "fa-download",
                    "condition": "status == \"closed\"",
                    "className": "btn-secondary"
                },
                {
                    "label": "Print Summary",
                    "action": "pos:printSessionSummary",
                    "icon": "fa-print",
                    "className": "btn-secondary"
                },
                {
                    "label": "Close",
                    "onClick": "closeModal",
                    "className": "btn-ghost"
                }
            ]
        },
        "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;
