-- +migrate Up

-- 1. Add logged_at to sync_log for TTL-based pruning.
ALTER TABLE sync_log ADD COLUMN logged_at INTEGER NOT NULL DEFAULT 0;

-- 2. Migrate settings_history to UUID TEXT primary key and add updated_at.
ALTER TABLE settings_history RENAME TO settings_history_old;

CREATE TABLE settings_history (
  id              TEXT PRIMARY KEY,
  effective_from  TEXT NOT NULL,
  hours_per_week  REAL NOT NULL,
  workdays_mask   INTEGER NOT NULL DEFAULT 31,
  timezone        TEXT NOT NULL DEFAULT 'UTC',
  created_at      INTEGER NOT NULL,
  updated_at      INTEGER NOT NULL
);

INSERT INTO settings_history (id, effective_from, hours_per_week, workdays_mask, timezone, created_at, updated_at)
SELECT lower(hex(randomblob(16))), effective_from, hours_per_week, workdays_mask, timezone, created_at, created_at
FROM settings_history_old;

DROP TABLE settings_history_old;

-- +migrate Down
-- (intentionally left minimal; restoring integer PK requires recreating the table again)
ALTER TABLE sync_log DROP COLUMN logged_at;
