ALTER TABLE field_time_entries
  ADD COLUMN IF NOT EXISTS work_date DATE NULL AFTER intervention_id,
  ADD COLUMN IF NOT EXISTS morning_starts_at DATETIME NULL AFTER work_date,
  ADD COLUMN IF NOT EXISTS morning_ends_at DATETIME NULL AFTER morning_starts_at,
  ADD COLUMN IF NOT EXISTS afternoon_starts_at DATETIME NULL AFTER morning_ends_at,
  ADD COLUMN IF NOT EXISTS afternoon_ends_at DATETIME NULL AFTER afternoon_starts_at,
  ADD COLUMN IF NOT EXISTS morning_minutes INT UNSIGNED NULL AFTER break_minutes,
  ADD COLUMN IF NOT EXISTS afternoon_minutes INT UNSIGNED NULL AFTER morning_minutes,
  ADD COLUMN IF NOT EXISTS duration_minutes INT UNSIGNED NULL AFTER afternoon_minutes,
  ADD COLUMN IF NOT EXISTS work_note TEXT NULL AFTER expense_note,
  ADD COLUMN IF NOT EXISTS source_type VARCHAR(30) NOT NULL DEFAULT 'manual' AFTER work_note,
  ADD COLUMN IF NOT EXISTS submitted_by INT UNSIGNED NULL AFTER status,
  ADD COLUMN IF NOT EXISTS approved_by INT UNSIGNED NULL AFTER submitted_by,
  ADD COLUMN IF NOT EXISTS approved_at DATETIME NULL AFTER approved_by,
  ADD COLUMN IF NOT EXISTS updated_at DATETIME NULL AFTER created_at;

UPDATE field_time_entries
SET work_date = DATE(starts_at)
WHERE work_date IS NULL AND starts_at IS NOT NULL;
