-- Migration: Enhance click events for brand & campaign analytics
-- Adds event_type and page_type for better categorization

-- Add event_type column (default to 'product_click' for existing rows)
ALTER TABLE product_click_events
ADD COLUMN IF NOT EXISTS event_type VARCHAR(50) DEFAULT 'product_click';

-- Add page_type column for tracking which page the event originated from
ALTER TABLE product_click_events
ADD COLUMN IF NOT EXISTS page_type VARCHAR(100);

-- Add URL path for debugging/analysis
ALTER TABLE product_click_events
ADD COLUMN IF NOT EXISTS url_path TEXT;

-- Add device type (desktop, mobile, tablet)
ALTER TABLE product_click_events
ADD COLUMN IF NOT EXISTS device_type VARCHAR(20);

-- Create index on event_type for filtering
CREATE INDEX IF NOT EXISTS idx_product_click_events_event_type
ON product_click_events(event_type);

-- Create index on page_type for page-level analytics
CREATE INDEX IF NOT EXISTS idx_product_click_events_page_type
ON product_click_events(page_type);

-- Create composite index for brand analytics (brand_id + occurred_at)
CREATE INDEX IF NOT EXISTS idx_product_click_events_brand_time
ON product_click_events(brand_id, occurred_at)
WHERE brand_id IS NOT NULL;

-- Create composite index for store+brand analytics
CREATE INDEX IF NOT EXISTS idx_product_click_events_store_brand
ON product_click_events(store_id, brand_id)
WHERE store_id IS NOT NULL;

-- Add comment for documentation
COMMENT ON COLUMN product_click_events.event_type IS 'Type of event: product_click, page_view, etc.';
COMMENT ON COLUMN product_click_events.page_type IS 'Page where event occurred: StoreDetailPage, BrandsIntelligence, CampaignDetail, etc.';
COMMENT ON COLUMN product_click_events.device_type IS 'Device type: desktop, mobile, tablet';
