-- Add sample territories for testing
-- This creates realistic territory data for the Territory Manager system

-- Insert sample territories
INSERT INTO territories (id, name, boundary, population, market_size, is_active, created_at, updated_at) VALUES
(
  'territory-denver-metro',
  'Denver Metro Area',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-105.1178, 39.7392],
      [-104.8736, 39.7392],
      [-104.8736, 39.9142],
      [-105.1178, 39.9142],
      [-105.1178, 39.7392]
    ]]
  }',
  2963821,
  'large',
  true,
  NOW(),
  NOW()
),
(
  'territory-boulder-county',
  'Boulder County',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-105.6000, 39.9000],
      [-105.0000, 39.9000],
      [-105.0000, 40.3000],
      [-105.6000, 40.3000],
      [-105.6000, 39.9000]
    ]]
  }',
  330758,
  'medium',
  true,
  NOW(),
  NOW()
),
(
  'territory-colorado-springs',
  'Colorado Springs',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-104.9000, 38.7000],
      [-104.6000, 38.7000],
      [-104.6000, 39.0000],
      [-104.9000, 39.0000],
      [-104.9000, 38.7000]
    ]]
  }',
  715522,
  'medium',
  true,
  NOW(),
  NOW()
),
(
  'territory-fort-collins',
  'Fort Collins',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-105.2000, 40.4000],
      [-105.0000, 40.4000],
      [-105.0000, 40.7000],
      [-105.2000, 40.7000],
      [-105.2000, 40.4000]
    ]]
  }',
  169810,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-grand-junction',
  'Grand Junction',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-108.7000, 39.0000],
      [-108.4000, 39.0000],
      [-108.4000, 39.2000],
      [-108.7000, 39.2000],
      [-108.7000, 39.0000]
    ]]
  }',
  65560,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-aspen-vail',
  'Aspen & Vail Valley',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-106.9000, 39.4000],
      [-106.2000, 39.4000],
      [-106.2000, 39.7000],
      [-106.9000, 39.7000],
      [-106.9000, 39.4000]
    ]]
  }',
  25000,
  'premium',
  true,
  NOW(),
  NOW()
),
(
  'territory-steamboat-springs',
  'Steamboat Springs',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-106.9000, 40.4000],
      [-106.7000, 40.4000],
      [-106.7000, 40.6000],
      [-106.9000, 40.6000],
      [-106.9000, 40.4000]
    ]]
  }',
  13224,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-durango',
  'Durango & Southwest Colorado',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-108.0000, 37.0000],
      [-107.5000, 37.0000],
      [-107.5000, 37.5000],
      [-108.0000, 37.5000],
      [-108.0000, 37.0000]
    ]]
  }',
  52000,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-pueblo',
  'Pueblo',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-104.8000, 38.1000],
      [-104.4000, 38.1000],
      [-104.4000, 38.4000],
      [-104.8000, 38.4000],
      [-104.8000, 38.1000]
    ]]
  }',
  111876,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-greeley',
  'Greeley & Weld County',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-104.9000, 40.3000],
      [-104.5000, 40.3000],
      [-104.5000, 40.6000],
      [-104.9000, 40.6000],
      [-104.9000, 40.3000]
    ]]
  }',
  328981,
  'medium',
  true,
  NOW(),
  NOW()
),
(
  'territory-loveland',
  'Loveland',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-105.2000, 40.2000],
      [-105.0000, 40.2000],
      [-105.0000, 40.5000],
      [-105.2000, 40.5000],
      [-105.2000, 40.2000]
    ]]
  }',
  76378,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-longmont',
  'Longmont',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-105.2000, 40.1000],
      [-105.0000, 40.1000],
      [-105.0000, 40.3000],
      [-105.2000, 40.3000],
      [-105.2000, 40.1000]
    ]]
  }',
  98885,
  'small',
  true,
  NOW(),
  NOW()
),
(
  'territory-summit-county',
  'Summit County (Breckenridge, Keystone)',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-106.2000, 39.4000],
      [-105.9000, 39.4000],
      [-105.9000, 39.7000],
      [-106.2000, 39.7000],
      [-106.2000, 39.4000]
    ]]
  }',
  31055,
  'premium',
  true,
  NOW(),
  NOW()
),
(
  'territory-eagle-county',
  'Eagle County (Vail, Beaver Creek)',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-106.7000, 39.5000],
      [-106.3000, 39.5000],
      [-106.3000, 39.8000],
      [-106.7000, 39.8000],
      [-106.7000, 39.5000]
    ]]
  }',
  58731,
  'premium',
  true,
  NOW(),
  NOW()
),
(
  'territory-telluride',
  'Telluride',
  '{
    "type": "Polygon",
    "coordinates": [[
      [-108.0000, 37.8000],
      [-107.8000, 37.8000],
      [-107.8000, 38.0000],
      [-108.0000, 38.0000],
      [-108.0000, 37.8000]
    ]]
  }',
  2607,
  'premium',
  true,
  NOW(),
  NOW()
);

