import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import {
  casinoGames, coinPackages, bingoRooms, pokerTables, pokerTournaments,
  sportEvents, restrictedStates, users, wallets, staff
} from "../drizzle/schema.ts";
import crypto from "crypto";

const PROVIDERS = [
  "Pragmatic Play", "NetEnt", "Microgaming", "Play'n GO", "Evolution Gaming",
  "Betsoft", "Quickspin", "Red Tiger", "Thunderkick",
  "Big Time Gaming", "Relax Gaming", "Push Gaming", "Hacksaw Gaming", "NoLimit City",
  "Elk Studios", "Nolimit City", "Wazdan", "Kalamba Games", "Fantasma Games",
  "Stakelogic", "Playtech", "IGT", "WMS", "Aristocrat",
  "Blueprint Gaming", "Leander Games", "Nucleus Gaming", "Swintt", "Booming Games"
];

const SLOT_TITLES = [
  "Gates of Olympus", "Sweet Bonanza", "Wolf Gold", "The Dog House", "Fruit Party",
  "Starlight Princess", "Big Bass Bonanza", "Book of Dead", "Starburst", "Gonzo's Quest",
  "Mega Moolah", "Book of Ra", "Immortal Romance", "Thunderstruck II", "Avalon",
  "Bonanza", "White Rabbit", "Reactoonz", "Jammin' Jars", "Fire Joker",
  "Dead or Alive 2", "Razor Shark", "Money Train 2", "Money Train 3", "Tombstone RIP",
  "Mental", "Punk Toilet", "East Coast vs West Coast", "San Quentin", "Infectious 5",
  "Wanted Dead or a Wild", "Chaos Crew", "Chaos Crew 2", "Deadwood", "Tombstone",
  "The Rave", "Milky Way", "Limbo", "Folsom Prison", "Fire in the Hole",
  "Wild Chapo", "Pixies of the Forest", "Cleopatra", "Da Vinci Diamonds", "Wheel of Fortune",
  "Buffalo", "Quick Hit", "88 Fortunes", "Dancing Drums", "Dragon Link",
  "Lightning Link", "Jackpot Party", "Zeus", "Kronos", "Pompeii",
  "Ainsworth Dynamite", "Golden Goddess", "Kitty Glitter", "Lobstermania", "Hexbreaker",
  "Siberian Storm", "Cats", "Pharaoh's Fortune", "Coyote Moon", "Double Diamond",
  "Triple Diamond", "Red White Blue", "Wild Cherry", "Blazing 7s", "Lucky Larry's Lobstermania",
  "Mystical Unicorn", "Enchanted Unicorn", "Timber Wolf", "Jungle Wild", "Prowling Panther",
  "Crystal Forest", "Pixies of the Forest 2", "Emerald Isle", "Leprechaun's Fortune", "Lucky Larry",
  "Mayan Chief", "Aztec Riches", "Incan Adventure", "Montezuma", "Mayan Sun",
  "Fu Dao Le", "Fu Bat", "Fu Nan Fu Nu", "Zhao Cai Jin Bao", "Choy Sun Doa",
  "5 Dragons", "5 Dragons Gold", "5 Frogs", "5 Koi", "5 Frogs Gold",
  "50 Lions", "50 Dragons", "50 Horses", "100 Lions", "200 Lions",
  "Geisha", "Geisha Wonders", "Sakura Fortune", "Koi Princess", "Ninja Ways",
  "Samurai's Path", "Ninja Master", "Dragon Tiger", "Dragon's Luck", "Dragon's Fire",
  "Dragon Kingdom", "Dragon's Treasure", "Dragon's Myth", "Dragon's Reels", "Dragon Shrine",
  "Pirate's Plenty", "Pirate Gold", "Pirate Kingdom", "Pirates Charm", "Pirate's Map",
  "Treasure Island", "Treasure of the Pyramids", "Treasure Mine", "Treasure Nile", "Treasure Hunt",
  "Jungle Spirit", "Jungle Stripes", "Jungle Gorilla", "Jungle Giants", "Jungle Monkeys",
  "Wild Wild West", "Wild Wild Riches", "Wild Wild Spin", "Wild Toro", "Wild Turkey",
  "Wild Swarm", "Wild Seas", "Wild Rockets", "Wild Orient", "Wild Nords",
  "Mega Fortune", "Mega Fortune Dreams", "Mega Joker", "Mega Moolah Isis", "Mega Moolah Absolootly Mad",
  "Hall of Gods", "Divine Fortune", "Age of Gods", "Age of the Gods", "Age of Gods Furious 4",
  "Jackpot Giant", "Jackpot Jester", "Jackpot Rango", "Jackpot Raiders", "Jackpot Bells",
  "Medusa", "Medusa Megaways", "Medusa's Gaze", "Perseus vs Medusa", "Medusa Wild",
  "Hercules", "Hercules Son of Zeus", "Hercules and Pegasus", "Hercules High and Mighty", "Hercules Megaways",
  "Zeus III", "Zeus Ancient Fortunes", "Zeus Unleashed", "Zeus Lightning Power Reels", "Zeus Wild Thunder",
  "Poseidon", "Poseidon's Rising", "Poseidon's Kingdom", "Poseidon's Treasure", "Poseidon's Fortune",
  "Apollo God of the Sun", "Apollo Rising", "Apollo's Rising", "Apollo Power Reels", "Apollo Pays",
  "Athena", "Athena Rising", "Athena's Battle", "Athena's Fortune", "Athena Megaways",
  "Ares", "Ares God of War", "Ares Rising", "Ares Battle", "Ares Megaways",
  "Hades", "Hades Gigablox", "Hades Rising", "Hades Underworld", "Hades Fortune",
  "Pandora's Box", "Pandora's Fortune", "Pandora's Treasure", "Pandora's Wild", "Pandora's Riches",
  "Midas Golden Touch", "Midas Riches", "Midas Fortune", "Midas Wild", "Midas Touch",
  "Achilles", "Achilles Deluxe", "Achilles Rising", "Achilles Fortune", "Achilles Megaways",
  "Troy", "Troy Megaways", "Troy Rising", "Troy Fortune", "Troy Wild",
  "Odyssey", "Odyssey of the Gods", "Odyssey Rising", "Odyssey Fortune", "Odyssey Megaways",
  "Viking Runecraft", "Viking Voyage", "Vikings", "Viking Wild", "Viking Riches",
  "Viking Age", "Viking Fortune", "Viking Megaways", "Viking Rising", "Viking Gold",
  "Thor's Lightning", "Thor's Vengeance", "Thor Hammer Time", "Thor Megaways", "Thor Rising",
  "Odin's Riches", "Odin's Fortune", "Odin Megaways", "Odin Rising", "Odin's Wild",
  "Loki Wild Crash", "Loki's Fortune", "Loki Megaways", "Loki Rising", "Loki's Riches",
  "Freya's Fortune", "Freya Megaways", "Freya Rising", "Freya's Wild", "Freya's Riches",
  "Valhalla", "Valhalla Rising", "Valhalla Fortune", "Valhalla Megaways", "Valhalla Wild",
  "Asgard", "Asgard Rising", "Asgard Fortune", "Asgard Megaways", "Asgard Wild",
  "Midgard", "Midgard Rising", "Midgard Fortune", "Midgard Megaways", "Midgard Wild",
  "Ragnarok", "Ragnarok Rising", "Ragnarok Fortune", "Ragnarok Megaways", "Ragnarok Wild",
  "Egyptian Fortunes", "Egyptian Gold", "Egyptian Riches", "Egyptian Wild", "Egyptian Rising",
  "Cleopatra Gold", "Cleopatra Rising", "Cleopatra Fortune", "Cleopatra Megaways", "Cleopatra Wild",
  "Book of Cleopatra", "Book of Egypt", "Book of Fortune", "Book of Gold", "Book of Riches",
  "Book of Sun", "Book of Moon", "Book of Stars", "Book of Pyramids", "Book of Pharaoh",
  "Eye of Ra", "Eye of Horus", "Eye of Anubis", "Eye of Osiris", "Eye of Isis",
  "Anubis Rising", "Anubis Fortune", "Anubis Megaways", "Anubis Wild", "Anubis Gold",
  "Osiris Rising", "Osiris Fortune", "Osiris Megaways", "Osiris Wild", "Osiris Gold",
  "Isis Rising", "Isis Fortune", "Isis Megaways", "Isis Wild", "Isis Gold",
  "Ra Rising", "Ra Fortune", "Ra Megaways", "Ra Wild", "Ra Gold",
  "Sphinx Wild", "Sphinx Rising", "Sphinx Fortune", "Sphinx Megaways", "Sphinx Gold",
  "Pyramid Wild", "Pyramid Rising", "Pyramid Fortune", "Pyramid Megaways", "Pyramid Gold",
  "Pharaoh Wild", "Pharaoh Rising", "Pharaoh Fortune", "Pharaoh Megaways", "Pharaoh Gold",
  "Mummy Wild", "Mummy Rising", "Mummy Fortune", "Mummy Megaways", "Mummy Gold",
  "Scarab Wild", "Scarab Rising", "Scarab Fortune", "Scarab Megaways", "Scarab Gold",
  "Camel Wild", "Camel Rising", "Camel Fortune", "Camel Megaways", "Camel Gold",
  "Desert Wild", "Desert Rising", "Desert Fortune", "Desert Megaways", "Desert Gold",
  "Oasis Wild", "Oasis Rising", "Oasis Fortune", "Oasis Megaways", "Oasis Gold",
  "Safari Wild", "Safari Rising", "Safari Fortune", "Safari Megaways", "Safari Gold",
  "Lion Wild", "Lion Rising", "Lion Fortune", "Lion Megaways", "Lion Gold",
  "Elephant Wild", "Elephant Rising", "Elephant Fortune", "Elephant Megaways", "Elephant Gold",
  "Rhino Wild", "Rhino Rising", "Rhino Fortune", "Rhino Megaways", "Rhino Gold",
  "Hippo Wild", "Hippo Rising", "Hippo Fortune", "Hippo Megaways", "Hippo Gold",
  "Giraffe Wild", "Giraffe Rising", "Giraffe Fortune", "Giraffe Megaways", "Giraffe Gold",
  "Zebra Wild", "Zebra Rising", "Zebra Fortune", "Zebra Megaways", "Zebra Gold",
  "Cheetah Wild", "Cheetah Rising", "Cheetah Fortune", "Cheetah Megaways", "Cheetah Gold",
  "Leopard Wild", "Leopard Rising", "Leopard Fortune", "Leopard Megaways", "Leopard Gold",
  "Panther Wild", "Panther Rising", "Panther Fortune", "Panther Megaways", "Panther Gold",
  "Tiger Wild", "Tiger Rising", "Tiger Fortune", "Tiger Megaways", "Tiger Gold",
  "Bear Wild", "Bear Rising", "Bear Fortune", "Bear Megaways", "Bear Gold",
  "Wolf Wild", "Wolf Rising", "Wolf Fortune", "Wolf Megaways", "Wolf Gold",
  "Fox Wild", "Fox Rising", "Fox Fortune", "Fox Megaways", "Fox Gold",
  "Deer Wild", "Deer Rising", "Deer Fortune", "Deer Megaways", "Deer Gold",
  "Moose Wild", "Moose Rising", "Moose Fortune", "Moose Megaways", "Moose Gold",
  "Bison Wild", "Bison Rising", "Bison Fortune", "Bison Megaways", "Bison Gold",
  "Eagle Wild", "Eagle Rising", "Eagle Fortune", "Eagle Megaways", "Eagle Gold",
  "Hawk Wild", "Hawk Rising", "Hawk Fortune", "Hawk Megaways", "Hawk Gold",
  "Falcon Wild", "Falcon Rising", "Falcon Fortune", "Falcon Megaways", "Falcon Gold",
  "Owl Wild", "Owl Rising", "Owl Fortune", "Owl Megaways", "Owl Gold",
  "Raven Wild", "Raven Rising", "Raven Fortune", "Raven Megaways", "Raven Gold",
  "Phoenix Wild", "Phoenix Rising", "Phoenix Fortune", "Phoenix Megaways", "Phoenix Gold",
  "Dragon Wild", "Dragon Rising", "Dragon Fortune", "Dragon Megaways", "Dragon Gold",
  "Unicorn Wild", "Unicorn Rising", "Unicorn Fortune", "Unicorn Megaways", "Unicorn Gold",
  "Pegasus Wild", "Pegasus Rising", "Pegasus Fortune", "Pegasus Megaways", "Pegasus Gold",
  "Griffin Wild", "Griffin Rising", "Griffin Fortune", "Griffin Megaways", "Griffin Gold",
  "Kraken Wild", "Kraken Rising", "Kraken Fortune", "Kraken Megaways", "Kraken Gold",
  "Leviathan Wild", "Leviathan Rising", "Leviathan Fortune", "Leviathan Megaways", "Leviathan Gold",
  "Hydra Wild", "Hydra Rising", "Hydra Fortune", "Hydra Megaways", "Hydra Gold",
  "Cerberus Wild", "Cerberus Rising", "Cerberus Fortune", "Cerberus Megaways", "Cerberus Gold",
  "Minotaur Wild", "Minotaur Rising", "Minotaur Fortune", "Minotaur Megaways", "Minotaur Gold",
  "Cyclops Wild", "Cyclops Rising", "Cyclops Fortune", "Cyclops Megaways", "Cyclops Gold",
  "Centaur Wild", "Centaur Rising", "Centaur Fortune", "Centaur Megaways", "Centaur Gold",
  "Satyr Wild", "Satyr Rising", "Satyr Fortune", "Satyr Megaways", "Satyr Gold",
  "Faun Wild", "Faun Rising", "Faun Fortune", "Faun Megaways", "Faun Gold",
  "Nymph Wild", "Nymph Rising", "Nymph Fortune", "Nymph Megaways", "Nymph Gold",
  "Siren Wild", "Siren Rising", "Siren Fortune", "Siren Megaways", "Siren Gold",
  "Mermaid Wild", "Mermaid Rising", "Mermaid Fortune", "Mermaid Megaways", "Mermaid Gold",
  "Selkie Wild", "Selkie Rising", "Selkie Fortune", "Selkie Megaways", "Selkie Gold",
  "Kelpie Wild", "Kelpie Rising", "Kelpie Fortune", "Kelpie Megaways", "Kelpie Gold",
  "Banshee Wild", "Banshee Rising", "Banshee Fortune", "Banshee Megaways", "Banshee Gold",
  "Leprechaun Wild", "Leprechaun Rising", "Leprechaun Fortune", "Leprechaun Megaways", "Leprechaun Gold",
  "Fairy Wild", "Fairy Rising", "Fairy Fortune", "Fairy Megaways", "Fairy Gold",
  "Elf Wild", "Elf Rising", "Elf Fortune", "Elf Megaways", "Elf Gold",
  "Dwarf Wild", "Dwarf Rising", "Dwarf Fortune", "Dwarf Megaways", "Dwarf Gold",
  "Gnome Wild", "Gnome Rising", "Gnome Fortune", "Gnome Megaways", "Gnome Gold",
  "Troll Wild", "Troll Rising", "Troll Fortune", "Troll Megaways", "Troll Gold",
  "Ogre Wild", "Ogre Rising", "Ogre Fortune", "Ogre Megaways", "Ogre Gold",
  "Giant Wild", "Giant Rising", "Giant Fortune", "Giant Megaways", "Giant Gold",
  "Titan Wild", "Titan Rising", "Titan Fortune", "Titan Megaways", "Titan Gold",
  "Colossus Wild", "Colossus Rising", "Colossus Fortune", "Colossus Megaways", "Colossus Gold",
  "Behemoth Wild", "Behemoth Rising", "Behemoth Fortune", "Behemoth Megaways", "Behemoth Gold",
];

