-- Migration 047: Auto-Registration Token Support
-- Adds install_uuid, app_version, and last_used_at columns to token tables
-- so Android devices can self-register and receive a trial token on first launch.

-- Add install_uuid (used for idempotent re-registration)
ALTER TABLE ftt_tokens
    ADD COLUMN IF NOT EXISTS install_uuid VARCHAR(128) NULL AFTER device_id,
    ADD COLUMN IF NOT EXISTS app_version VARCHAR(64) NULL AFTER install_uuid,
    ADD COLUMN IF NOT EXISTS last_used_at TIMESTAMP NULL AFTER created_at;

-- Add index for fast install_uuid lookup
ALTER TABLE ftt_tokens
    ADD UNIQUE INDEX IF NOT EXISTS idx_install_uuid (install_uuid);

-- Mirror to shadow table if it exists
ALTER TABLE tokens
    ADD COLUMN IF NOT EXISTS install_uuid VARCHAR(128) NULL AFTER device_id,
    ADD COLUMN IF NOT EXISTS app_version VARCHAR(64) NULL AFTER install_uuid,
    ADD COLUMN IF NOT EXISTS last_used_at TIMESTAMP NULL AFTER created_at;

ALTER TABLE tokens
    ADD UNIQUE INDEX IF NOT EXISTS idx_install_uuid (install_uuid);
