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

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 Seeding missing test users...');

  // Get tenant and existing roles
  const tenant = await prisma.tenant.findUnique({ where: { id: '00000000-0000-0000-0000-000000000001' } });
  if (!tenant) throw new Error('Tenant not found');

  const roles = await prisma.role.findMany();
  const getRoleId = (code: string) => roles.find(r => r.code === code)?.id;

  const defaultPass = await bcrypt.hash('Test@12345', 10);

  // 1. Copropriétaire (Owner)
  const copro = await prisma.user.upsert({
    where: { email: 'copro@atlas.ma' },
    update: {},
    create: {
      email: 'copro@atlas.ma',
      passwordHash: defaultPass,
      firstName: 'Ahmed',
      lastName: 'Propriétaire',
      language: 'fr',
      roles: { create: { roleId: getRoleId('COPROPRIETAIRE')!, tenantId: tenant.id } },
    },
  });

  // 2. Locataire (Tenant)
  const locataire = await prisma.user.upsert({
    where: { email: 'locataire@atlas.ma' },
    update: {},
    create: {
      email: 'locataire@atlas.ma',
      passwordHash: defaultPass,
      firstName: 'Sara',
      lastName: 'Locataire',
      language: 'fr',
      roles: { create: { roleId: getRoleId('LOCATAIRE')!, tenantId: tenant.id } },
    },
  });

  // 3. Gardien (Guard)
  const gardien = await prisma.user.upsert({
    where: { email: 'gardien@atlas.ma' },
    update: {},
    create: {
      email: 'gardien@atlas.ma',
      passwordHash: defaultPass,
      firstName: 'Hassan',
      lastName: 'Gardien',
      language: 'fr',
      roles: { create: { roleId: getRoleId('GARDIEN')!, tenantId: tenant.id } },
    },
  });

  // 4. Prestataire (Supplier)
  const prestataire = await prisma.user.upsert({
    where: { email: 'prestataire@atlas.ma' },
    update: {},
    create: {
      email: 'prestataire@atlas.ma',
      passwordHash: defaultPass,
      firstName: 'Youssef',
      lastName: 'Plombier',
      language: 'fr',
      roles: { create: { roleId: getRoleId('PRESTATAIRE')!, tenantId: tenant.id } },
    },
  });

  // Assign to Lot A-101 and A-102
  const lotA101 = await prisma.lot.findFirst({ where: { lotNumber: 'A-101' } });
  const lotA102 = await prisma.lot.findFirst({ where: { lotNumber: 'A-102' } });

  if (lotA101) {
    await prisma.resident.upsert({
      where: { userId_lotId_role: { userId: copro.id, lotId: lotA101.id, role: 'PROPRIETAIRE' } },
      update: {},
      create: { userId: copro.id, lotId: lotA101.id, role: 'PROPRIETAIRE' }
    });
  }

  if (lotA102) {
    await prisma.resident.upsert({
      where: { userId_lotId_role: { userId: locataire.id, lotId: lotA102.id, role: 'LOCATAIRE' } },
      update: {},
      create: { userId: locataire.id, lotId: lotA102.id, role: 'LOCATAIRE' }
    });
  }

  console.log('✅ Added missing test users: copro@, locataire@, gardien@, prestataire@ / Test@12345');
}

main().catch(console.error).finally(() => prisma.$disconnect());
