import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt';

const prisma = new PrismaClient();

async function main() {
  console.log('Seeding sample users...');

  const passwordHash = await bcrypt.hash('password123', 10);

  // 1. Admin
  await prisma.user.upsert({
    where: { email: 'admin@demo.com' },
    update: { passwordHash },
    create: {
      email: 'admin@demo.com',
      fullName: 'Admin Demo',
      passwordHash,
      role: 'ADMIN',
      status: 'ACTIVE',
      phone: '0811111111',
    },
  });

  // 2. Verified Donor
  const donorVerified = await prisma.user.upsert({
    where: { email: 'donatur.verified@demo.com' },
    update: { passwordHash },
    create: {
      email: 'donatur.verified@demo.com',
      fullName: 'Donatur Verified',
      passwordHash,
      role: 'DONOR',
      status: 'ACTIVE',
      phone: '0822222222',
    },
  });

  await prisma.userVerification.upsert({
    where: { userId: donorVerified.id },
    update: { status: 'APPROVED' },
    create: {
      userId: donorVerified.id,
      idCardNumber: '1111111111111111',
      idCardPhoto: 'https://via.placeholder.com/400x250?text=KTP+Donatur',
      selfiePhoto: 'https://via.placeholder.com/300x400?text=Selfie+Donatur',
      status: 'APPROVED',
      verifiedAt: new Date(),
    }
  });

  // 3. Unverified Donor
  await prisma.user.upsert({
    where: { email: 'donatur.baru@demo.com' },
    update: { passwordHash },
    create: {
      email: 'donatur.baru@demo.com',
      fullName: 'Donatur Baru',
      passwordHash,
      role: 'DONOR',
      status: 'ACTIVE',
      phone: '0833333333',
    },
  });

  // 4. Verified Campaigner
  const campaignerVerified = await prisma.user.upsert({
    where: { email: 'penggalang.verified@demo.com' },
    update: { passwordHash },
    create: {
      email: 'penggalang.verified@demo.com',
      fullName: 'Penggalang Verified',
      passwordHash,
      role: 'TENANT_OWNER',
      status: 'ACTIVE',
      phone: '0844444444',
    },
  });

  await prisma.userVerification.upsert({
    where: { userId: campaignerVerified.id },
    update: { status: 'APPROVED' },
    create: {
      userId: campaignerVerified.id,
      idCardNumber: '2222222222222222',
      idCardPhoto: 'https://via.placeholder.com/400x250?text=KTP+Penggalang',
      selfiePhoto: 'https://via.placeholder.com/300x400?text=Selfie+Penggalang',
      status: 'APPROVED',
      verifiedAt: new Date(),
    }
  });

  // 5. Unverified Campaigner
  await prisma.user.upsert({
    where: { email: 'penggalang.baru@demo.com' },
    update: { passwordHash },
    create: {
      email: 'penggalang.baru@demo.com',
      fullName: 'Penggalang Baru',
      passwordHash,
      role: 'TENANT_OWNER',
      status: 'ACTIVE',
      phone: '0855555555',
    },
  });

  console.log('Seeding finished successfully!');
}

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