-- Script to create the trigger on the task table
-- Run this after creating the trigger function

-- Drop existing trigger if it exists
DROP TRIGGER IF EXISTS trg_task_fixed_update_visibility ON task;

-- Create the trigger
CREATE TRIGGER trg_task_fixed_update_visibility
  AFTER UPDATE ON task
  FOR EACH ROW
  WHEN (
    lower(NEW.status) = 'fixed'
    AND lower(COALESCE(OLD.status, '')) IS DISTINCT FROM lower(NEW.status)
  )
  EXECUTE FUNCTION fn_task_fixed_update_visibility();

-- Verify trigger was created
SELECT 
    tgname AS trigger_name,
    tgrelid::regclass AS table_name,
    tgenabled AS enabled,
    pg_get_triggerdef(oid) AS trigger_definition
FROM pg_trigger
WHERE tgname = 'trg_task_fixed_update_visibility';

