-- Seed Conversion Events for E2E Testing
-- Generates realistic funnel data across multiple traffic sources

-- Clear existing test data (keeping production data safe with timestamp filter)
DELETE FROM conversion_events WHERE created_at > NOW() - INTERVAL '7 days';

-- Helper: Generate UUIDs
-- We'll use deterministic UUIDs for testing

-- ============================================================================
-- ORGANIC TRAFFIC (SEO) - Best converting source
-- ============================================================================
-- VISIT stage: 5000
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    NULL,
    'VISIT',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    (ARRAY['desktop', 'mobile', 'tablet'])[floor(random() * 3 + 1)],
    (ARRAY['Chrome', 'Safari', 'Firefox', 'Edge'])[floor(random() * 4 + 1)],
    (ARRAY['US', 'GB', 'DE', 'CA', 'AU'])[floor(random() * 5 + 1)],
    NULL,
    NOW() - (random() * INTERVAL '7 days')
FROM generate_series(1, 5000);

-- SIGNUP stage: 2500 (50% conversion from visit)
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'SIGNUP',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    (ARRAY['desktop', 'mobile', 'tablet'])[floor(random() * 3 + 1)],
    (ARRAY['Chrome', 'Safari', 'Firefox'])[floor(random() * 3 + 1)],
    (ARRAY['US', 'GB', 'DE'])[floor(random() * 3 + 1)],
    NULL,
    NOW() - (random() * INTERVAL '6 days')
FROM generate_series(1, 2500);

-- PROFILE_COMPLETE: 1800 (72% from signup)
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'PROFILE_COMPLETE',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    (ARRAY['desktop', 'mobile'])[floor(random() * 2 + 1)],
    (ARRAY['Chrome', 'Safari'])[floor(random() * 2 + 1)],
    (ARRAY['US', 'GB'])[floor(random() * 2 + 1)],
    NULL,
    NOW() - (random() * INTERVAL '5 days')
FROM generate_series(1, 1800);

-- FIRST_CONTENT: 1200 (67% from profile)
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'FIRST_CONTENT',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    (ARRAY['desktop', 'mobile'])[floor(random() * 2 + 1)],
    'Chrome',
    'US',
    NULL,
    NOW() - (random() * INTERVAL '4 days')
FROM generate_series(1, 1200);

-- SUBSCRIBE: 600 (50% from content)
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'SUBSCRIBE',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    'desktop',
    'Chrome',
    'US',
    1999,
    NOW() - (random() * INTERVAL '3 days')
FROM generate_series(1, 600);

-- PURCHASE: 350 (58% from subscribe)
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'PURCHASE',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    'desktop',
    'Chrome',
    'US',
    1999 + floor(random() * 5000),
    NOW() - (random() * INTERVAL '2 days')
FROM generate_series(1, 350);

-- REPEAT_PURCHASE: 120
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    gen_random_uuid(),
    'REPEAT_PURCHASE',
    'ORGANIC',
    NULL,
    'organic',
    'https://google.com',
    '/escorts/miami',
    'desktop',
    'Chrome',
    'US',
    2999 + floor(random() * 3000),
    NOW() - (random() * INTERVAL '1 day')
FROM generate_series(1, 120);

-- ============================================================================
-- PAID TRAFFIC (Google/Meta Ads) - Higher intent but lower volume
-- ============================================================================
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT
    gen_random_uuid(),
    gen_random_uuid(),
    NULL,
    'VISIT',
    'PAID',
    'summer-2025-promo',
    'cpc',
    'https://ads.google.com',
    '/landing/premium',
    (ARRAY['desktop', 'mobile'])[floor(random() * 2 + 1)],
    'Chrome',
    'US',
    NULL,
    NOW() - (random() * INTERVAL '7 days')
FROM generate_series(1, 3000);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SIGNUP', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '6 days') FROM generate_series(1, 1800);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PROFILE_COMPLETE', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '5 days') FROM generate_series(1, 1400);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'FIRST_CONTENT', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '4 days') FROM generate_series(1, 1000);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SUBSCRIBE', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', 2999, NOW() - (random() * INTERVAL '3 days') FROM generate_series(1, 550);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PURCHASE', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', 2999 + floor(random() * 5000), NOW() - (random() * INTERVAL '2 days') FROM generate_series(1, 320);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'REPEAT_PURCHASE', 'PAID', 'summer-2025-promo', 'cpc', 'https://ads.google.com', '/landing/premium', 'desktop', 'Chrome', 'US', 3999, NOW() - (random() * INTERVAL '1 day') FROM generate_series(1, 95);

