import { faker } from "@faker-js/faker";
import { openConnection } from "@/lib/db";
import { posts } from "@/db/schema/posts";
import { categories } from "@/db/schema/categories";
import { users } from "@/db/schema/users";

type Post = typeof posts.$inferInsert;
type Category = typeof categories.$inferInsert;
type User = typeof users.$inferInsert;

async function main() {
  const { db, closeConnection } = await openConnection();
  const postList: Post[] = [];
  const categoryList: Category[] = [];
  const userList: User[] = [];

  for (let i = 0; i < 100; i++) {
    postList.push({
      title: faker.lorem.sentence(),
      publishedAt: faker.date.past(),
      content: faker.lorem.paragraphs(1),
    });
  }

  for (let i = 0; i < 100; i++) {
    categoryList.push({
      name: faker.commerce.department(),
    });
  }

  userList.push({
    name: "test",
    email: "test@example.com",
    image: faker.image.avatar(),
    role: "admin",
    password: "$2b$10$3hyMqhFf3VAZ.8m9HTeGH.6WdS.lFYo9/ELOHh8oMhbbhV49LEMna",
  });

  for (let i = 0; i < 10; i++) {
    const firstName = faker.person.firstName();
    const lastName = faker.person.lastName();
    userList.push({
      name: faker.person.fullName({ firstName, lastName }),
      email: faker.internet.email({
        firstName,
        lastName,
        provider: "example.com",
      }),
      image: faker.image.avatar(),
      role: faker.helpers.arrayElement(["user", "admin"]),
    });
  }

  await db.delete(posts);
  await db.delete(categories);
  await db.delete(users);

  await db.insert(posts).values(postList);
  await db.insert(categories).values(categoryList);
  await db.insert(users).values(userList);

  await closeConnection();
}

main();
