// /opt/erp-system/prisma/seed.ts
import { PrismaClient } from '@prisma/client';
import { PrismaPg } from '@prisma/adapter-pg';
import { Pool } from 'pg';
import bcrypt from 'bcryptjs';
import * as dotenv from 'dotenv';

// Umgebungsvariablen laden
dotenv.config();

// Prisma mit dem PostgreSQL-Adapter initialisieren
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
const adapter = new PrismaPg(pool);
const prisma = new PrismaClient({ adapter });

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

  const admin = await prisma.user.upsert({
    where: { email: 'admin@erp.local' },
    update: {},
    create: {
      email: 'admin@erp.local',
      firstName: 'System',
      lastName: 'Admin',
      passwordHash: hashedPassword,
      role: 'ADMIN',
    },
  });

  console.log('Initianler Admin erstellt:', admin.email);
}

main()
  .catch((e) => {
    console.error(e);
    process.exit(1);
  })
  .finally(async () => {
    // Verbindung sauber trennen
    await pool.end(); 
  });