-- ============================================================================
-- SOCIAL TRAFFIC (Twitter, Reddit) - Lower conversion but high volume
-- ============================================================================
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), NULL, 'VISIT', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', NULL, NOW() - (random() * INTERVAL '7 days') FROM generate_series(1, 4000);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SIGNUP', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', NULL, NOW() - (random() * INTERVAL '6 days') FROM generate_series(1, 1200);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PROFILE_COMPLETE', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', NULL, NOW() - (random() * INTERVAL '5 days') FROM generate_series(1, 700);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'FIRST_CONTENT', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', NULL, NOW() - (random() * INTERVAL '4 days') FROM generate_series(1, 400);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SUBSCRIBE', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', 1999, NOW() - (random() * INTERVAL '3 days') FROM generate_series(1, 180);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PURCHASE', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', 1999, NOW() - (random() * INTERVAL '2 days') FROM generate_series(1, 85);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'REPEAT_PURCHASE', 'SOCIAL', 'twitter-viral', 'social', 'https://twitter.com', '/', 'mobile', 'Safari', 'US', 2999, NOW() - (random() * INTERVAL '1 day') FROM generate_series(1, 25);

-- ============================================================================
-- EMAIL TRAFFIC - Re-engagement, high conversion
-- ============================================================================
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'VISIT', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '7 days') FROM generate_series(1, 1500);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SIGNUP', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '6 days') FROM generate_series(1, 1100);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PROFILE_COMPLETE', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '5 days') FROM generate_series(1, 950);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'FIRST_CONTENT', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '4 days') FROM generate_series(1, 800);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SUBSCRIBE', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', 2999, NOW() - (random() * INTERVAL '3 days') FROM generate_series(1, 450);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PURCHASE', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', 2999 + floor(random() * 4000), NOW() - (random() * INTERVAL '2 days') FROM generate_series(1, 280);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'REPEAT_PURCHASE', 'EMAIL', 'winback-jan-2025', 'email', NULL, '/welcome-back', 'desktop', 'Chrome', 'US', 3999, NOW() - (random() * INTERVAL '1 day') FROM generate_series(1, 110);

-- ============================================================================
-- REFERRAL TRAFFIC - Partner sites
-- ============================================================================
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), NULL, 'VISIT', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '7 days') FROM generate_series(1, 800);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SIGNUP', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '6 days') FROM generate_series(1, 450);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PROFILE_COMPLETE', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '5 days') FROM generate_series(1, 320);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'FIRST_CONTENT', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '4 days') FROM generate_series(1, 220);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SUBSCRIBE', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', 1999, NOW() - (random() * INTERVAL '3 days') FROM generate_series(1, 120);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PURCHASE', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', 1999, NOW() - (random() * INTERVAL '2 days') FROM generate_series(1, 65);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'REPEAT_PURCHASE', 'REFERRAL', NULL, 'referral', 'https://partner-site.com/best-platforms', '/', 'desktop', 'Chrome', 'US', 2999, NOW() - (random() * INTERVAL '1 day') FROM generate_series(1, 20);

-- ============================================================================
-- DIRECT TRAFFIC - Bookmarks, direct URL
-- ============================================================================
INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), NULL, 'VISIT', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '7 days') FROM generate_series(1, 2000);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SIGNUP', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '6 days') FROM generate_series(1, 900);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PROFILE_COMPLETE', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '5 days') FROM generate_series(1, 600);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'FIRST_CONTENT', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', NULL, NOW() - (random() * INTERVAL '4 days') FROM generate_series(1, 380);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'SUBSCRIBE', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', 1999, NOW() - (random() * INTERVAL '3 days') FROM generate_series(1, 200);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'PURCHASE', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', 1999, NOW() - (random() * INTERVAL '2 days') FROM generate_series(1, 110);

INSERT INTO conversion_events (id, session_id, user_id, stage, source, campaign, medium, referrer, landing_page, device_type, browser, country, value, created_at)
SELECT gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'REPEAT_PURCHASE', 'DIRECT', NULL, NULL, NULL, '/', 'desktop', 'Chrome', 'US', 2999, NOW() - (random() * INTERVAL '1 day') FROM generate_series(1, 35);

-- ============================================================================
-- Summary: Expected funnel data by source
-- ============================================================================
-- ORGANIC:  5000 → 2500 → 1800 → 1200 → 600 → 350 → 120 (7.0% overall)
-- PAID:     3000 → 1800 → 1400 → 1000 → 550 → 320 → 95  (10.7% overall)
-- SOCIAL:   4000 → 1200 → 700  → 400  → 180 → 85  → 25  (2.1% overall)
-- EMAIL:    1500 → 1100 → 950  → 800  → 450 → 280 → 110 (18.7% overall)
-- REFERRAL: 800  → 450  → 320  → 220  → 120 → 65  → 20  (8.1% overall)
-- DIRECT:   2000 → 900  → 600  → 380  → 200 → 110 → 35  (5.5% overall)
