-- Migration: Update module configs to add client_name to columnsToShow and fix vendorName in forms
-- Date: 20250905

-- Update Time Entries & Expenses module config to add client_name
UPDATE modules
SET config =
  '{
    "title":"Time Entries & Expenses",
    "header":[
      {"type":"filter-select",
        "options":{
          "source_table":"users",
          "source_column":"id",
          "display_column":"full_name",
          "default_label":"All",
          "data_column":"user_id"
        }
      },
      {"type":"filter-select",
        "options":{
          "data_column":"approved",
          "options":[
            {"label":"All","value":""},
            {"label":"Approved","value":"true"},
            {"label":"Pending","value":"false"}
          ],
          "value": "false"
        }
      },
      {"type":"filter-date-range",
        "options":{"data_column":"logged_date"}
      }
    ],
    "body":[
      {"type":"tabs",
        "tabs":[
          {"label":"Time Entries","elements":[
            {"type":"table","options":{
              "tableName":"time_entries",
              "inlineEdit":true,
              "showGlobalSearch":false,
              "addButton":false,
              "filterField":["user_id","approved","logged_date"],
              "columnsToShow":["counsel", "client_name", "hours", "total", "description", "billable","approved","approval_comment"]
            }}
          ]},
          {"label":"Expenses","elements":[
            {"type":"table","options":{
              "tableName":"expenses",
              "inlineEdit":true,
              "showGlobalSearch":false,
              "addButton":false,
              "filterField":["user_id","approved","logged_date"],
              "columnsToShow":["counsel", "client_name","amount","description","billable","approved","approval_comment"]
            }}
          ]},
          {"label":"One Time Costs","elements":[
            {"type":"table","options":{
              "tableName":"one_time_service_costs",
              "inlineEdit":true,
              "filterField":["user_id","approved","cost_date"],
              "columnsToShow":["counsel", "client_name", "amount","description","billable","approved","comments"]
            }}
          ]}
        ]
      }
    ],
    "footer":[{"type":"button","label":"Close","onClick":"closeModal"}]
  }'
WHERE name = 'time_entries_expenses';

update modules SET config =
       $json$
{
  "title": "Case Management",
  "header": [
    {
      "type": "filter-select",
      "options": {
        "source_table": "cases",
        "source_column": "case_id",
        "display_column": "title",
        "data_column": "case_id",
        "data_column_id": "selected_case_id",
        "placeholder": "Search cases by number, title, or client...",
        "search_fields": ["case_number","title","client_name"],
        "max_results": 25,
        "debounce_ms": 250,
        "scope": "admin_user",
        "userField": "lead_rep_id"
      }
    },
    { "type": "button", "label": "Add Client", "formName": "Client" },
    { "type": "button", "label": "Edit Client", "onClick": "openEditClientForm" },
    { "type": "button", "label": "Add Case", "formName": "Case" },
    { "type": "button", "label": "Edit Case", "onClick": "openEditCaseForm" },
    { "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" }
  ],
  "body": [
    {
      "type": "tabs",
      "tabs": [
        {
          "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",
                "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",
                "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": ["cost_date","amount","description","billable"],
                "filterField": "reference_id",
                "multipleSelect": true
              }
            }
          ]
        },
        {
          "label": "Invoices",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "invoices",
                "inlineEdit": true,
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "reference_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": "button", "label": "Record Payment", "formName": "Case Payment", "prefillFilter": "case_id" },
            {
              "type": "table",
              "options": {
                "tableName": "payments",
                "inlineEdit": true,
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "reference_id",
                "filters": { "reference_table": "cases" }
              }
            }
          ]
        }
      ]
    }
  ],
  "footer": [
    { "type": "button", "label": "Close", "onClick": "closeModal" }
  ]
}
 $json$::jsonb,
 where name = 'Case Management';
