import { AccountStatus, PrismaClient, UserRole } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  const phone = process.env.SUPER_ADMIN_PHONE;
  const name = process.env.SUPER_ADMIN_NAME ?? 'Platform Super Admin';

  if (!phone) {
    throw new Error('SUPER_ADMIN_PHONE is required to create the production super admin account.');
  }

  const user = await prisma.user.upsert({
    where: { phone },
    update: {
      name,
      role: UserRole.SUPER_ADMIN,
      status: AccountStatus.ACTIVE,
      estateId: null
    },
    create: {
      estateId: null,
      phone,
      name,
      role: UserRole.SUPER_ADMIN,
      status: AccountStatus.ACTIVE
    }
  });

  console.log(`Super admin ready: ${user.phone} (${user.id})`);
}

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

