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

const prisma = new PrismaClient();

async function configureLuebeckLocation() {
  console.log('🔧 Konfiguriere Lübeck Location & Kilometerpauschalen...\n');

  try {
    const location = await prisma.location.findUnique({
      where: { slug: 'luebeck' },
    });

    if (!location) {
      console.error('❌ Lübeck Location nicht gefunden!');
      process.exit(1);
    }

    await prisma.location.update({
      where: { id: location.id },
      data: {
        warehouseAddress: 'Wahmstraße 83',
        warehouseZip: '23552',
        warehouseCity: 'Lübeck',
      },
    });

    console.log('✅ Lager-Adresse gesetzt: Wahmstraße 83, 23552 Lübeck\n');

    const models = ['VINTAGE_SMILE', 'VINTAGE_PHOTOS', 'NOSTALGIE', 'MAGIC_MIRROR'];

    for (const model of models) {
      const existing = await prisma.priceConfig.findUnique({
        where: {
          locationId_model: {
            locationId: location.id,
            model: model as any,
          },
        },
      });

      if (existing) {
        await prisma.priceConfig.update({
          where: {
            locationId_model: {
              locationId: location.id,
              model: model as any,
            },
          },
          data: {
            kmFlatRate: 60.0,
            kmFlatRateUpTo: 15,
            pricePerKm: 0.40,
            kmMultiplier: 4,
          },
        });

        console.log(`✅ ${model}: Kilometerpauschale aktualisiert`);
      } else {
        console.log(`⚠️  ${model}: Keine PriceConfig gefunden, überspringe...`);
      }
    }

    console.log('\n🎉 Lübeck Location erfolgreich konfiguriert!');
    console.log('\nKonfiguration:');
    console.log('  📍 Lager: Wahmstraße 83, 23552 Lübeck');
    console.log('  💰 Pauschale bis 15km: 60,00€ brutto');
    console.log('  💰 Darüber: 0,40€ netto/km × 4 Strecken');

  } catch (error) {
    console.error('❌ Fehler:', error);
    throw error;
  } finally {
    await prisma.$disconnect();
  }
}

configureLuebeckLocation();
