const CATEGORIES = {
  vinted: [
    { id: 5, name: 'Damen', children: [
      { id: 1904, name: 'Kleidung', children: [
        { id: 1, name: 'Tops & T-Shirts' }, { id: 2, name: 'Pullover & Strickjacken' },
        { id: 3, name: 'Blusen & Hemden' }, { id: 4, name: 'Kleider' }, { id: 6, name: 'Röcke' },
        { id: 7, name: 'Hosen & Leggings' }, { id: 8, name: 'Jeans' }, { id: 9, name: 'Jacken & Mäntel' },
        { id: 10, name: 'Anzüge & Blazer' }, { id: 11, name: 'Sportbekleidung' }, { id: 12, name: 'Bademode' },
        { id: 13, name: 'Unterwäsche & Nachtwäsche' }, { id: 15, name: 'Overalls & Jumpsuits' },
        { id: 1226, name: 'Shorts' },
      ]},
      { id: 1905, name: 'Schuhe', children: [
        { id: 17, name: 'Sneaker' }, { id: 18, name: 'Stiefel & Stiefeletten' },
        { id: 19, name: 'Pumps & High Heels' }, { id: 20, name: 'Sandalen' }, { id: 21, name: 'Ballerinas' },
        { id: 23, name: 'Sportschuhe' },
      ]},
      { id: 1906, name: 'Taschen', children: [
        { id: 25, name: 'Handtaschen' }, { id: 26, name: 'Umhängetaschen' }, { id: 27, name: 'Rucksäcke' },
        { id: 28, name: 'Clutches' }, { id: 29, name: 'Geldbörsen & Etuis' },
      ]},
      { id: 1907, name: 'Accessoires', children: [
        { id: 31, name: 'Schmuck' }, { id: 32, name: 'Uhren' }, { id: 33, name: 'Sonnenbrillen' },
        { id: 34, name: 'Schals & Tücher' }, { id: 35, name: 'Gürtel' }, { id: 36, name: 'Mützen & Hüte' },
      ]},
      { id: 1919, name: 'Pflege & Kosmetik', children: [
        { id: 38, name: 'Makeup' }, { id: 39, name: 'Hautpflege' }, { id: 41, name: 'Parfum' },
      ]},
    ]},
    { id: 1206, name: 'Herren', children: [
      { id: 1908, name: 'Kleidung', children: [
        { id: 50, name: 'T-Shirts & Poloshirts' }, { id: 51, name: 'Pullover & Hoodies' },
        { id: 52, name: 'Hemden' }, { id: 53, name: 'Hosen' }, { id: 54, name: 'Jeans' },
        { id: 55, name: 'Shorts' }, { id: 56, name: 'Jacken & Mäntel' }, { id: 57, name: 'Anzüge & Sakkos' },
        { id: 58, name: 'Sportbekleidung' }, { id: 60, name: 'Unterwäsche & Nachtwäsche' },
      ]},
      { id: 1909, name: 'Schuhe', children: [
        { id: 62, name: 'Sneaker' }, { id: 63, name: 'Stiefel & Stiefeletten' },
        { id: 64, name: 'Business-Schuhe' }, { id: 65, name: 'Sandalen' }, { id: 66, name: 'Sportschuhe' },
      ]},
      { id: 1910, name: 'Taschen', children: [
        { id: 68, name: 'Rucksäcke' }, { id: 69, name: 'Umhängetaschen' }, { id: 71, name: 'Geldbörsen' },
      ]},
      { id: 1911, name: 'Accessoires', children: [
        { id: 72, name: 'Uhren' }, { id: 73, name: 'Sonnenbrillen' }, { id: 74, name: 'Krawatten & Fliegen' },
        { id: 75, name: 'Gürtel' }, { id: 76, name: 'Mützen & Hüte' }, { id: 78, name: 'Schmuck' },
      ]},
    ]},
    { id: 1207, name: 'Kinder', children: [
      { id: 1912, name: 'Mädchen-Kleidung', children: [
        { id: 100, name: 'Tops & T-Shirts' }, { id: 102, name: 'Kleider & Röcke' },
        { id: 103, name: 'Hosen & Jeans' }, { id: 104, name: 'Jacken & Mäntel' },
      ]},
      { id: 1913, name: 'Jungen-Kleidung', children: [
        { id: 110, name: 'Tops & T-Shirts' }, { id: 111, name: 'Pullover & Hoodies' },
        { id: 112, name: 'Hosen & Jeans' }, { id: 113, name: 'Jacken & Mäntel' },
      ]},
      { id: 1915, name: 'Kinderschuhe', children: [
        { id: 130, name: 'Sneaker' }, { id: 131, name: 'Stiefel' }, { id: 132, name: 'Sandalen' },
      ]},
      { id: 1916, name: 'Spielzeug', children: [
        { id: 140, name: 'Puppen' }, { id: 142, name: 'Bausteine' }, { id: 143, name: 'Kuscheltiere' },
        { id: 144, name: 'Brettspiele & Puzzles' },
      ]},
    ]},
    { id: 1208, name: 'Haus & Wohnen', children: [
      { id: 1917, name: 'Dekoration', children: [
        { id: 200, name: 'Bilder & Poster' }, { id: 201, name: 'Kerzen' }, { id: 202, name: 'Vasen' },
      ]},
      { id: 245, name: 'Küche', children: [
        { id: 210, name: 'Geschirr' }, { id: 211, name: 'Gläser & Tassen' }, { id: 213, name: 'Küchengeräte' },
      ]},
      { id: 246, name: 'Textilien', children: [
        { id: 220, name: 'Bettwäsche' }, { id: 222, name: 'Kissen & Decken' }, { id: 223, name: 'Teppiche' },
      ]},
      { id: 247, name: 'Möbel', children: [
        { id: 230, name: 'Regale' }, { id: 231, name: 'Tische' }, { id: 232, name: 'Stühle' },
      ]},
    ]},
    { id: 1209, name: 'Unterhaltung', children: [
      { id: 249, name: 'Bücher', children: [
        { id: 300, name: 'Romane' }, { id: 301, name: 'Sachbücher' }, { id: 303, name: 'Comics & Manga' },
      ]},
      { id: 251, name: 'Spiele & Konsolen', children: [
        { id: 320, name: 'Videospiele' }, { id: 321, name: 'Konsolen' }, { id: 323, name: 'Brettspiele' },
      ]},
      { id: 252, name: 'Sport', children: [
        { id: 330, name: 'Fitnessgeräte' }, { id: 331, name: 'Fahrräder' }, { id: 332, name: 'Camping' },
      ]},
    ]},
    { id: 1210, name: 'Elektronik', children: [
      { id: 253, name: 'Handys', children: [
        { id: 400, name: 'iPhones' }, { id: 401, name: 'Samsung' }, { id: 402, name: 'Sonstige Smartphones' },
        { id: 403, name: 'Handyhüllen & Zubehör' },
      ]},
      { id: 254, name: 'Computer & Tablets', children: [
        { id: 410, name: 'Laptops' }, { id: 411, name: 'Tablets' }, { id: 414, name: 'NAS & Server' },
        { id: 417, name: 'PC-Zubehör' },
      ]},
      { id: 255, name: 'Foto & Kamera', children: [
        { id: 420, name: 'Digitalkameras' }, { id: 422, name: 'Kamerazubehör' },
      ]},
      { id: 256, name: 'Audio & HiFi', children: [
        { id: 430, name: 'Kopfhörer' }, { id: 431, name: 'Lautsprecher' },
      ]},
    ]},
  ],

  'ebay-ka': [
    { id: 'mode', name: 'Mode & Beauty', children: [
      { id: 'damen', name: 'Damenmode', children: [
        { id: 'dk1', name: 'Oberteile & T-Shirts' }, { id: 'dk2', name: 'Kleider' },
        { id: 'dk3', name: 'Hosen & Jeans' }, { id: 'dk4', name: 'Jacken & Mäntel' },
        { id: 'dk5', name: 'Pullover & Strickwaren' }, { id: 'dk6', name: 'Schuhe' },
        { id: 'dk7', name: 'Taschen' }, { id: 'dk8', name: 'Accessoires' },
      ]},
      { id: 'herren', name: 'Herrenmode', children: [
        { id: 'hk1', name: 'T-Shirts & Hemden' }, { id: 'hk2', name: 'Hosen & Jeans' },
        { id: 'hk3', name: 'Jacken & Mäntel' }, { id: 'hk4', name: 'Pullover & Hoodies' },
        { id: 'hk5', name: 'Schuhe' }, { id: 'hk6', name: 'Accessoires' },
      ]},
      { id: 'beauty', name: 'Beauty & Gesundheit', children: [
        { id: 'bk1', name: 'Parfum' }, { id: 'bk2', name: 'Hautpflege' }, { id: 'bk3', name: 'Makeup' },
      ]},
    ]},
    { id: 'elektronik', name: 'Elektronik', children: [
      { id: 'handy', name: 'Handy & Telefon', children: [
        { id: 'ek1', name: 'Smartphones' }, { id: 'ek2', name: 'Zubehör' },
      ]},
      { id: 'pc', name: 'PC & Zubehör', children: [
        { id: 'ek3', name: 'Notebooks & Laptops' }, { id: 'ek4', name: 'PCs & Desktops' },
        { id: 'ek5', name: 'Monitore' }, { id: 'ek6', name: 'Netzwerk & Server' },
        { id: 'ek7', name: 'Drucker & Scanner' }, { id: 'ek8', name: 'Zubehör' },
      ]},
      { id: 'audio', name: 'Audio & HiFi', children: [
        { id: 'ek9', name: 'Kopfhörer' }, { id: 'ek10', name: 'Lautsprecher' }, { id: 'ek11', name: 'HiFi-Anlagen' },
      ]},
      { id: 'foto', name: 'Foto & Kamera', children: [
        { id: 'ek12', name: 'Digitalkameras' }, { id: 'ek13', name: 'Objektive' }, { id: 'ek14', name: 'Zubehör' },
      ]},
      { id: 'tv', name: 'TV & Video', children: [
        { id: 'ek15', name: 'Fernseher' }, { id: 'ek16', name: 'Streaming' },
      ]},
      { id: 'konsolen', name: 'Konsolen & Spiele', children: [
        { id: 'ek17', name: 'Konsolen' }, { id: 'ek18', name: 'Spiele' }, { id: 'ek19', name: 'Zubehör' },
      ]},
    ]},
    { id: 'haus', name: 'Haus & Garten', children: [
      { id: 'moebel', name: 'Möbel', children: [
        { id: 'hk1', name: 'Tische' }, { id: 'hk2', name: 'Stühle' }, { id: 'hk3', name: 'Regale & Schränke' },
      ]},
      { id: 'deko', name: 'Dekoration', children: [
        { id: 'hk4', name: 'Bilder & Poster' }, { id: 'hk5', name: 'Vasen & Kerzen' },
      ]},
      { id: 'kueche', name: 'Küche', children: [
        { id: 'hk6', name: 'Geschirr' }, { id: 'hk7', name: 'Geräte' },
      ]},
      { id: 'garten', name: 'Garten', children: [
        { id: 'hk8', name: 'Gartenmöbel' }, { id: 'hk9', name: 'Geräte & Werkzeug' },
      ]},
    ]},
    { id: 'kinder', name: 'Familie, Kind & Baby', children: [
      { id: 'kkleidung', name: 'Kinderkleidung', children: [
        { id: 'kk1', name: 'Mädchen' }, { id: 'kk2', name: 'Jungen' }, { id: 'kk3', name: 'Baby' },
      ]},
      { id: 'spielzeug', name: 'Spielzeug', children: [
        { id: 'kk4', name: 'Lego & Bausteine' }, { id: 'kk5', name: 'Puppen & Figuren' },
        { id: 'kk6', name: 'Brettspiele' }, { id: 'kk7', name: 'Outdoor-Spielzeug' },
      ]},
    ]},
    { id: 'freizeit', name: 'Freizeit & Sport', children: [
      { id: 'sport', name: 'Sport', children: [
        { id: 'fk1', name: 'Fitnessgeräte' }, { id: 'fk2', name: 'Fahrräder' },
        { id: 'fk3', name: 'Wintersport' }, { id: 'fk4', name: 'Ballsport' },
      ]},
      { id: 'buecher', name: 'Bücher & Medien', children: [
        { id: 'fk5', name: 'Bücher' }, { id: 'fk6', name: 'CDs & Vinyl' }, { id: 'fk7', name: 'DVDs & Blu-ray' },
      ]},
    ]},
    { id: 'auto', name: 'Auto, Rad & Boot', children: [
      { id: 'azubehoer', name: 'Zubehör & Teile', children: [
        { id: 'ak1', name: 'Reifen & Felgen' }, { id: 'ak2', name: 'Ersatzteile' },
        { id: 'ak3', name: 'Tuning & Styling' },
      ]},
    ]},
  ],

  'ebay-de': [
    { id: 'fashion', name: 'Kleidung & Accessoires', children: [
      { id: 'ed-damen', name: 'Damenbekleidung', children: [
        { id: 'ed1', name: 'Tops & Shirts' }, { id: 'ed2', name: 'Kleider' },
        { id: 'ed3', name: 'Hosen' }, { id: 'ed4', name: 'Jacken & Mäntel' },
        { id: 'ed5', name: 'Pullover' }, { id: 'ed6', name: 'Röcke' },
      ]},
      { id: 'ed-herren', name: 'Herrenbekleidung', children: [
        { id: 'ed7', name: 'T-Shirts' }, { id: 'ed8', name: 'Hemden' },
        { id: 'ed9', name: 'Hosen' }, { id: 'ed10', name: 'Jacken & Mäntel' },
        { id: 'ed11', name: 'Pullover & Hoodies' },
      ]},
      { id: 'ed-schuhe', name: 'Schuhe', children: [
        { id: 'ed12', name: 'Sneaker' }, { id: 'ed13', name: 'Stiefel' },
        { id: 'ed14', name: 'Sandalen' }, { id: 'ed15', name: 'Businessschuhe' },
      ]},
      { id: 'ed-acc', name: 'Accessoires', children: [
        { id: 'ed16', name: 'Uhren' }, { id: 'ed17', name: 'Schmuck' },
        { id: 'ed18', name: 'Taschen' }, { id: 'ed19', name: 'Sonnenbrillen' },
      ]},
    ]},
    { id: 'computer', name: 'Computer, Tablets & Netzwerk', children: [
      { id: 'ed-pc', name: 'Desktops & All-in-Ones', children: [
        { id: 'ed20', name: 'PC-Systeme' }, { id: 'ed21', name: 'Komponenten' },
      ]},
      { id: 'ed-nb', name: 'Notebooks & Tablets', children: [
        { id: 'ed22', name: 'Notebooks' }, { id: 'ed23', name: 'Tablets' },
      ]},
      { id: 'ed-nw', name: 'Netzwerk', children: [
        { id: 'ed24', name: 'Router & Modems' }, { id: 'ed25', name: 'NAS-Server' },
        { id: 'ed26', name: 'Switches & Hubs' },
      ]},
      { id: 'ed-zubehoer', name: 'Zubehör', children: [
        { id: 'ed27', name: 'Tastaturen & Mäuse' }, { id: 'ed28', name: 'Monitore' },
        { id: 'ed29', name: 'Speicher' }, { id: 'ed30', name: 'Kabel & Adapter' },
      ]},
    ]},
    { id: 'handy', name: 'Handys & Kommunikation', children: [
      { id: 'ed-phones', name: 'Smartphones', children: [
        { id: 'ed31', name: 'Apple iPhone' }, { id: 'ed32', name: 'Samsung Galaxy' },
        { id: 'ed33', name: 'Sonstige' },
      ]},
      { id: 'ed-phzub', name: 'Zubehör', children: [
        { id: 'ed34', name: 'Hüllen & Cover' }, { id: 'ed35', name: 'Ladegeräte' },
        { id: 'ed36', name: 'Displayschutz' },
      ]},
    ]},
    { id: 'haus', name: 'Möbel & Wohnen', children: [
      { id: 'ed-moebel', name: 'Möbel', children: [
        { id: 'ed40', name: 'Tische' }, { id: 'ed41', name: 'Stühle' },
        { id: 'ed42', name: 'Regale' }, { id: 'ed43', name: 'Betten' },
      ]},
      { id: 'ed-deko', name: 'Dekoration', children: [
        { id: 'ed44', name: 'Bilder' }, { id: 'ed45', name: 'Kerzen' }, { id: 'ed46', name: 'Vasen' },
      ]},
    ]},
    { id: 'sport', name: 'Sporting Goods', children: [
      { id: 'ed-fitness', name: 'Fitness', children: [
        { id: 'ed50', name: 'Fitnessgeräte' }, { id: 'ed51', name: 'Laufbänder' },
      ]},
      { id: 'ed-outdoor', name: 'Camping & Outdoor', children: [
        { id: 'ed52', name: 'Zelte' }, { id: 'ed53', name: 'Rucksäcke' },
      ]},
    ]},
  ],

  etsy: [
    { id: 'jewel', name: 'Schmuck', children: [
      { id: 'et-rings', name: 'Ringe' }, { id: 'et-necklaces', name: 'Ketten' },
      { id: 'et-earrings', name: 'Ohrringe' }, { id: 'et-bracelets', name: 'Armbänder' },
    ]},
    { id: 'clothing', name: 'Kleidung', children: [
      { id: 'et-dresses', name: 'Kleider' }, { id: 'et-tops', name: 'Tops & Shirts' },
      { id: 'et-outerwear', name: 'Jacken & Mäntel' }, { id: 'et-pants', name: 'Hosen' },
      { id: 'et-shoes', name: 'Schuhe' }, { id: 'et-accessories', name: 'Accessoires' },
    ]},
    { id: 'home', name: 'Haus & Wohnen', children: [
      { id: 'et-deco', name: 'Wanddeko & Kunst' }, { id: 'et-furniture', name: 'Möbel' },
      { id: 'et-kitchen', name: 'Küche & Essen' }, { id: 'et-textiles', name: 'Textilien' },
      { id: 'et-candles', name: 'Kerzen & Duft' },
    ]},
    { id: 'vintage', name: 'Vintage', children: [
      { id: 'et-vclothing', name: 'Vintage Kleidung' }, { id: 'et-vjewelry', name: 'Vintage Schmuck' },
      { id: 'et-vhome', name: 'Vintage Haus & Wohnen' }, { id: 'et-vcollect', name: 'Sammlerstücke' },
    ]},
    { id: 'craft', name: 'Bastelmaterial', children: [
      { id: 'et-fabric', name: 'Stoffe' }, { id: 'et-beads', name: 'Perlen & Zubehör' },
      { id: 'et-tools', name: 'Werkzeug' }, { id: 'et-patterns', name: 'Schnittmuster' },
    ]},
    { id: 'art', name: 'Kunst', children: [
      { id: 'et-prints', name: 'Drucke & Poster' }, { id: 'et-painting', name: 'Gemälde' },
      { id: 'et-photo', name: 'Fotografie' }, { id: 'et-sculpture', name: 'Skulpturen' },
    ]},
    { id: 'gifts', name: 'Geschenke', children: [
      { id: 'et-personal', name: 'Personalisiert' }, { id: 'et-party', name: 'Party & Feier' },
    ]},
    { id: 'bags', name: 'Taschen & Geldbörsen', children: [
      { id: 'et-handbags', name: 'Handtaschen' }, { id: 'et-backpacks', name: 'Rucksäcke' },
      { id: 'et-wallets', name: 'Geldbörsen' },
    ]},
    { id: 'electronics', name: 'Elektronik & Zubehör', children: [
      { id: 'et-phonecases', name: 'Handyhüllen' }, { id: 'et-laptopacc', name: 'Laptop-Zubehör' },
      { id: 'et-gadgets', name: 'Gadgets' },
    ]},
  ],
};

