-- =====================================================
-- Migration: Add Biometric Authentication Support
-- Description: Adds columns for WebAuthn biometric auth
-- Date: January 2026
-- =====================================================

-- Add biometric authentication columns to users table
DO $$ 
BEGIN
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='biometric_enabled') THEN
        ALTER TABLE users ADD COLUMN biometric_enabled BOOLEAN DEFAULT FALSE;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='biometric_credential_id') THEN
        ALTER TABLE users ADD COLUMN biometric_credential_id TEXT;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='biometric_public_key') THEN
        ALTER TABLE users ADD COLUMN biometric_public_key TEXT;
    END IF;
    IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users' AND column_name='biometric_registered_at') THEN
        ALTER TABLE users ADD COLUMN biometric_registered_at TIMESTAMP;
    END IF;
END $$;

-- Create index for faster biometric lookups
CREATE INDEX IF NOT EXISTS idx_users_biometric_credential 
ON users(biometric_credential_id) 
WHERE biometric_enabled = TRUE;

-- Update existing users to have biometric_enabled = false
UPDATE users SET biometric_enabled = FALSE WHERE biometric_enabled IS NULL;

