const { PrismaClient, DealStatus, SaleType, AffiliateType } = require('@prisma/client')
const bcrypt = require("bcryptjs");

const prisma = new PrismaClient()

async function main() {
  const password = 'test'
  const hashedPassword = await bcrypt.hash(password, 10)

  // ---------- USERS ----------
  const admin = await prisma.user.upsert({
    where: { email: 'test' },
    update: {},
    create: {
      username: 'test',
      email: 'test',
      passwordHash: hashedPassword,
    },
  })

  const user = await prisma.user.upsert({
    where: { email: 'test2' },
    update: {},
    create: {
      username: 'test2',
      email: 'test2',
      passwordHash: hashedPassword,
    },
  })

  // ---------- Seller ----------
  const amazon = await prisma.seller.upsert({
    where: { name: 'Amazon' },
    update: {},
    create: {
      name: 'Amazon',
      isActive: true,
      createdById: admin.id,
    },
  })

  // ---------- Seller DOMAINS ----------
  const domains = ['amazon.com', 'amazon.com.tr']

  for (const domain of domains) {
    await prisma.SellerDomain.upsert({
      where: { domain },
      update: {},
      create: {
        domain,
        sellerId: amazon.id,
        createdById: admin.id,
      },
    })
  }

  // ---------- DEAL ----------
  const deal = await prisma.deal.create({
    data: {
      title: 'Samsung SSD 1TB',
      description: 'Test deal açıklaması',
      url: 'https://www.amazon.com.tr/dp/test',
      price: 1299.99,
      status: DealStatus.ACTIVE,
      saletype: SaleType.ONLINE,
      affiliateType: AffiliateType.NON_AFFILIATE,
      commentCount:1,
      userId: user.id,
      sellerId: amazon.id,
    },
  })

  // ---------- DEAL IMAGES ----------
  await prisma.dealImage.createMany({
    data: [
      {
        dealId: deal.id,
        imageUrl: 'https://placehold.co/600x400',
        order: 0,
      },
      {
        dealId: deal.id,
        imageUrl: 'https://placehold.co/600x401',
        order: 1,
      },
    ],
  })

  // ---------- VOTE ----------
  await prisma.dealVote.upsert({
    where: {
      dealId_userId: {
        dealId: deal.id,
        userId: admin.id,
      },
    },
    update: {},
    create: {
      dealId: deal.id,
      userId: admin.id,
      voteType: 1,
    },
  })

  // ---------- COMMENT ----------
  await prisma.comment.create({
    data: {
      text: 'Gerçekten iyi fırsat',
      userId: admin.id,
      dealId: deal.id,
    },
  })

  console.log('✅ Seed tamamlandı')
}

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