-- Audit logging for SQLite (libsql/better-sqlite3).
--
-- Postgres used a generic trigger function + a per-request session variable
-- (set_config) to attribute audit rows. SQLite has neither, so we emulate it:
--   * audit_ctx holds the current user id for the active connection (set by the
--     app via setAuditContext() before each mutation).
--   * Per-table AFTER INSERT/UPDATE/DELETE triggers write audit_log rows,
--     reading the actor from audit_ctx.
--
-- NOTE: audit_ctx is connection-global. Attribution is correct when mutations
-- run serially per connection (the better-sqlite3 case); under heavy
-- interleaved concurrency the actor could be misattributed.

CREATE TABLE IF NOT EXISTS audit_ctx (
  id INTEGER PRIMARY KEY CHECK (id = 1),
  user_id TEXT
);
INSERT OR IGNORE INTO audit_ctx (id, user_id) VALUES (1, NULL);

CREATE TRIGGER audit_user_insert AFTER INSERT ON user
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'user', NEW.id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_user_update AFTER UPDATE ON user
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'user', NEW.id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_user_delete AFTER DELETE ON user
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'user', OLD.id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_request_insert AFTER INSERT ON stay_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_request_update AFTER UPDATE ON stay_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_request_delete AFTER DELETE ON stay_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_insert AFTER INSERT ON stay
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay', NEW.stay_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_update AFTER UPDATE ON stay
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay', NEW.stay_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_stay_delete AFTER DELETE ON stay
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'stay', OLD.stay_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_route_insert AFTER INSERT ON boat_route
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_route', NEW.route_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_route_update AFTER UPDATE ON boat_route
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_route', NEW.route_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_route_delete AFTER DELETE ON boat_route
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_route', OLD.route_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_insert AFTER INSERT ON boat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat', NEW.boat_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_update AFTER UPDATE ON boat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat', NEW.boat_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_delete AFTER DELETE ON boat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat', OLD.boat_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_trip_insert AFTER INSERT ON boat_trip
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_trip', NEW.trip_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_trip_update AFTER UPDATE ON boat_trip
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_trip', NEW.trip_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_trip_delete AFTER DELETE ON boat_trip
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_trip', OLD.trip_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_reservation_request_insert AFTER INSERT ON boat_reservation_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_reservation_request_update AFTER UPDATE ON boat_reservation_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_reservation_request_delete AFTER DELETE ON boat_reservation_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_reservation_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_manifest_entry_insert AFTER INSERT ON boat_manifest_entry
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', NEW.entry_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_manifest_entry_update AFTER UPDATE ON boat_manifest_entry
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', NEW.entry_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_boat_manifest_entry_delete AFTER DELETE ON boat_manifest_entry
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'boat_manifest_entry', OLD.entry_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_insert AFTER INSERT ON room
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room', NEW.room_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_update AFTER UPDATE ON room
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room', NEW.room_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_delete AFTER DELETE ON room
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room', OLD.room_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_block_insert AFTER INSERT ON room_block
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_block', NEW.block_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_block_update AFTER UPDATE ON room_block
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_block', NEW.block_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_block_delete AFTER DELETE ON room_block
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_block', OLD.block_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_request_insert AFTER INSERT ON room_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_request_update AFTER UPDATE ON room_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_request_delete AFTER DELETE ON room_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_allocation_insert AFTER INSERT ON room_allocation
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_allocation', NEW.allocation_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_allocation_update AFTER UPDATE ON room_allocation
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_allocation', NEW.allocation_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_room_allocation_delete AFTER DELETE ON room_allocation
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'room_allocation', OLD.allocation_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_dietary_profile_insert AFTER INSERT ON dietary_profile
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'dietary_profile', NEW.profile_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_dietary_profile_update AFTER UPDATE ON dietary_profile
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'dietary_profile', NEW.profile_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_dietary_profile_delete AFTER DELETE ON dietary_profile
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'dietary_profile', OLD.profile_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_service_insert AFTER INSERT ON meal_service
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_service', NEW.service_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_service_update AFTER UPDATE ON meal_service
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_service', NEW.service_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_service_delete AFTER DELETE ON meal_service
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_service', OLD.service_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_attendance_insert AFTER INSERT ON meal_attendance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_attendance', NEW.attendance_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_attendance_update AFTER UPDATE ON meal_attendance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_attendance', NEW.attendance_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_meal_attendance_delete AFTER DELETE ON meal_attendance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'meal_attendance', OLD.attendance_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_insert AFTER INSERT ON retreat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat', NEW.retreat_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_update AFTER UPDATE ON retreat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat', NEW.retreat_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_delete AFTER DELETE ON retreat
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat', OLD.retreat_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_person_insert AFTER INSERT ON retreat_person
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_person', NEW.retreat_person_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_person_update AFTER UPDATE ON retreat_person
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_person', NEW.retreat_person_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_person_delete AFTER DELETE ON retreat_person
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_person', OLD.retreat_person_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_schedule_item_insert AFTER INSERT ON retreat_schedule_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', NEW.item_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_schedule_item_update AFTER UPDATE ON retreat_schedule_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', NEW.item_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_retreat_schedule_item_delete AFTER DELETE ON retreat_schedule_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'retreat_schedule_item', OLD.item_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_template_insert AFTER INSERT ON task_template
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_template', NEW.template_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_template_update AFTER UPDATE ON task_template
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_template', NEW.template_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_template_delete AFTER DELETE ON task_template
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_template', OLD.template_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_instance_insert AFTER INSERT ON task_instance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_instance', NEW.task_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_instance_update AFTER UPDATE ON task_instance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_instance', NEW.task_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_instance_delete AFTER DELETE ON task_instance
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_instance', OLD.task_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_assignment_insert AFTER INSERT ON task_assignment
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_assignment', NEW.assignment_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_assignment_update AFTER UPDATE ON task_assignment
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_assignment', NEW.assignment_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_task_assignment_delete AFTER DELETE ON task_assignment
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'task_assignment', OLD.assignment_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_item_insert AFTER INSERT ON inventory_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_item', NEW.item_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_item_update AFTER UPDATE ON inventory_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_item', NEW.item_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_item_delete AFTER DELETE ON inventory_item
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_item', OLD.item_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_request_insert AFTER INSERT ON inventory_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_request', NEW.request_id, 'INSERT', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_request_update AFTER UPDATE ON inventory_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_request', NEW.request_id, 'UPDATE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

CREATE TRIGGER audit_inventory_request_delete AFTER DELETE ON inventory_request
BEGIN
  INSERT INTO audit_log (audit_id, table_name, record_id, action, user_id, changed_fields)
  VALUES (lower(hex(randomblob(16))), 'inventory_request', OLD.request_id, 'DELETE', (SELECT user_id FROM audit_ctx WHERE id = 1), NULL);
END;

