-- ====================================================================
-- Sign in with Google for customers (2026-09-26)
--   1. users.google_id — Google's stable account id (the ID token's `sub`).
--      An account is found by it first, so a customer who later changes the
--      email on their Google account still lands in the same account.
--   2. users.created_via gains 'google' (accounts first created through
--      Google sign-in).
-- Keys (Client ID / Secret) live in the existing site_settings columns
-- google_client_id / google_client_secret (Settings → Social Login).
-- Safe to run more than once.
-- ====================================================================

SET NAMES utf8mb4;

SET @has_col = (SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users' AND COLUMN_NAME = 'google_id');
SET @sql = IF(@has_col = 0,
  'ALTER TABLE `users` ADD COLUMN `google_id` VARCHAR(64) NULL DEFAULT NULL AFTER `email_verified_at`, ADD UNIQUE KEY `uk_users_google_id` (`google_id`)',
  'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

ALTER TABLE `users`
  MODIFY `created_via` ENUM('register','guest_checkout','google') NOT NULL DEFAULT 'register'
  COMMENT 'guest_checkout = auto-created at checkout; the inbox owner claims it via an emailed set-password link. google = first signed in with Google';
