-- Hardware ID Cleanup Migration
-- Phase 1: Add new columns alongside old ones

-- 1. Update sessions table
ALTER TABLE sessions 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- 2. Update device_registrations table
ALTER TABLE device_registrations 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint TEXT;

-- 3. Update license_tokens table
ALTER TABLE license_tokens 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- 4. Update license_heartbeats table (partitioned)
ALTER TABLE license_heartbeats 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- 5. Update activation_events table
ALTER TABLE activation_events 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255),
ADD COLUMN IF NOT EXISTS previous_hardware_fingerprint VARCHAR(255);

-- 6. Update active_sessions table
ALTER TABLE active_sessions 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- 7. Update license_sessions table
ALTER TABLE license_sessions 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- 8. Update session_history table
ALTER TABLE session_history 
ADD COLUMN IF NOT EXISTS machine_name VARCHAR(255),
ADD COLUMN IF NOT EXISTS hardware_fingerprint VARCHAR(255);

-- Copy existing data to new columns
-- For now, we'll copy hardware_id to hardware_fingerprint
-- machine_name will be populated by the application

UPDATE sessions SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE device_registrations SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE license_tokens SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE license_heartbeats SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE activation_events SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE activation_events SET previous_hardware_fingerprint = previous_hardware_id WHERE previous_hardware_fingerprint IS NULL;
UPDATE active_sessions SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE license_sessions SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;
UPDATE session_history SET hardware_fingerprint = hardware_id WHERE hardware_fingerprint IS NULL;

-- Create indexes for new columns
CREATE INDEX IF NOT EXISTS idx_sessions_hardware_fingerprint ON sessions(hardware_fingerprint);
CREATE INDEX IF NOT EXISTS idx_device_registrations_hardware_fingerprint ON device_registrations(hardware_fingerprint);
CREATE INDEX IF NOT EXISTS idx_license_tokens_hardware_fingerprint ON license_tokens(hardware_fingerprint);
CREATE INDEX IF NOT EXISTS idx_license_heartbeats_hardware_fingerprint ON license_heartbeats(hardware_fingerprint, timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_active_sessions_hardware_fingerprint ON active_sessions(hardware_fingerprint);
CREATE INDEX IF NOT EXISTS idx_license_sessions_hardware_fingerprint ON license_sessions(license_id, hardware_fingerprint);
CREATE INDEX IF NOT EXISTS idx_session_history_hardware_fingerprint ON session_history(hardware_fingerprint);

-- Note: Old columns are NOT dropped yet. This will be done in a later migration
-- after verifying the system works with new columns.