-- Migration: Add station and staff filters to Payroll module
-- Date: 2026-01-26
-- Description: Adds filter-select components for branches (stations) and users (staff)
--              to enable filtering BAF Excel exports by location and/or specific employee

UPDATE modules 
SET config = $json$ {
  "title": "Payroll",
  "header": [
    {
      "type": "filter-date-range",
      "options": {
        "data_column": "start_date",
        "label": "Pay Period"
      }
    },
    {
      "type": "filter-select",
      "options": {
        "source_table": "branches",
        "source_column": "name",
        "display_column": "name",
        "data_column": "branch_id",
        "populateMode": "search",
        "max_results": 50
      }
    },
    {
      "type": "filter-select",
      "options": {
        "source_table": "users",
        "source_column": "full_name",
        "display_column": "full_name",
        "data_column": "user_id",
        "placeholder": "Search staff by name...",
        "search_fields": ["full_name"],
        "populateMode": "search",
        "max_results": 50
      }
    },
    { "type": "button", "label": "Generate Pay", "onClick": "generatePayForEmployees" },
    { "type": "button", "label": "Export Excel", "onClick": "exportTimeClockExcel" }
  ],
  "body": [
    {
      "type": "tabs",
      "tabs": [
        {
          "label": "Worked Hours",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "time_clock_aggregates",
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "start_date",
                "listenColumn": "start_date",
                "syncFilters": true,
                "columnsToShow": ["name", "start_date", "end_date", "total_net_hours", "total_overtime_hours", "total_regular_hours", "total_payable_hours", "total_days_worked"],
                "rowClick": "selectRecord",
                "rowActions": [
                  { "label": "Daily Details", "onClick": "viewDailyDetails" },
                  { "label": "View Details", "onClick": "viewTimeClockDetails" },
                  { "label": "View GPS Map", "onClick": "viewTimeClockGpsMap" }
                ]
              }
            }
          ]
        },
        {
          "label": "Payslips",
          "elements": [
            {
              "type": "table",
              "options": {
                "tableName": "generated_payslips",
                "showGlobalSearch": false,
                "addButton": false,
                "filterField": "start_date",
                "listenColumn": "start_date",
                "columnsToShow": ["user_name", "org_name", "gross_pay", "net_pay", "start_date", "end_date"]
              }
            }
          ]
        }
      ]
    }
  ],
  "footer": [
    { "type": "button", "label": "Close", "onClick": "closeModal" }
  ]
} $json$::jsonb
WHERE module_id = '018e3f0a-12d3-7c45-b89a-2f3c4e1d5b6c'::uuid;

-- Verify the update
SELECT 
  name,
  (config->'header')::text AS header_config
FROM modules
WHERE module_id = '018e3f0a-12d3-7c45-b89a-2f3c4e1d5b6c'::uuid;
