import { usePrisma } from "../../utils/prisma";

/**
 * GET /api/providers
 * Liefert die Liste der in Deutschland lizenzierten Glücksspiel-Anbieter (GGL-Whitelist).
 * Quelle: Gemeinsame Glücksspielbehörde der Länder (ggl-behoerde.de)
 * Diese Daten werden statisch gepflegt + aus der DB ergänzt.
 */

// Statische GGL-lizenzierte Anbieter (Stand: 2026)
// Kategorie: sportwetten | casino | poker | slots
const GGL_PROVIDERS = [
  // Sportwetten-Lizenzen
  {
    name: "bet365",
    domain: "bet365.com",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "Tipico",
    domain: "tipico.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "bwin",
    domain: "bwin.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "Betano",
    domain: "betano.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "Betway",
    domain: "betway.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "ODDSET",
    domain: "oddset.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "Interwetten",
    domain: "interwetten.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "Unibet",
    domain: "unibet.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "888sport",
    domain: "888sport.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "hoch",
  },
  {
    name: "Betsson",
    domain: "betsson.com",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "NEObet",
    domain: "neobet.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "AdmiralBet",
    domain: "admiralbet.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "Winamax",
    domain: "winamax.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  {
    name: "Merkur Sports",
    domain: "merkursports.de",
    category: "sportwetten",
    license: "GGL-SW",
    risk: "mittel",
  },
  // Virtuelle Automatenspiele
  {
    name: "Merkur Slots",
    domain: "merkur-slots.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Jackpotpiraten",
    domain: "jackpotpiraten.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Jokerstar",
    domain: "jokerstar.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "BingBong",
    domain: "bingbong.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Slotmagie",
    domain: "slotmagie.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Drückglück",
    domain: "drueckglueck.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Hyperino",
    domain: "hyperino.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Löwen Play",
    domain: "loewenplay.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  {
    name: "Sunmaker",
    domain: "sunmaker.de",
    category: "slots",
    license: "GGL-VA",
    risk: "sehr hoch",
  },
  // Poker
  {
    name: "PokerStars",
    domain: "pokerstars.de",
    category: "poker",
    license: "GGL-PK",
    risk: "hoch",
  },
  {
    name: "GGPoker",
    domain: "ggpoker.de",
    category: "poker",
    license: "GGL-PK",
    risk: "hoch",
  },
  {
    name: "888poker",
    domain: "888poker.de",
    category: "poker",
    license: "GGL-PK",
    risk: "hoch",
  },
  // Illegale / häufig genutzte ohne DE-Lizenz
  {
    name: "Stake",
    domain: "stake.com",
    category: "casino",
    license: "keine (Curaçao)",
    risk: "extrem",
  },
  {
    name: "Rollbit",
    domain: "rollbit.com",
    category: "casino",
    license: "keine",
    risk: "extrem",
  },
  {
    name: "Roobet",
    domain: "roobet.com",
    category: "casino",
    license: "keine (Curaçao)",
    risk: "extrem",
  },
  {
    name: "Gamdom",
    domain: "gamdom.com",
    category: "casino",
    license: "keine (Curaçao)",
    risk: "extrem",
  },
  {
    name: "CSGORoll",
    domain: "csgoroll.com",
    category: "casino",
    license: "keine",
    risk: "extrem",
  },
  {
    name: "Duelbits",
    domain: "duelbits.com",
    category: "casino",
    license: "keine (Curaçao)",
    risk: "extrem",
  },
];

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const category = query.category as string | undefined;

  let providers = [...GGL_PROVIDERS];

  // Optionale DB-Erweiterung: community-gemeldete Anbieter
  try {
    const db = usePrisma();
    const data = await db.blocklistDomain.findMany({
      where: { isActive: true, reportCount: { gt: 5 } },
      select: { domain: true, source: true },
      take: 50,
    });

    for (const d of data) {
      if (!providers.find((p) => p.domain === d.domain)) {
        providers.push({
          name: d.domain.replace(/\.\w+$/, ""),
          domain: d.domain,
          category: "casino",
          license: "unbekannt",
          risk: "hoch",
        });
      }
    }
  } catch {
    // DB nicht erreichbar → nur statische Daten
  }

  // Filter nach Kategorie
  if (category && category !== "all") {
    providers = providers.filter((p) => p.category === category);
  }

  return {
    total: providers.length,
    providers,
    source: "GGL Whitelist + Community Reports",
    lastUpdated: "2026-04-01",
  };
});
