-- ============================================================================
-- Biometric Devices Module Definition (Declarative)
-- Date: 2025-11-17
-- ============================================================================
-- This module provides management interface for biometric attendance devices.
-- Uses ActionRegistry, ModalBuilder, and DataLoader for declarative UI.
--
-- Features:
-- - Device registration and management
-- - Online/offline status monitoring
-- - Device approval/disable controls
-- - Connection logs and audit trail
-- - No custom JavaScript required!
-- ============================================================================

INSERT INTO modules (
    module_id,
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    '019a8e0a-c0e5-7999-9d58-51fadd75895b'::uuid,
    'BiometricDevices',
    '{
        "title": "Biometric Devices",
        "modulePath": "biometric-devices",
        "dataSources": {
            "main": {
                "table": "biometric_devices",
                "orderBy": "createdAt DESC"
            },
            "logs": {
                "table": "biometric_device_logs",
                "orderBy": "createdAt DESC"
            },
            "branches": {
                "table": "branches",
                "displayField": "name",
                "valueField": "branch_id"
            }
        },
        "header": [
            {
                "type": "button",
                "label": "Register Device",
                "formId": 630
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "device_type",
                    "label": "Device Type",
                    "options": [
                        {"label": "All Types", "value": ""},
                        {"label": "F500 (Fingerprint)", "value": "F500"},
                        {"label": "M50 (Face + Temp)", "value": "M50"},
                        {"label": "M91 (Multi-modal)", "value": "M91"}
                    ]
                }
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "is_online",
                    "label": "Status",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Online", "value": "true"},
                        {"label": "Offline", "value": "false"}
                    ]
                }
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "approved",
                    "label": "Approval",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Approved", "value": "true"},
                        {"label": "Disabled", "value": "false"}
                    ]
                }
            },
            {
                "type": "button",
                "label": "Refresh",
                "icon": "fa-sync",
                "class": "btn btn-ghost",
                "action": "refresh",
                "ariaLabel": "Refresh device list"
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "biometricDevicesTable",
                "options": {
                    "tableName": "biometric_devices",
                    "primaryKeyField": "biometric_device_id",
                    "showGlobalSearch": true,
                    "columnsToShow": [
                        "device_sn",
                        "name",
                        "device_type",
                        "branch_id",
                        "is_online",
                        "last_seen",
                        "approved",
                        "createdAt"
                    ],
                    "columnLabels": {
                        "device_sn": "Serial Number",
                        "name": "Device Name",
                        "device_type": "Type",
                        "branch_id": "Branch",
                        "is_online": "Status",
                        "last_seen": "Last Seen",
                        "approved": "Approved",
                        "createdAt": "Registered"
                    },
                    "columnFormatters": {
                        "is_online": {
                            "type": "badge",
                            "true": {"label": "Online", "class": "badge-success"},
                            "false": {"label": "Offline", "class": "badge-error"}
                        },
                        "approved": {
                            "type": "badge",
                            "true": {"label": "Enabled", "class": "badge-success"},
                            "false": {"label": "Disabled", "class": "badge-warning"}
                        },
                        "branch_id": {
                            "type": "lookup",
                            "dataSource": "branches"
                        }
                    },
                    "rowClick": "selectRecord",
                    "rowActions": [
                        {
                            "label": "View Details",
                            "icon": "fa-eye",
                            "class": "btn btn-info btn-xs",
                            "action": "openSelectedDetail",
                            "ariaLabel": "View device details"
                        },
                        {
                            "label": "View Logs",
                            "icon": "fa-list",
                            "class": "btn btn-ghost btn-xs",
                            "action": "biometric:view-logs",
                            "ariaLabel": "View device connection logs"
                        },
                        {
                            "label": "Disable",
                            "icon": "fa-ban",
                            "class": "btn btn-warning btn-xs",
                            "action": "biometric:toggle-approval",
                            "condition": "approved == true",
                            "confirm": "Disable this device? It will not be able to connect.",
                            "ariaLabel": "Disable device"
                        },
                        {
                            "label": "Enable",
                            "icon": "fa-check",
                            "class": "btn btn-success btn-xs",
                            "action": "biometric:toggle-approval",
                            "condition": "approved == false",
                            "confirm": "Enable this device?",
                            "ariaLabel": "Enable device"
                        },
                        {
                            "label": "Delete",
                            "icon": "fa-trash",
                            "class": "btn btn-error btn-xs",
                            "action": "delete",
                            "confirm": "Delete this device? This cannot be undone.",
                            "ariaLabel": "Delete device"
                        }
                    ]
                }
            }
        ],
        "detailModal": {
            "title": "Device Details",
            "size": "large",
            "tabs": [
                {
                    "id": "info",
                    "label": "Device Information",
                    "icon": "fa-info-circle",
                    "sections": [
                        {
                            "title": "Basic Information",
                            "layout": "two-column",
                            "fields": [
                                {
                                    "name": "device_sn",
                                    "label": "Device Serial Number",
                                    "type": "text",
                                    "required": true,
                                    "placeholder": "Enter device serial number",
                                    "helpText": "Physical serial number from the device hardware"
                                },
                                {
                                    "name": "name",
                                    "label": "Device Name",
                                    "type": "text",
                                    "required": true,
                                    "placeholder": "e.g., Main Entrance Scanner"
                                },
                                {
                                    "name": "device_type",
                                    "label": "Device Model",
                                    "type": "select",
                                    "required": true,
                                    "options": [
                                        {"label": "F500 - Fingerprint Scanner", "value": "F500"},
                                        {"label": "M50 - Face Recognition + Temperature", "value": "M50"},
                                        {"label": "M91 - Multi-modal Biometric", "value": "M91"}
                                    ]
                                },
                                {
                                    "name": "branch_id",
                                    "label": "Branch Location",
                                    "type": "lookup",
                                    "required": true,
                                    "dataSource": "branches",
                                    "helpText": "Select the branch where this device is installed"
                                },
                                {
                                    "name": "cloud_id",
                                    "label": "Cloud ID",
                                    "type": "text",
                                    "placeholder": "Optional cloud service identifier"
                                },
                                {
                                    "name": "approved",
                                    "label": "Device Approved",
                                    "type": "checkbox",
                                    "default": true,
                                    "helpText": "Disabled devices cannot connect to the system"
                                }
                            ]
                        },
                        {
                            "title": "Description",
                            "layout": "single-column",
                            "fields": [
                                {
                                    "name": "description",
                                    "label": "Notes",
                                    "type": "textarea",
                                    "rows": 3,
                                    "placeholder": "Additional notes about this device..."
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "status",
                    "label": "Status & Activity",
                    "icon": "fa-chart-line",
                    "sections": [
                        {
                            "title": "Connection Status",
                            "layout": "two-column",
                            "fields": [
                                {
                                    "name": "is_online",
                                    "label": "Current Status",
                                    "type": "badge",
                                    "readonly": true,
                                    "formatter": {
                                        "type": "badge",
                                        "true": {"label": "Online", "class": "badge-success"},
                                        "false": {"label": "Offline", "class": "badge-error"}
                                    }
                                },
                                {
                                    "name": "last_seen",
                                    "label": "Last Seen",
                                    "type": "datetime",
                                    "readonly": true
                                },
                                {
                                    "name": "websocket_session",
                                    "label": "Session ID",
                                    "type": "text",
                                    "readonly": true,
                                    "helpText": "WebSocket connection identifier"
                                },
                                {
                                    "name": "createdAt",
                                    "label": "Registered On",
                                    "type": "datetime",
                                    "readonly": true
                                }
                            ]
                        },
                        {
                            "title": "Recent Activity Logs",
                            "component": "data-grid",
                            "dataSource": "logs",
                            "filters": {
                                "biometric_device_id": "{biometric_device_id}"
                            },
                            "limit": 10,
                            "columns": [
                                {
                                    "field": "event_type",
                                    "label": "Event",
                                    "width": "150px"
                                },
                                {
                                    "field": "createdAt",
                                    "label": "Time",
                                    "width": "180px",
                                    "formatter": "datetime"
                                },
                                {
                                    "field": "details",
                                    "label": "Details",
                                    "formatter": "json"
                                }
                            ]
                        }
                    ]
                },
                {
                    "id": "settings",
                    "label": "Device Settings",
                    "icon": "fa-cog",
                    "sections": [
                        {
                            "title": "Configuration",
                            "layout": "single-column",
                            "fields": [
                                {
                                    "name": "settings",
                                    "label": "Device Settings (JSON)",
                                    "type": "json-editor",
                                    "helpText": "Advanced device configuration in JSON format",
                                    "placeholder": "{}"
                                }
                            ]
                        }
                    ]
                }
            ],
            "actions": [
                {
                    "label": "Save",
                    "icon": "fa-save",
                    "class": "btn btn-primary",
                    "action": "save",
                    "closeOnSuccess": true
                },
                {
                    "label": "Cancel",
                    "class": "btn btn-ghost",
                    "action": "close"
                }
            ]
        },
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "onClick": "closeModal"
            }
        ]
    }'::jsonb,
    'fa-fingerprint',
    'Biometric Devices',
    'Manage biometric attendance devices and monitor connection status'
);

