import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
  console.log("🔍 بررسی کاربران...\n");

  const users = await prisma.user.findMany({
    select: {
      id: true,
      email: true,
      name: true,
      role: true,
      createdAt: true,
      team: {
        select: {
          id: true,
          name: true,
        },
      },
    },
  });

  console.log(`تعداد کاربران: ${users.length}\n`);

  users.forEach((user, index) => {
    console.log(`${index + 1}. ${user.email}`);
    console.log(`   ID: ${user.id}`);
    console.log(`   نام: ${user.name || "ندارد"}`);
    console.log(`   نقش: ${user.role}`);
    console.log(`   تیم: ${user.team ? user.team.name : "ندارد"}`);
    console.log(`   تاریخ ثبت‌نام: ${user.createdAt.toISOString()}`);
    console.log("");
  });

  // بررسی Session ها
  const sessions = await prisma.session.findMany({
    select: {
      id: true,
      userId: true,
      expires: true,
      user: {
        select: {
          email: true,
        },
      },
    },
  });

  console.log(`\n📋 تعداد Session های فعال: ${sessions.length}\n`);

  sessions.forEach((session, index) => {
    const isExpired = session.expires < new Date();
    console.log(`${index + 1}. User: ${session.user.email}`);
    console.log(`   Session ID: ${session.id}`);
    console.log(`   User ID: ${session.userId}`);
    console.log(`   وضعیت: ${isExpired ? "منقضی شده ❌" : "فعال ✓"}`);
    console.log(`   انقضا: ${session.expires.toISOString()}`);
    console.log("");
  });
}

main()
  .catch((e) => {
    console.error("❌ خطا:", e);
    process.exit(1);
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
