-- Add new fields to visitor_events table for better analytics tracking

ALTER TABLE visitor_events ADD COLUMN IF NOT EXISTS page_path VARCHAR(512);
ALTER TABLE visitor_events ADD COLUMN IF NOT EXISTS page_name VARCHAR(512);
ALTER TABLE visitor_events ADD COLUMN IF NOT EXISTS data JSONB;

-- Create indexes for better query performance
CREATE INDEX IF NOT EXISTS idx_visitor_events_page_path ON visitor_events(page_path);
CREATE INDEX IF NOT EXISTS idx_visitor_events_created_at ON visitor_events(created_at);
CREATE INDEX IF NOT EXISTS idx_visitor_events_event_type_created_at ON visitor_events(event_type, created_at);

-- Copy existing 'page' data to 'page_path' if page_path is empty
UPDATE visitor_events SET page_path = page WHERE page_path IS NULL OR page_path = '';
