-- Migration: Add Assigned To and Date Range filters to Task Manager
-- Date: 2025-12-21
-- Description: Updates Task Manager module to include assignee filter and due date from/to filters

UPDATE modules
SET config = $json$
{
  "title": "Task Manager",
  "header": [
    {
      "type": "button",
      "label": "New Task",
      "formId": 200
    },
    {
      "type": "filter-select",
      "options": {
        "source_table": "users",
        "source_column": "user_id",
        "display_column": "username",
        "data_column": "assignee_id",
        "placeholder": "Filter by Assigned To...",
        "search_fields": ["username", "email"],
        "max_results": 25,
        "debounce_ms": 250
      }
    },
    {
      "type": "date",
      "label": "Due Date From",
      "id": "due_date_from",
      "data_column": "due_date_from"
    },
    {
      "type": "date",
      "label": "Due Date To",
      "id": "due_date_to",
      "data_column": "due_date_to"
    },
    {
      "type": "filter-select",
      "options": {
        "data_column": "status",
        "options": [
          {"label": "All", "value": ""},
          {"label": "Pending", "value": "pending"},
          {"label": "In Progress", "value": "in_progress"},
          {"label": "Completed", "value": "completed"},
          {"label": "Cancelled", "value": "cancelled"}
        ]
      }
    },
    {
      "type": "filter-select",
      "options": {
        "data_column": "priority",
        "options": [
          {"label": "All", "value": ""},
          {"label": "Low", "value": "low"},
          {"label": "Medium", "value": "medium"},
          {"label": "High", "value": "high"},
          {"label": "Critical", "value": "critical"}
        ]
      }
    }
  ],
  "body": [
    {
      "type": "tabs",
      "tabs": [
        {
          "label": "My Tasks",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "generic_tasks",
                "showGlobalSearch": true,
                "addButton": false,
                "editFormId": 203,
                "filterField": ["assignee_id", "status", "priority", "due_date_from", "due_date_to"],
                "scope": "user",
                "userField": "assignee_id",
                "columnsToShow": ["title", "due_date", "hours", "priority", "status", "module_id"],
                "columnLabels": {
                  "module_id": "Module"
                },
                "sortBy": "due_date",
                "sortDir": "ASC",
                "rowClick": "selectRecord"
              }
            }
          ]
        },
        {
          "label": "All Tasks",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "generic_tasks",
                "showGlobalSearch": true,
                "addButton": false,
                "editFormId": 203,
                "filterField": ["assignee_id", "status", "priority", "due_date_from", "due_date_to"],
                "columnsToShow": ["title", "assignee_id", "due_date", "hours", "priority", "status", "module_id"],
                "columnLabels": {
                  "assignee_id": "Assigned To",
                  "module_id": "Module"
                },
                "sortBy": "due_date",
                "sortDir": "ASC",
                "rowClick": "selectRecord"
              }
            }
          ]
        },
        {
          "label": "Completed",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "generic_tasks",
                "showGlobalSearch": true,
                "addButton": false,
                "editFormId": 203,
                "filters": {
                  "status": "completed"
                },
                "columnsToShow": ["title", "assignee_id", "due_date", "priority", "updatedAt"],
                "columnLabels": {
                  "assignee_id": "Assigned To",
                  "updatedAt": "Completed At"
                },
                "sortBy": "updatedAt",
                "sortDir": "DESC",
                "rowClick": "selectRecord"
              }
            }
          ]
        }
      ]
    }
  ],
  "footer": [
    {
      "type": "button",
      "label": "Close",
      "onClick": "closeModal"
    }
  ]
}
$json$::jsonb
WHERE name = 'Task Manager' OR module_id = '01943a8b-2345-7890-abcd-123456789def'::uuid;
