-- Add first_name, last_name, and notifications_enabled columns to mobile_users table
ALTER TABLE mobile_users ADD COLUMN first_name TEXT;
ALTER TABLE mobile_users ADD COLUMN last_name TEXT;
ALTER TABLE mobile_users ADD COLUMN notifications_enabled INTEGER DEFAULT 1 NOT NULL;

-- For existing users, set notifications_enabled to true (1)
UPDATE mobile_users SET notifications_enabled = 1 WHERE notifications_enabled IS NULL;

-- Migrate firstName and lastName from metadata to columns for existing users
UPDATE mobile_users 
SET 
  first_name = json_extract(metadata, '$.firstName'),
  last_name = json_extract(metadata, '$.lastName')
WHERE metadata IS NOT NULL 
  AND (json_extract(metadata, '$.firstName') IS NOT NULL OR json_extract(metadata, '$.lastName') IS NOT NULL);

-- Create index for notifications_enabled since it will be used in queries
CREATE INDEX IF NOT EXISTS idx_mobile_users_notifications_enabled ON mobile_users(notifications_enabled);
