-- =============================================================================
-- ANALYTICS E2E TEST SEED DATA
-- =============================================================================
-- Creates test data for device fingerprinting and tracking E2E tests.
--
-- Test Scenarios:
--   - Multiple sessions with different device types
--   - Various geolocation data (US, EU, VPN users)
--   - Content views and interactions for testing
--
-- Expected behavior:
--   - Track view endpoint accepts client device data
--   - SessionFingerprint is created/updated
--   - Enrichment services populate geo/VPN/device data
-- =============================================================================

-- Create device_type enum if not exists
DO $$ BEGIN
  CREATE TYPE device_type AS ENUM ('MOBILE', 'TABLET', 'DESKTOP');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- Create content_type enum if not exists
DO $$ BEGIN
  CREATE TYPE content_type AS ENUM ('LISTING', 'PROFILE', 'POST', 'MESSAGE', 'MEDIA');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- =============================================================================
-- SESSION FINGERPRINTS - Test device detection results
-- =============================================================================
CREATE TABLE IF NOT EXISTS session_fingerprints (
  session_id UUID PRIMARY KEY,
  device_type device_type NOT NULL DEFAULT 'DESKTOP',
  is_bot BOOLEAN NOT NULL DEFAULT FALSE,
  browser VARCHAR(50),
  browser_version VARCHAR(20),
  browser_major VARCHAR(10),
  os VARCHAR(50),
  os_version VARCHAR(20),
  device_vendor VARCHAR(50),
  device_model VARCHAR(50),
  screen_width INTEGER,
  screen_height INTEGER,
  viewport_width INTEGER,
  viewport_height INTEGER,
  pixel_ratio REAL,
  color_depth INTEGER,
  language VARCHAR(10),
  languages TEXT,
  timezone VARCHAR(50),
  timezone_offset INTEGER,
  country VARCHAR(2),
  region VARCHAR(10),
  city VARCHAR(100),
  is_eu BOOLEAN NOT NULL DEFAULT FALSE,
  geo_timezone VARCHAR(50),
  is_vpn BOOLEAN NOT NULL DEFAULT FALSE,
  is_datacenter BOOLEAN NOT NULL DEFAULT FALSE,
  is_tor BOOLEAN NOT NULL DEFAULT FALSE,
  device_memory REAL,
  hardware_concurrency INTEGER,
  touch_points INTEGER,
  cookies_enabled BOOLEAN,
  do_not_track BOOLEAN,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- =============================================================================
-- CONTENT VIEWS - Test tracking data
-- =============================================================================
CREATE TABLE IF NOT EXISTS content_views (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  content_id VARCHAR(255) NOT NULL,
  content_type content_type NOT NULL,
  user_id UUID,
  session_id UUID NOT NULL,
  device_type device_type NOT NULL DEFAULT 'DESKTOP',
  referrer TEXT,
  duration INTEGER,
  viewed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- =============================================================================
-- SEED TEST SESSIONS
-- =============================================================================

-- Session 1: Desktop Chrome user from US
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  os, os_version,
  screen_width, screen_height, viewport_width, viewport_height,
  language, timezone, timezone_offset,
  country, region, city, is_eu,
  is_vpn, is_datacenter, is_tor
) VALUES (
  '11111111-1111-1111-1111-111111111111',
  'DESKTOP', false,
  'Chrome', '120.0.0.0', '120',
  'Windows', '10',
  1920, 1080, 1920, 900,
  'en-US', 'America/New_York', 300,
  'US', 'NY', 'New York', false,
  false, false, false
) ON CONFLICT (session_id) DO NOTHING;

-- Session 2: Mobile Safari user from Germany (EU)
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  os, os_version,
  device_vendor, device_model,
  screen_width, screen_height, viewport_width, viewport_height,
  language, timezone, timezone_offset,
  country, region, city, is_eu,
  is_vpn, is_datacenter, is_tor,
  touch_points, pixel_ratio
) VALUES (
  '22222222-2222-2222-2222-222222222222',
  'MOBILE', false,
  'Safari', '17.2', '17',
  'iOS', '17.2',
  'Apple', 'iPhone',
  390, 844, 390, 664,
  'de-DE', 'Europe/Berlin', -60,
  'DE', 'BE', 'Berlin', true,
  false, false, false,
  5, 3
) ON CONFLICT (session_id) DO NOTHING;

-- Session 3: VPN user (should be flagged)
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  os, os_version,
  screen_width, screen_height, viewport_width, viewport_height,
  language, timezone, timezone_offset,
  country, region, city, is_eu,
  is_vpn, is_datacenter, is_tor
) VALUES (
  '33333333-3333-3333-3333-333333333333',
  'DESKTOP', false,
  'Firefox', '121.0', '121',
  'Linux', 'Ubuntu',
  2560, 1440, 2560, 1340,
  'en-GB', 'Europe/London', 0,
  'NL', 'NH', 'Amsterdam', true,
  true, false, false
) ON CONFLICT (session_id) DO NOTHING;