const CATEGORIES = ["slots", "slots", "slots", "slots", "table", "live", "jackpot", "new", "popular", "crash", "instant"] as const;
const VOLATILITIES = ["low", "medium", "high", "very_high"] as const;

function slugify(title: string, idx: number): string {
  return title.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/(^-|-$)/g, "") + "-" + idx;
}

async function seed() {
  const conn = await mysql.createConnection(process.env.DATABASE_URL!);
  const db = drizzle(conn);

  console.log("Seeding casino games...");
  const gamesData = SLOT_TITLES.map((title, i) => {
    const provider = PROVIDERS[i % PROVIDERS.length];
    const category = CATEGORIES[i % CATEGORIES.length];
    const volatility = VOLATILITIES[i % VOLATILITIES.length];
    const rtp = 92 + (i % 8);
    const reels = [3, 5, 5, 5, 6][i % 5];
    const paylines = [10, 20, 25, 40, 50, 100, 243, 1024][i % 8];
    return {
      slug: slugify(title, i),
      title,
      provider,
      category,
      volatility,
      rtp,
      reels,
      paylines,
      isActive: 1,
      isFeatured: i < 20,
      isNew: i < 50,
      playCount: Math.floor(Math.random() * 500000),
      description: `${title} is an exciting ${category} game from ${provider} with ${rtp}% RTP and ${volatility} volatility.`,
      tags: JSON.stringify([category, provider.toLowerCase().replace(/\s+/g, "-"), volatility]),
      features: JSON.stringify(["Wild", "Scatter", "Free Spins", ...(i % 3 === 0 ? ["Bonus Game"] : []), ...(i % 5 === 0 ? ["Multiplier"] : [])]),
    };
  });

  // Insert in batches of 50
  for (let i = 0; i < gamesData.length; i += 50) {
    const batch = gamesData.slice(i, i + 50);
    await db.insert(casinoGames).values(batch).onDuplicateKeyUpdate({ set: { slug: batch[0].slug } });
  }
  console.log(`Inserted ${gamesData.length} casino games`);

  // Seed coin packages
  console.log("Seeding coin packages...");
  await db.insert(coinPackages).values([
    { name: "Starter Pack", gcAmount: "5000.00", scBonusAmount: "0.00", priceUsd: "4.99", isPopular: false, isBestValue: false, sortOrder: 1 },
    { name: "Bronze Pack", gcAmount: "15000.00", scBonusAmount: "1.00", priceUsd: "9.99", isPopular: false, isBestValue: false, sortOrder: 2 },
    { name: "Silver Pack", gcAmount: "40000.00", scBonusAmount: "3.00", priceUsd: "24.99", isPopular: true, isBestValue: false, sortOrder: 3 },
    { name: "Gold Pack", gcAmount: "100000.00", scBonusAmount: "8.00", priceUsd: "49.99", isPopular: false, isBestValue: false, sortOrder: 4 },
    { name: "Platinum Pack", gcAmount: "250000.00", scBonusAmount: "20.00", priceUsd: "99.99", isPopular: false, isBestValue: true, sortOrder: 5 },
    { name: "Diamond Pack", gcAmount: "750000.00", scBonusAmount: "75.00", priceUsd: "249.99", isPopular: false, isBestValue: false, sortOrder: 6 },
    { name: "Elite Pack", gcAmount: "2000000.00", scBonusAmount: "200.00", priceUsd: "499.99", isPopular: false, isBestValue: false, sortOrder: 7 },
  ]).onDuplicateKeyUpdate({ set: { name: "Starter Pack" } });

  // Seed bingo rooms
  console.log("Seeding bingo rooms...");
  await db.insert(bingoRooms).values([
    { name: "Lucky Lounge", currency: "GC", ticketPrice: "100.00", maxCards: 4, jackpot: "50000.00", jackpotBallLimit: 76, status: "open" },
    { name: "SC1 Room", currency: "SC", ticketPrice: "1.00", maxCards: 4, jackpot: "500.00", jackpotBallLimit: 76, status: "open" },
    { name: "SC2 Room", currency: "SC", ticketPrice: "2.00", maxCards: 4, jackpot: "1000.00", jackpotBallLimit: 76, status: "open" },
    { name: "SC5 Room", currency: "SC", ticketPrice: "5.00", maxCards: 4, jackpot: "2500.00", jackpotBallLimit: 76, status: "open" },
    { name: "SC10 High Roller", currency: "SC", ticketPrice: "10.00", maxCards: 4, jackpot: "5000.00", jackpotBallLimit: 76, status: "open" },
  ]).onDuplicateKeyUpdate({ set: { name: "Lucky Lounge" } });

  // Seed poker tables
  console.log("Seeding poker tables...");
  await db.insert(pokerTables).values([
    { name: "Beginner's Table", gameType: "holdem", currency: "GC", smallBlind: "50.00", bigBlind: "100.00", minBuyIn: "5000.00", maxBuyIn: "20000.00", maxPlayers: 9, status: "waiting" },
    { name: "Low Stakes GC", gameType: "holdem", currency: "GC", smallBlind: "250.00", bigBlind: "500.00", minBuyIn: "25000.00", maxBuyIn: "100000.00", maxPlayers: 9, status: "waiting" },
    { name: "Mid Stakes GC", gameType: "holdem", currency: "GC", smallBlind: "1000.00", bigBlind: "2000.00", minBuyIn: "100000.00", maxBuyIn: "400000.00", maxPlayers: 6, status: "waiting" },
    { name: "SC Micro", gameType: "holdem", currency: "SC", smallBlind: "0.25", bigBlind: "0.50", minBuyIn: "25.00", maxBuyIn: "100.00", maxPlayers: 9, status: "waiting" },
    { name: "SC Low", gameType: "holdem", currency: "SC", smallBlind: "0.50", bigBlind: "1.00", minBuyIn: "50.00", maxBuyIn: "200.00", maxPlayers: 9, status: "waiting" },
    { name: "SC Mid", gameType: "holdem", currency: "SC", smallBlind: "2.50", bigBlind: "5.00", minBuyIn: "250.00", maxBuyIn: "1000.00", maxPlayers: 6, status: "waiting" },
    { name: "SC High Roller", gameType: "holdem", currency: "SC", smallBlind: "10.00", bigBlind: "20.00", minBuyIn: "1000.00", maxBuyIn: "5000.00", maxPlayers: 6, status: "waiting" },
  ]).onDuplicateKeyUpdate({ set: { name: "Beginner's Table" } });

  // Seed poker tournaments
  console.log("Seeding poker tournaments...");
  const tomorrow = new Date(Date.now() + 86400000);
  const nextWeek = new Date(Date.now() + 7 * 86400000);
  await db.insert(pokerTournaments).values([
    {
      name: "Daily GC Freeroll", currency: "GC", buyIn: "0.00", prizePool: "1000000.00",
      maxPlayers: 500, registeredPlayers: 0, startTime: tomorrow,
      status: "registering",
      structure: JSON.stringify({ levels: [{ level: 1, smallBlind: 500, bigBlind: 1000, duration: 15 }] }),
      payoutStructure: JSON.stringify([{ place: 1, percent: 30 }, { place: 2, percent: 20 }, { place: 3, percent: 10 }]),
    },
    {
      name: "Weekly SC Championship", currency: "SC", buyIn: "10.00", prizePool: "0.00",
      maxPlayers: 200, registeredPlayers: 0, startTime: nextWeek,
      status: "registering",
      structure: JSON.stringify({ levels: [{ level: 1, smallBlind: 25, bigBlind: 50, duration: 20 }] }),
      payoutStructure: JSON.stringify([{ place: 1, percent: 35 }, { place: 2, percent: 20 }, { place: 3, percent: 12 }]),
    },
  ]).onDuplicateKeyUpdate({ set: { name: "Daily GC Freeroll" } });

  // Seed sport events
  console.log("Seeding sport events...");
  const now = new Date();
  const inHour = new Date(now.getTime() + 3600000);
  const in2Hours = new Date(now.getTime() + 7200000);
  const in3Hours = new Date(now.getTime() + 10800000);
  const in6Hours = new Date(now.getTime() + 21600000);
  const tomorrow2 = new Date(now.getTime() + 86400000);
  const in2Days = new Date(now.getTime() + 2 * 86400000);

  await db.insert(sportEvents).values([
    { sport: "Football", league: "NFL", homeTeam: "Kansas City Chiefs", awayTeam: "San Francisco 49ers", startTime: inHour, status: "live", homeScore: 14, awayScore: 10, period: "Q2", odds: JSON.stringify({ homeWin: 1.85, awayWin: 2.10, draw: null, homeSpread: -3.5, awaySpread: 3.5, over: 47.5, under: 47.5 }) },
    { sport: "Football", league: "NFL", homeTeam: "Dallas Cowboys", awayTeam: "New York Giants", startTime: in2Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.65, awayWin: 2.40, draw: null, homeSpread: -6.5, awaySpread: 6.5, over: 44.5, under: 44.5 }) },
    { sport: "Football", league: "NFL", homeTeam: "Green Bay Packers", awayTeam: "Chicago Bears", startTime: in3Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.75, awayWin: 2.20, draw: null, homeSpread: -4.5, awaySpread: 4.5, over: 42.5, under: 42.5 }) },
    { sport: "Basketball", league: "NBA", homeTeam: "Los Angeles Lakers", awayTeam: "Boston Celtics", startTime: inHour, status: "live", homeScore: 68, awayScore: 72, period: "Q3", odds: JSON.stringify({ homeWin: 2.10, awayWin: 1.75, draw: null, homeSpread: 2.5, awaySpread: -2.5, over: 225.5, under: 225.5 }) },
    { sport: "Basketball", league: "NBA", homeTeam: "Golden State Warriors", awayTeam: "Miami Heat", startTime: in2Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.70, awayWin: 2.25, draw: null, homeSpread: -5.5, awaySpread: 5.5, over: 218.5, under: 218.5 }) },
    { sport: "Basketball", league: "NBA", homeTeam: "Milwaukee Bucks", awayTeam: "Philadelphia 76ers", startTime: in6Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.80, awayWin: 2.05, draw: null, homeSpread: -3.5, awaySpread: 3.5, over: 222.5, under: 222.5 }) },
    { sport: "Baseball", league: "MLB", homeTeam: "New York Yankees", awayTeam: "Boston Red Sox", startTime: in3Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.60, awayWin: 2.45, draw: null, runLine: -1.5, runLineOdds: 1.95, over: 8.5, under: 8.5 }) },
    { sport: "Baseball", league: "MLB", homeTeam: "Los Angeles Dodgers", awayTeam: "San Francisco Giants", startTime: in6Hours, status: "upcoming", odds: JSON.stringify({ homeWin: 1.55, awayWin: 2.55, draw: null, runLine: -1.5, runLineOdds: 1.90, over: 7.5, under: 7.5 }) },
    { sport: "Hockey", league: "NHL", homeTeam: "Toronto Maple Leafs", awayTeam: "Montreal Canadiens", startTime: tomorrow2, status: "upcoming", odds: JSON.stringify({ homeWin: 1.75, awayWin: 2.20, draw: 3.80, puckLine: -1.5, puckLineOdds: 2.10, over: 5.5, under: 5.5 }) },
    { sport: "Hockey", league: "NHL", homeTeam: "Colorado Avalanche", awayTeam: "Vegas Golden Knights", startTime: tomorrow2, status: "upcoming", odds: JSON.stringify({ homeWin: 1.90, awayWin: 2.00, draw: 4.00, puckLine: -1.5, puckLineOdds: 2.25, over: 6.0, under: 6.0 }) },
    { sport: "Soccer", league: "Premier League", homeTeam: "Manchester City", awayTeam: "Arsenal", startTime: in2Days, status: "upcoming", odds: JSON.stringify({ homeWin: 1.80, awayWin: 4.50, draw: 3.60, homeSpread: -0.5, awaySpread: 0.5, over: 2.5, under: 2.5 }) },
    { sport: "Soccer", league: "La Liga", homeTeam: "Real Madrid", awayTeam: "FC Barcelona", startTime: in2Days, status: "upcoming", odds: JSON.stringify({ homeWin: 2.10, awayWin: 3.40, draw: 3.20, homeSpread: 0, awaySpread: 0, over: 2.5, under: 2.5 }) },
    { sport: "MMA", league: "UFC", homeTeam: "Jon Jones", awayTeam: "Stipe Miocic", startTime: in2Days, status: "upcoming", odds: JSON.stringify({ homeWin: 1.40, awayWin: 3.00, draw: null }) },
    { sport: "Boxing", league: "WBC", homeTeam: "Canelo Alvarez", awayTeam: "Jermall Charlo", startTime: in2Days, status: "upcoming", odds: JSON.stringify({ homeWin: 1.35, awayWin: 3.25, draw: 15.00 }) },
    { sport: "Tennis", league: "ATP Masters", homeTeam: "Novak Djokovic", awayTeam: "Carlos Alcaraz", startTime: tomorrow2, status: "upcoming", odds: JSON.stringify({ homeWin: 1.95, awayWin: 1.90, draw: null }) },
  ]).onDuplicateKeyUpdate({ set: { sport: "Football" } });

  // Seed restricted states
  console.log("Seeding restricted states...");
  await db.insert(restrictedStates).values([
    { stateCode: "WA", stateName: "Washington", reason: "State law prohibits sweepstakes gaming", isActive: 1 },
    { stateCode: "ID", stateName: "Idaho", reason: "State law prohibits sweepstakes gaming", isActive: 1 },
    { stateCode: "MT", stateName: "Montana", reason: "State law prohibits sweepstakes gaming", isActive: 1 },
    { stateCode: "NV", stateName: "Nevada", reason: "State gaming regulations restrict sweepstakes", isActive: 1 },
    { stateCode: "KY", stateName: "Kentucky", reason: "State law prohibits sweepstakes gaming", isActive: 1 },
  ]).onDuplicateKeyUpdate({ set: { stateCode: "WA" } });

  // Seed admin user
  console.log("Seeding admin user...");
  const adminHash = crypto.createHash("sha256").update("Default").digest("hex");
  await db.insert(users).values({
    openId: "admin-coinkrazy-001",
    email: "admin@playcoinkrazy.com",
    passwordHash: adminHash,
    name: "CoinKrazy Admin",
    role: "admin",
    loginMethod: "local",
  }).onDuplicateKeyUpdate({ set: { name: "CoinKrazy Admin" } });

  // Create admin wallet
  const adminUsers = await db.select({
    id: users.id,
    openId: users.openId,
    name: users.name,
    email: users.email,
    loginMethod: users.loginMethod,
    role: users.role,
    createdAt: users.createdAt,
    updatedAt: users.updatedAt,
    lastSignedIn: users.lastSignedIn,
  }).from(users).limit(1);
  if (adminUsers.length > 0) {
    await db.insert(wallets).values({
      userId: adminUsers[0].id,
      gcBalance: "1000000.00",
      scBalance: "10000.00",
    }).onDuplicateKeyUpdate({ set: { gcBalance: "1000000.00" } });
  }

  console.log("Seed complete!");
  await conn.end();
}

seed().catch(console.error);