-- ============================================================================
-- Biometric Device Logs Module Definition
-- ============================================================================
-- Dedicated module for viewing detailed device logs and audit trail
-- ============================================================================

INSERT INTO modules (
    module_id,
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    '019a8e0a-c0f1-7690-a532-1d8575ae6252'::uuid,
    'BiometricDeviceLogs',
    '{
        "title": "Device Activity Logs",
        "modulePath": "biometric-device-logs",
        "dataSources": {
            "main": {
                "table": "biometric_device_logs",
                "orderBy": "createdAt DESC"
            },
            "devices": {
                "table": "biometric_devices",
                "displayField": "name",
                "valueField": "biometric_device_id"
            }
        },
        "header": [
            {
                "type": "filter-select",
                "options": {
                    "data_column": "biometric_device_id",
                    "label": "Device",
                    "dataSource": "devices",
                    "includeAll": true
                }
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "event_type",
                    "label": "Event Type",
                    "options": [
                        {"label": "All Events", "value": ""},
                        {"label": "Login Success", "value": "login_success"},
                        {"label": "Login Rejected", "value": "login_rejected"},
                        {"label": "Register Rejected", "value": "register_rejected"},
                        {"label": "Device Created", "value": "device_created"},
                        {"label": "Device Updated", "value": "device_updated"},
                        {"label": "Device Deleted", "value": "device_deleted"},
                        {"label": "Attendance Recorded", "value": "attendance_recorded"},
                        {"label": "Attendance Rejected", "value": "attendance_rejected"}
                    ]
                }
            },
            {
                "type": "filter-date-range",
                "options": {
                    "data_column": "createdAt",
                    "label": "Date Range"
                }
            },
            {
                "type": "button",
                "label": "Refresh",
                "icon": "fa-sync",
                "class": "btn btn-ghost",
                "action": "refresh"
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "deviceLogsTable",
                "options": {
                    "tableName": "biometric_device_logs",
                    "primaryKeyField": "biometric_device_log_id",
                    "showGlobalSearch": true,
                    "columnsToShow": [
                        "biometric_device_id",
                        "device_sn",
                        "event_type",
                        "createdAt",
                        "details"
                    ],
                    "columnLabels": {
                        "biometric_device_id": "Device",
                        "device_sn": "Serial Number",
                        "event_type": "Event Type",
                        "createdAt": "Timestamp",
                        "details": "Details"
                    },
                    "columnFormatters": {
                        "biometric_device_id": {
                            "type": "lookup",
                            "dataSource": "devices"
                        },
                        "details": {
                            "type": "json-preview"
                        },
                        "event_type": {
                            "type": "badge",
                            "mappings": {
                                "login_success": {"class": "badge-success"},
                                "login_rejected": {"class": "badge-error"},
                                "register_rejected": {"class": "badge-warning"},
                                "attendance_recorded": {"class": "badge-info"}
                            }
                        }
                    },
                    "rowClick": "selectRecord"
                }
            }
        ]
    }'::jsonb,
    'fa-list-check',
    'Device Activity Logs',
    'View biometric device connection logs and audit trail'
);