-- Session 4: Tablet user from Iceland
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  os, os_version,
  device_vendor, device_model,
  screen_width, screen_height, viewport_width, viewport_height,
  language, timezone, timezone_offset,
  country, region, city, is_eu,
  is_vpn, is_datacenter, is_tor,
  touch_points, pixel_ratio
) VALUES (
  '44444444-4444-4444-4444-444444444444',
  'TABLET', false,
  'Safari', '17.2', '17',
  'iPadOS', '17.2',
  'Apple', 'iPad',
  1024, 1366, 1024, 1266,
  'is-IS', 'Atlantic/Reykjavik', 0,
  'IS', null, 'Reykjavik', true,
  false, false, false,
  5, 2
) ON CONFLICT (session_id) DO NOTHING;

-- Session 5: Bot (Googlebot)
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  country, is_eu,
  is_vpn, is_datacenter, is_tor
) VALUES (
  '55555555-5555-5555-5555-555555555555',
  'DESKTOP', true,
  'Googlebot', '2.1', '2',
  'US', false,
  false, true, false
) ON CONFLICT (session_id) DO NOTHING;

-- Session 6: Tor user (should be flagged)
INSERT INTO session_fingerprints (
  session_id, device_type, is_bot,
  browser, browser_version, browser_major,
  os, os_version,
  screen_width, screen_height, viewport_width, viewport_height,
  language, timezone,
  country, is_eu,
  is_vpn, is_datacenter, is_tor
) VALUES (
  '66666666-6666-6666-6666-666666666666',
  'DESKTOP', false,
  'Firefox', '115.0', '115',
  'Windows', '10',
  1920, 1080, 1600, 900,
  'en-US', 'UTC',
  null, false,
  false, false, true
) ON CONFLICT (session_id) DO NOTHING;

-- =============================================================================
-- SEED CONTENT VIEWS
-- =============================================================================

-- Views for desktop US user
INSERT INTO content_views (content_id, content_type, session_id, device_type, referrer, duration)
VALUES
  ('listing-001', 'LISTING', '11111111-1111-1111-1111-111111111111', 'DESKTOP', 'https://google.com', 45),
  ('listing-002', 'LISTING', '11111111-1111-1111-1111-111111111111', 'DESKTOP', null, 120),
  ('profile-001', 'PROFILE', '11111111-1111-1111-1111-111111111111', 'DESKTOP', 'https://twitter.com', 30);

-- Views for mobile EU user
INSERT INTO content_views (content_id, content_type, session_id, device_type, referrer, duration)
VALUES
  ('listing-001', 'LISTING', '22222222-2222-2222-2222-222222222222', 'MOBILE', null, 60),
  ('listing-003', 'LISTING', '22222222-2222-2222-2222-222222222222', 'MOBILE', 'https://instagram.com', 90);

-- Views for VPN user
INSERT INTO content_views (content_id, content_type, session_id, device_type, referrer, duration)
VALUES
  ('listing-004', 'LISTING', '33333333-3333-3333-3333-333333333333', 'DESKTOP', null, 15);

-- =============================================================================
-- CREATE INDEXES
-- =============================================================================
CREATE INDEX IF NOT EXISTS idx_session_fingerprints_device_type ON session_fingerprints (device_type);
CREATE INDEX IF NOT EXISTS idx_session_fingerprints_country ON session_fingerprints (country);
CREATE INDEX IF NOT EXISTS idx_session_fingerprints_is_eu ON session_fingerprints (is_eu);
CREATE INDEX IF NOT EXISTS idx_session_fingerprints_is_vpn ON session_fingerprints (is_vpn);
CREATE INDEX IF NOT EXISTS idx_session_fingerprints_is_bot ON session_fingerprints (is_bot);
CREATE INDEX IF NOT EXISTS idx_content_views_session ON content_views (session_id);
CREATE INDEX IF NOT EXISTS idx_content_views_content ON content_views (content_id);

-- =============================================================================
-- VERIFICATION QUERIES (for debugging)
-- =============================================================================
-- Check sessions by type: SELECT device_type, COUNT(*) FROM session_fingerprints GROUP BY device_type;
-- Check EU users: SELECT * FROM session_fingerprints WHERE is_eu = true;
-- Check VPN/Tor users: SELECT * FROM session_fingerprints WHERE is_vpn OR is_tor;
-- Check bot sessions: SELECT * FROM session_fingerprints WHERE is_bot = true;