-- Add some sample territory managers (for testing purposes)
INSERT INTO territory_managers (id, user_id, territory_id, referral_code, status, application_date, approved_date, profile, earnings_data, created_at, updated_at) VALUES
(
  'tm-john-denver',
  'user-john-denver',
  'territory-denver-metro',
  'DENVER001',
  'active',
  NOW() - INTERVAL '30 days',
  NOW() - INTERVAL '25 days',
  '{
    "full_name": "John Denver",
    "phone": "+1-303-555-0101",
    "address": {
      "street": "123 Main St",
      "city": "Denver",
      "state": "CO",
      "zip_code": "80202",
      "country": "US"
    },
    "has_transportation": true,
    "has_event_experience": true,
    "motivation": "I love helping local events succeed and have extensive experience in event management."
  }',
  '{
    "total_commission": 3250.00,
    "current_month_commission": 850.00,
    "total_events_referred": 12,
    "success_rate": 0.85,
    "average_commission_per_event": 270.83
  }',
  NOW() - INTERVAL '30 days',
  NOW()
),
(
  'tm-sarah-boulder',
  'user-sarah-boulder',
  'territory-boulder-county',
  'BOULDER01',
  'active',
  NOW() - INTERVAL '45 days',
  NOW() - INTERVAL '40 days',
  '{
    "full_name": "Sarah Mitchell",
    "phone": "+1-303-555-0102",
    "address": {
      "street": "456 Pearl St",
      "city": "Boulder",
      "state": "CO",
      "zip_code": "80302",
      "country": "US"
    },
    "has_transportation": true,
    "has_event_experience": true,
    "motivation": "Boulder has an amazing event scene and I want to help connect organizers with the best ticketing solutions."
  }',
  '{
    "total_commission": 2180.00,
    "current_month_commission": 620.00,
    "total_events_referred": 8,
    "success_rate": 0.92,
    "average_commission_per_event": 272.50
  }',
  NOW() - INTERVAL '45 days',
  NOW()
),
(
  'tm-mike-springs',
  'user-mike-springs',
  'territory-colorado-springs',
  'SPRINGS01',
  'active',
  NOW() - INTERVAL '20 days',
  NOW() - INTERVAL '15 days',
  '{
    "full_name": "Mike Thompson",
    "phone": "+1-719-555-0103",
    "address": {
      "street": "789 Pikes Peak Ave",
      "city": "Colorado Springs",
      "state": "CO",
      "zip_code": "80903",
      "country": "US"
    },
    "has_transportation": true,
    "has_event_experience": false,
    "motivation": "New to events but excited to learn and help grow the Colorado Springs event community."
  }',
  '{
    "total_commission": 480.00,
    "current_month_commission": 320.00,
    "total_events_referred": 3,
    "success_rate": 0.75,
    "average_commission_per_event": 160.00
  }',
  NOW() - INTERVAL '20 days',
  NOW()
);

