import { getDatabase } from "../src/lib/database/index";

async function verifyDatabase() {
  console.log("Starting database verification...");
  const db = await getDatabase();
  await db.connect();

  try {
    // 1. Create User
    console.log("Creating test user...");
    const userId = `test-user-${Date.now()}`;
    const user = await db.createUser({
      id: userId,
      email: `test-${Date.now()}@example.com`,
      firstName: "Test",
      lastName: "User",
      password: "password123",
      role: "client",
      phone: "1234567890",
    });
    console.log("User created:", user.id);

    // 2. Create Client
    console.log("Creating test client...");
    const client = await db.createClient({
      userId: user.id,
      membershipType: "basic",
      membershipStatus: "active",
      joinDate: new Date(),
    });
    console.log("Client created:", client.id);

    // 3. Create Fitness Profile
    console.log("Creating fitness profile...");
    const profile = await db.createFitnessProfile({
      id: "test-profile-id",
      userId: user.id,
      height: 180,
      weight: 75,
      age: 30,
      gender: "male",
      activityLevel: "moderately_active",
      fitnessGoals: ["weight_loss"],
      medicalConditions: "None",
    });
    console.log("Fitness profile created for:", profile.userId);

    // 4. Attendance Check-in
    console.log("Checking in...");
    const checkIn = await db.checkIn(user.id, "gym", "Test check-in");
    console.log("Checked in:", checkIn.id);

    // 5. Verify Active Check-in
    const activeCheckIn = await db.getActiveCheckIn(user.id);
    if (!activeCheckIn || activeCheckIn.id !== checkIn.id) {
      throw new Error("Active check-in verification failed");
    }
    console.log("Active check-in verified");

    // 6. Attendance Check-out
    console.log("Checking out...");
    const checkOut = await db.checkOut(checkIn.id);
    console.log("Checked out:", checkOut?.checkOutTime);

    // 7. Cleanup
    console.log("Cleaning up...");
    await db.deleteUser(user.id);
    console.log("Cleanup complete");

    console.log("✅ Verification successful!");
  } catch (error) {
    console.error("❌ Verification failed:", error);
    process.exit(1);
  } finally {
    await db.disconnect();
  }
}

verifyDatabase();
