import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

const shop = process.env.DEMO_SHOP || 'partner-nook-dev.myshopify.com';

// Merchant (one store, one merchant)
let m = await prisma.merchant.findUnique({ where: { shop } });
if (!m) m = await prisma.merchant.create({ data: { shop, programName: 'Honasa', minPayout: 500 } });
else m = await prisma.merchant.update({ where: { id: m.id }, data: { programName: 'Honasa' } });

// Two campaigns/brands in the SAME store
await prisma.campaign.deleteMany({ where: { merchantId: m.id } });
const mama = await prisma.campaign.create({
  data: { merchantId: m.id, name: 'Mamaearth', matchType: 'VENDOR', matchValue: 'Mamaearth', commissionType: 'PERCENT', commissionValue: 10 },
});
const derma = await prisma.campaign.create({
  data: { merchantId: m.id, name: 'The Derma Co', matchType: 'VENDOR', matchValue: 'The Derma Co', commissionType: 'PERCENT', commissionValue: 15 },
});

// One affiliate with a coupon
let sara = await prisma.affiliate.findFirst({ where: { merchantId: m.id, email: 'sara@example.com' } });
if (!sara)
  sara = await prisma.affiliate.create({
    data: { merchantId: m.id, name: 'Sara', email: 'sara@example.com', status: 'ACTIVE', couponCode: 'SARA10', balance: 0 },
  });
else sara = await prisma.affiliate.update({ where: { id: sara.id }, data: { status: 'ACTIVE', couponCode: 'SARA10', balance: 0 } });

console.log(JSON.stringify({
  merchant: m.programName,
  store: m.shop,
  campaigns: [`${mama.name} (10%)`, `${derma.name} (15%)`],
  affiliate: `${sara.name} / ${sara.couponCode}`,
}, null, 2));

await prisma.$disconnect();
