-- Migration: Update column button onClick handlers to use genericToggle with dataset config
-- Date: 2025-10-04
-- Description: Replace inline function strings with genericToggle and dataset configuration
--              This allows reusable toggle buttons across all boolean fields without creating
--              separate functions for each button

-- Since the module config is complex JSON, we'll delete and re-insert the updated version
-- First, save the module_id and other metadata
DO $$
DECLARE
    v_module_id uuid := '0194a8b2-9999-7123-8123-123456789ac8'::uuid;
BEGIN
    -- Update time_entries_expenses module with new genericToggle configuration
    -- The new config uses genericToggle with dataset parameters instead of specific functions
    UPDATE modules
    SET config = $json$
    {
      "title":"Time Entries & Expenses",
      "header":[
        {"type":"filter-select","options":{"source_table":"users","data_column":"user_id"}},
        {"type":"filter-select","options":{"data_column":"approved","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,
              "editFormId":38,
              "filterField":["user_id","approved","date"],
              "filters":{"approved":false},
              "columnsToShow":["counsel","client_name","case_name","hours","total","description","billable","approved","approval_comment"],
              "columnButtons":{
                "billable":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Billable","className":"badge badge-success badge-sm cursor-pointer","title":"Click to mark as non-billable","onClick":"genericToggle","dataset":{"tableName":"time_entries","primaryKey":"time_entry_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"entry"}},
                  "false":{"text":"✗ Non-Billable","className":"badge badge-ghost badge-sm cursor-pointer","title":"Click to mark as billable","onClick":"genericToggle","dataset":{"tableName":"time_entries","primaryKey":"time_entry_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"entry"}}
                }},
                "approved":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Approved","className":"badge badge-success badge-sm cursor-pointer","title":"Click to disapprove","onClick":"genericToggle","dataset":{"tableName":"time_entries","primaryKey":"time_entry_id","field":"approved","confirmTrue":"Approve this entry?","confirmFalse":"Disapprove this entry?","entityName":"entry"}},
                  "false":{"text":"⏳ Pending","className":"badge badge-warning badge-sm cursor-pointer","title":"Click to approve","onClick":"genericToggle","dataset":{"tableName":"time_entries","primaryKey":"time_entry_id","field":"approved","confirmTrue":"Approve this entry?","confirmFalse":"Disapprove this entry?","entityName":"entry"}}
                }}
              }
            }}
          ]},
          {"label":"Expenses","elements":[
            {"type":"table","options":{
              "tableName":"expenses",
              "inlineEdit":true,
              "showGlobalSearch":false,
              "addButton":false,
              "editFormId":48,
              "filterField":["user_id","approved","date"],
              "filters":{"approved":false},
              "columnsToShow":["counsel","client_name","case_name","amount","description","billable","approved","approval_comment"],
              "columnButtons":{
                "billable":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Billable","className":"badge badge-success badge-sm cursor-pointer","title":"Click to mark as non-billable","onClick":"genericToggle","dataset":{"tableName":"expenses","primaryKey":"expense_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"expense"}},
                  "false":{"text":"✗ Non-Billable","className":"badge badge-ghost badge-sm cursor-pointer","title":"Click to mark as billable","onClick":"genericToggle","dataset":{"tableName":"expenses","primaryKey":"expense_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"expense"}}
                }},
                "approved":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Approved","className":"badge badge-success badge-sm cursor-pointer","title":"Click to disapprove","onClick":"genericToggle","dataset":{"tableName":"expenses","primaryKey":"expense_id","field":"approved","confirmTrue":"Approve this expense?","confirmFalse":"Disapprove this expense?","entityName":"expense"}},
                  "false":{"text":"⏳ Pending","className":"badge badge-warning badge-sm cursor-pointer","title":"Click to approve","onClick":"genericToggle","dataset":{"tableName":"expenses","primaryKey":"expense_id","field":"approved","confirmTrue":"Approve this expense?","confirmFalse":"Disapprove this expense?","entityName":"expense"}}
                }}
              }
            }}
          ]},
          {"label":"One Time Costs","elements":[
            {"type":"table","options":{
              "tableName":"one_time_service_costs",
              "inlineEdit":true,
              "editFormId":58,
              "filterField":["user_id","approved","date"],
              "filters":{"approved":false},
              "columnsToShow":["counsel","client_name","case_name","amount","description","billable","approved","comments"],
              "columnButtons":{
                "billable":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Billable","className":"badge badge-success badge-sm cursor-pointer","title":"Click to mark as non-billable","onClick":"genericToggle","dataset":{"tableName":"one_time_service_costs","primaryKey":"one_time_service_cost_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"cost"}},
                  "false":{"text":"✗ Non-Billable","className":"badge badge-ghost badge-sm cursor-pointer","title":"Click to mark as billable","onClick":"genericToggle","dataset":{"tableName":"one_time_service_costs","primaryKey":"one_time_service_cost_id","field":"billable","confirmTrue":"Mark as billable?","confirmFalse":"Mark as non-billable?","entityName":"cost"}}
                }},
                "approved":{"showValue":false,"buttonMap":{
                  "true":{"text":"✓ Approved","className":"badge badge-success badge-sm cursor-pointer","title":"Click to disapprove","onClick":"genericToggle","dataset":{"tableName":"one_time_service_costs","primaryKey":"one_time_service_cost_id","field":"approved","confirmTrue":"Approve this cost?","confirmFalse":"Disapprove this cost?","entityName":"cost"}},
                  "false":{"text":"⏳ Pending","className":"badge badge-warning badge-sm cursor-pointer","title":"Click to approve","onClick":"genericToggle","dataset":{"tableName":"one_time_service_costs","primaryKey":"one_time_service_cost_id","field":"approved","confirmTrue":"Approve this cost?","confirmFalse":"Disapprove this cost?","entityName":"cost"}}
                }}
              }
            }}
          ]}
        ]}
      ],
      "footer":[{"type":"button","label":"Close","onClick":"closeModal"}]
    }
    $json$::jsonb
    WHERE module_id = v_module_id;
END $$;
