-- Migration: Complete Case Management module configuration
-- Date: 2025-10-02
-- Description: Updates Case Management module to support cross-tab selection,
--              adds Select row action, and ensures downstream tables filter by case_id.

UPDATE modules
SET config = '{
  "title": "Case Management",
  "header": [
    {
      "type": "filter-select",
      "options": {
        "source_table": "clients",
        "source_column": "client_id",
        "display_column": "name",
        "data_column": "client_name",
        "data_column_id": "selected_client_id",
        "placeholder": "Search by client name...",
        "search_fields": ["name"],
        "max_results": 25,
        "debounce_ms": 250,
        "populateMode": "immediate"
      }
    },
    { "type": "button", "label": "Add Client", "formName": "Client" },
    { "type": "button", "label": "Edit Client", "onClick": "openEditForm" },
    { "type": "button", "label": "Add Case", "formName": "Case" },
    { "type": "button", "label": "Edit Case", "onClick": "openEditForm" },
    { "type": "button", "label": "Add Hour", "formName": "Time Entries", "prefillFilter": "case_id", "source_column": "case_id" },
    { "type": "button", "label": "Add Expense", "formName": "Case Expense", "prefillFilter": "case_id" },
    { "type": "button", "label": "Add One Time Cost", "formName": "One Time Cost", "prefillFilter": "case_id" },
    { "type": "button", "label": "Create Cost Schedule", "onClick": "createCostSchedule" },
    { "type": "button", "label": "Create Invoice", "onClick": "createCostSchedule" },
    { "type": "button", "label": "Record Payment", "onClick": "recordGeneralPayment" }
  ],
  "body": [
    {
      "type": "tabs",
      "tabs": [
        {
          "label": "Cases",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "cases",
                "showGlobalSearch": false,
                "addButton": false,
                "columnsToShow": ["client_name", "case_number", "title", "lead_rep_name", "status", "added_by"],
                "filterField": "client_name",
                "listenColumn": "client_name",
                "primaryKeyField": "case_id",
                "rowActions": [
                  { "label": "Select", "onClick": "selectCaseForManagement" }
                ]
              }
            }
          ]
        },
        {
          "label": "Details",
          "elements": [
            { "type": "detail-view", "viewId": 2, "filterField": "case_id", "tableName": "cases" }
          ]
        },
        {
          "label": "Instructions",
          "elements": [
            { "type": "button", "label": "Add Instruction", "formName": "Case Instruction", "prefillFilter": "case_id" },
            { "type": "table", "options": { "tableName": "case_instructions", "inlineEdit": true, "showGlobalSearch": false, "addButton": false, "filterField": "case_id" } }
          ]
        },
        {
          "label": "Documents",
          "elements": [
            { "type": "documents", "options": { "filterField": "case_id" } }
          ]
        },
        {
          "label": "Hours",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "time_entries",
                "inlineEdit": true,
                "ownerEditOnly": true,
                "tableUrl": "../api/time_entries",
                "showGlobalSearch": false,
                "addButton": false,
                "editFormId": 38,
                "columnsToShow": ["Counsel", "date", "hours", "total", "work_type", "description"],
                "filterField": "reference_id",
                "listenColumn": "case_id",
                "multipleSelect": true
              }
            }
          ]
        },
        {
          "label": "Expenses",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "expenses",
                "inlineEdit": true,
                "ownerEditOnly": true,
                "showGlobalSearch": false,
                "addButton": false,
                "editFormId": 48,
                "columnsToShow": ["counsel", "amount", "description", "billable"],
                "filterField": "reference_id",
                "listenColumn": "case_id",
                "multipleSelect": true
              }
            }
          ]
        },
        {
          "label": "One Time Costs",
          "elements": [
            { "type": "button", "label": "Add One Time Cost", "formName": "One Time Cost", "prefillFilter": "case_id" },
            {
              "type": "table",
              "options": {
                "tableName": "one_time_service_costs",
                "inlineEdit": true,
                "ownerEditOnly": true,
                "showGlobalSearch": false,
                "addButton": false,
                "editFormId": 58,
                "columnsToShow": ["date", "amount", "description", "billable"],
                "filterField": "reference_id",
                "listenColumn": "case_id",
                "multipleSelect": true
              }
            }
          ]
        },
        {
          "label": "Invoices",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "invoices",
                "inlineEdit": true,
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "reference_id",
                "listenColumn": "case_id",
                "filters": { "reference_table": "cases" },
                "columnsToShow": ["invoice_date", "invoice_number", "amount", "amount_paid", "balance"],
                "rowActions": [
                  { "label": "Edit", "onClick": "openCaseInvoiceEditForm" },
                  { "label": "Record Payment", "onClick": "recordInvoicePayment" },
                  { "label": "Download", "onClick": "downloadCaseInvoice" },
                  { "label": "Preview", "onClick": "previewCaseInvoice" }
                ]
              }
            }
          ]
        },
        {
          "label": "Payments",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "payments",
                "inlineEdit": true,
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "reference_id",
                "listenColumn": "case_id",
                "filters": { "reference_table": "cases" }
              }
            }
          ]
        }
      ]
    }
  ],
  "footer": [
    { "type": "button", "label": "Close", "onClick": "closeModal" }
  ]
}'::jsonb
WHERE name = 'Case Management';
