-- Migration: Set billable field default to true for case management tables
-- Date: 2025-12-17
-- Description: Updates time_entries, expenses, and one_time_service_costs tables to default billable=true
--              and removes billable field from their forms since it should always be true for case management
--              Also adds computed-display field type and hours remaining display to time entry form

-- Add the computed-display element type if it doesn't exist
INSERT INTO element_types (name, html_type, sql_type)
VALUES ('computed-display', 'computed-display', NULL)
ON CONFLICT DO NOTHING;

-- Update table defaults
ALTER TABLE time_entries ALTER COLUMN billable SET DEFAULT true;
ALTER TABLE expenses ALTER COLUMN billable SET DEFAULT true;
ALTER TABLE one_time_service_costs ALTER COLUMN billable SET DEFAULT true;

-- Remove billable field from Time Entries form (id=38) and add hours remaining display
-- type_id 25 = computed-display
UPDATE forms 
SET elements = '[
  {"label":"Case","field_name":"case_id","data_column":"reference_id","type_id":4,"options_table":"cases","options_label_column":"title","options_value_column":"case_id","order_index":1},
  {"label":"Task","field_name":"generic_task_id","data_column":"generic_task_id","type_id":4,"options_table":"generic_tasks","options_label_column":"title","options_value_column":"task_id","options_filter":{"reference_table":"cases","reference_id_field":"reference_id"},"order_index":2},
  {"label":"Hours Remaining","field_name":"hours_remaining_display","type_id":25,"compute_function":"computeTaskHoursRemaining","watch_fields":["generic_task_id"],"help_text":"Automatically calculated based on task allocation and time logged","order_index":3},
  {"label":"Date","field_name":"date","data_column":"date","default":"now()","type_id":3,"order_index":4},
  {"label":"Hours","field_name":"hours","data_column":"hours","type_id":2,"order_index":5},
  {"label":"Description","field_name":"description","data_column":"description","type_id":8,"order_index":6},
  {"label":"Work Type","field_name":"work_type","data_column":"work_type_id","type_id":13,"options_table":"work_types","options_label_column":"name","options_value_column":"work_type_id","order_index":7},
  {"label":"Instructions","field_name":"instructions","data_column":"instructions","type_id":8,"order_index":8},
  {"label":"Documents","field_name":"documents","data_column":"document_ids","type_id":9,"order_index":9,"options":{"linkTable":"case_hour_documents","linkField":"document_id"}},
  {"field_name":"reference_table","data_column":"reference_table","type_id":17,"default_value":"cases","order_index":10}
]'::jsonb
WHERE id = 38;

-- Remove billable field from Case Expense form (id=48)
UPDATE forms 
SET elements = '[
  {"label":"Case","field_name":"case_id","data_column":"reference_id","type_id":4,"options_table":"cases","options_label_column":"title","options_value_column":"case_id","order_index":1},
  {"label":"Expense Date","field_name":"date","default":"now()","data_column":"date","type_id":3,"order_index":2},
  {"label":"Vendor","field_name":"vendor","data_column":"vendor_id","type_id":13,"options_table":"vendors","options_label_column":"vendorname","options_value_column":"vendor_id","order_index":3},
  {"label":"Invoice Number","field_name":"invoice_number","data_column":"invoice_number","type_id":1,"order_index":4},
  {"label":"Amount","field_name":"amount","data_column":"amount","type_id":2,"order_index":5},
  {"label":"Description","field_name":"description","data_column":"description","type_id":8,"order_index":6},
  {"label":"Documents","field_name":"document_ids","data_column":"document_ids","type_id":9,"order_index":7},
  {"field_name":"reference_table","data_column":"reference_table","type_id":17,"default_value":"cases","order_index":8}
]'::jsonb
WHERE id = 48;

-- Remove billable field from One Time Cost form (id=58)
UPDATE forms 
SET elements = '[
  {"label":"Case","field_name":"case_id","data_column":"reference_id","type_id":4,"options_table":"cases","options_label_column":"title","options_value_column":"case_id","order_index":1},
  {"label":"Cost Date","field_name":"date","default":"now()","data_column":"date","type_id":3,"order_index":2},
  {"label":"Vendor","field_name":"vendor","data_column":"vendor_id","type_id":13,"options_table":"vendors","options_label_column":"vendorname","options_value_column":"vendor_id","order_index":3},
  {"label":"Invoice Number","field_name":"invoice_number","data_column":"invoice_number","type_id":1,"order_index":4},
  {"label":"Amount","field_name":"amount","data_column":"amount","type_id":2,"order_index":5},
  {"label":"Description","field_name":"description","data_column":"description","type_id":8,"order_index":6},
  {"label":"Documents","field_name":"document_ids","data_column":"document_ids","type_id":9,"order_index":7},
  {"field_name":"reference_table","data_column":"reference_table","type_id":17,"default_value":"cases","order_index":8}
]'::jsonb
WHERE id = 58;
