import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  console.log('🔄 Setze korrekte LexOffice-Artikel-IDs pro Modell...\n');

  // Mapping: Modell -> Artikel-IDs
  const modelArticleIds: Record<string, { withoutFlat: string; withFlat: string }> = {
    'VINTAGE_SMILE': {
      withoutFlat: '5d9d3716-f81e-4e46-b5cf-13988f489cc2',
      withFlat: '3f26c02c-d705-41a6-9b49-2e2e96e77036',
    },
    'VINTAGE_PHOTOS': {
      withoutFlat: '5d9d3716-f81e-4e46-b5cf-13988f489cc2',
      withFlat: '3f26c02c-d705-41a6-9b49-2e2e96e77036',
    },
    'NOSTALGIE': {
      withoutFlat: '8954bd20-570c-4a7d-9ac3-8ee756652a89',
      withFlat: '701bd150-48c0-4937-b628-f4a754d86264',
    },
    'MAGIC_MIRROR': {
      withoutFlat: '72bbe51b-c0bb-437a-963b-248cb105553a',
      withFlat: '39ec59e7-57a6-4d6d-80f6-645e589c4b2c',
    },
  };

  // Update pro Modell
  for (const [model, ids] of Object.entries(modelArticleIds)) {
    const result = await prisma.priceConfig.updateMany({
      where: { model },
      data: {
        lexofficeArticleId: ids.withoutFlat,
        lexofficeArticleIdWithFlat: ids.withFlat,
      },
    });
    console.log(`✅ ${model}: ${result.count} PriceConfigs aktualisiert`);
  }

  // Kilometerpauschale für Lübeck
  const luebeck = await prisma.location.findFirst({ where: { city: 'Lübeck' } });
  if (luebeck) {
    const kmResult = await prisma.priceConfig.updateMany({
      where: { locationId: luebeck.id },
      data: { lexofficeKmFlatArticleId: 'd3e2b21b-e899-412d-b53e-c82a0a94fcfa' },
    });
    console.log(`✅ Lübeck Kilometerpauschale: ${kmResult.count} PriceConfigs aktualisiert`);
  }

  console.log('\n📦 Equipment-Artikel-IDs aktualisieren...\n');

  // Equipment-Mapping: Name -> Artikel-ID
  const equipmentArticleIds: Record<string, string> = {
    'Accessoires': 'c62d4dad-4f04-4330-9019-f9804bb43ddc',
    'VIP-Bänder': 'e3942394-94d7-45ea-a31b-a1b035f6f34e',
    'Erweiterte Druck-Flat DIY': '774aba7f-2c20-4d65-b8d0-4793f27d2d71',
    'Kartenspiel Fotoboxaufgaben': '17d563fe-5f00-4591-a414-d013d7ce68e0',
    'Roter Teppich': 'd138da65-4e23-4a88-813a-1f3725a75a15',
    'Service-Techniker vor Ort': 'ec972616-8bc5-4334-876d-b442838a8bbf',
  };

  for (const [name, articleId] of Object.entries(equipmentArticleIds)) {
    try {
      const equipment = await prisma.equipment.findFirst({
        where: { name: { contains: name, mode: 'insensitive' } },
      });

      if (equipment) {
        await prisma.equipment.update({
          where: { id: equipment.id },
          data: { lexofficeArticleId: articleId },
        });
        console.log(`✅ ${name}: Artikel-ID gesetzt`);
      } else {
        console.log(`⚠️  ${name}: Equipment nicht gefunden - übersprungen`);
      }
    } catch (error) {
      console.log(`⚠️  ${name}: Fehler - ${error}`);
    }
  }

  console.log('\n📊 Finale Übersicht:');
  const configs = await prisma.priceConfig.findMany({
    include: { location: true },
    orderBy: [{ location: { city: 'asc' } }, { model: 'asc' }],
  });

  console.table(configs.map(c => ({
    city: c.location?.city,
    model: c.model,
    withoutFlat: c.lexofficeArticleId?.slice(0, 8) + '...',
    withFlat: c.lexofficeArticleIdWithFlat?.slice(0, 8) + '...',
    kmFlat: c.lexofficeKmFlatArticleId?.slice(0, 8) + '...' || 'null',
  })));

  const equipment = await prisma.equipment.findMany({
    select: { name: true, lexofficeArticleId: true },
  });

  console.log('\n📦 Equipment mit Artikel-IDs:');
  console.table(equipment.map(e => ({
    name: e.name,
    articleId: e.lexofficeArticleId?.slice(0, 8) + '...' || 'null',
  })));
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