function flattenTree(tree, path) {
  const result = [];
  for (const node of tree) {
    const currentPath = [...(path || []), node.name];
    if (node.children) {
      for (const child of node.children) {
        const childPath = [...currentPath, child.name];
        if (child.children) {
          for (const leaf of child.children) {
            result.push({
              id: leaf.id, name: leaf.name,
              path: [...childPath, leaf.name],
              pathStr: [...childPath, leaf.name].join(' > '),
              search: [...childPath, leaf.name].join(' ').toLowerCase(),
            });
          }
        } else {
          result.push({
            id: child.id, name: child.name,
            path: childPath,
            pathStr: childPath.join(' > '),
            search: childPath.join(' ').toLowerCase(),
          });
        }
      }
    }
  }
  return result;
}

const flatCache = {};

export function getCategories(platformId) {
  return CATEGORIES[platformId] || [];
}

export function getCategoriesFlat(platformId) {
  if (!flatCache[platformId]) {
    flatCache[platformId] = flattenTree(CATEGORIES[platformId] || []);
  }
  return flatCache[platformId];
}

export function matchCategories(platformId, query) {
  if (!query) return [];
  const flat = getCategoriesFlat(platformId);
  const q = query.toLowerCase().trim();
  const words = q.split(/\s+/);
  const scored = flat.map(cat => {
    let score = 0;
    if (cat.search.includes(q)) score += 100;
    if (cat.name.toLowerCase() === q) score += 200;
    for (const w of words) {
      if (w.length < 2) continue;
      if (cat.search.includes(w)) score += 20;
      if (cat.name.toLowerCase().includes(w)) score += 30;
    }
    return { ...cat, score };
  }).filter(c => c.score > 0);
  scored.sort((a, b) => b.score - a.score);
  return scored.slice(0, 15);
}

export function getAllPlatformIds() {
  return Object.keys(CATEGORIES);
}
