-- Add deleted_at columns to user-related tables for soft-delete support.
-- Previously, user_delete_account hard-deleted rows from these tables.
-- After this migration, all deletions are soft (mark as deleted).

ALTER TABLE "user" ADD COLUMN IF NOT EXISTS restore_token_hash VARCHAR(64);
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS restore_token_expires_at TIMESTAMPTZ;

ALTER TABLE user_2fa ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_activity ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_appearance ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_block ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_device ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_follow ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_mail ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_notify_setting ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_oauth ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_password ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_password_reset ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_presence ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_profile ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
ALTER TABLE user_security_log ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;

CREATE INDEX IF NOT EXISTS idx_user_security_log_user_deleted ON user_security_log(user_id, deleted_at);
CREATE INDEX IF NOT EXISTS idx_user_activity_user_deleted ON user_activity(user_id, deleted_at);