-- Add sample leads
INSERT INTO leads (id, territory_manager_id, event_name, organizer_contact, event_details, status, notes, follow_up_date, created_at, updated_at) VALUES
(
  'lead-wedding-denver',
  'tm-john-denver',
  'Johnson Wedding Reception',
  '{
    "name": "Emily Johnson",
    "email": "emily.johnson@email.com",
    "phone": "+1-303-555-0201",
    "organization": "Johnson Family",
    "title": "Bride"
  }',
  '{
    "event_type": "Wedding",
    "event_date": "2025-08-15",
    "venue": "Denver Art Museum",
    "expected_attendance": 150,
    "budget_range": "$5,000-$10,000",
    "description": "Elegant wedding reception with cocktail hour and dinner"
  }',
  'confirmed',
  '["Initial contact made", "Venue confirmed", "Date set", "Waiting for final guest count"]',
  NOW() + INTERVAL '7 days',
  NOW() - INTERVAL '5 days',
  NOW()
),
(
  'lead-festival-boulder',
  'tm-sarah-boulder',
  'Boulder Music Festival',
  '{
    "name": "David Chen",
    "email": "david@bouldermusic.org",
    "phone": "+1-303-555-0202",
    "organization": "Boulder Music Association",
    "title": "Event Director"
  }',
  '{
    "event_type": "Music Festival",
    "event_date": "2025-07-20",
    "venue": "Chautauqua Park",
    "expected_attendance": 500,
    "budget_range": "$15,000-$25,000",
    "description": "Annual outdoor music festival featuring local and regional artists"
  }',
  'contacted',
  '["Sent initial proposal", "Discussed pricing options", "Awaiting decision"]',
  NOW() + INTERVAL '3 days',
  NOW() - INTERVAL '2 days',
  NOW()
),
(
  'lead-gala-denver',
  'tm-john-denver',
  'Children\'s Hospital Charity Gala',
  '{
    "name": "Lisa Rodriguez",
    "email": "lisa.rodriguez@childrenshospital.org",
    "phone": "+1-303-555-0203",
    "organization": "Children\'s Hospital Colorado",
    "title": "Development Manager"
  }',
  '{
    "event_type": "Charity Gala",
    "event_date": "2025-09-12",
    "venue": "Union Station",
    "expected_attendance": 300,
    "budget_range": "$20,000-$30,000",
    "description": "Annual fundraising gala with silent auction and dinner"
  }',
  'cold',
  '["Need to follow up with initial contact"]',
  NOW() + INTERVAL '1 day',
  NOW() - INTERVAL '1 day',
  NOW()
),
(
  'lead-corporate-springs',
  'tm-mike-springs',
  'Tech Company Annual Conference',
  '{
    "name": "Robert Kim",
    "email": "robert.kim@techcorp.com",
    "phone": "+1-719-555-0204",
    "organization": "TechCorp Solutions",
    "title": "HR Director"
  }',
  '{
    "event_type": "Corporate Conference",
    "event_date": "2025-08-05",
    "venue": "The Broadmoor",
    "expected_attendance": 200,
    "budget_range": "$10,000-$15,000",
    "description": "Annual company conference with keynotes and networking"
  }',
  'converted',
  '["Successfully converted to BCT customer", "Event setup complete", "Tickets selling well"]',
  NULL,
  NOW() - INTERVAL '10 days',
  NOW()
);

-- Add sample commissions
INSERT INTO commissions (id, territory_manager_id, event_id, tickets_sold, commission_per_ticket, total_commission, status, payout_date, created_at, updated_at) VALUES
(
  'comm-springs-tech',
  'tm-mike-springs',
  'event-tech-conference',
  180,
  0.40,
  72.00,
  'paid',
  NOW() - INTERVAL '5 days',
  NOW() - INTERVAL '10 days',
  NOW()
),
(
  'comm-denver-wedding',
  'tm-john-denver',
  'event-smith-wedding',
  120,
  0.40,
  48.00,
  'paid',
  NOW() - INTERVAL '2 days',
  NOW() - INTERVAL '15 days',
  NOW()
),
(
  'comm-boulder-concert',
  'tm-sarah-boulder',
  'event-boulder-concert',
  250,
  0.40,
  100.00,
  'unpaid',
  NULL,
  NOW() - INTERVAL '3 days',
  NOW()
);

-- Add sample notifications
INSERT INTO tm_notifications (id, territory_manager_id, type, title, message, data, read, created_at) VALUES
(
  'notif-john-payout',
  'tm-john-denver',
  'payout',
  'Commission Payout Processed',
  'Your commission of $48.00 for the Smith Wedding has been processed and will arrive in 2-3 business days.',
  '{"amount": 48.00, "event_name": "Smith Wedding"}',
  false,
  NOW() - INTERVAL '2 days'
),
(
  'notif-sarah-lead',
  'tm-sarah-boulder',
  'lead_update',
  'Lead Status Updated',
  'Your lead for Boulder Music Festival has been updated to "contacted" status.',
  '{"lead_id": "lead-festival-boulder", "status": "contacted"}',
  false,
  NOW() - INTERVAL '1 day'
),
(
  'notif-mike-achievement',
  'tm-mike-springs',
  'achievement',
  'Achievement Unlocked: First Conversion!',
  'Congratulations! You\'ve successfully converted your first lead. You\'ve earned the "First Conversion" achievement and a $50 bonus.',
  '{"achievement_name": "First Conversion", "bonus": 50.00}',
  false,
  NOW() - INTERVAL '3 days'
),
(
  'notif-john-system',
  'tm-john-denver',
  'system',
  'New Marketing Materials Available',
  'We\'ve added new marketing materials to your toolkit including updated flyers and email templates.',
  '{"materials_count": 3}',
  true,
  NOW() - INTERVAL '5 days'
);

-- Update territories with assignments
UPDATE territories SET assigned_to = 'tm-john-denver' WHERE id = 'territory-denver-metro';
UPDATE territories SET assigned_to = 'tm-sarah-boulder' WHERE id = 'territory-boulder-county';
UPDATE territories SET assigned_to = 'tm-mike-springs' WHERE id = 'territory-colorado-springs';