import dotenv from "dotenv";
import { user } from "./schema/users";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

dotenv.config({ path: ".env.local" });

// Get email from command line arguments
const email = process.argv[2];
const name = process.argv[3];

// Check if email is provided
if (!email) {
  console.error("You must provide an email address");
  process.exit(1);
}

// Connect to the database
const pool = new Pool({
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  user: process.env.DB_USERNAME,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
});

const db = drizzle(pool);

async function createUser(email: string, name: string) {
  try {
    await db.insert(user).values({ email: email, firstName: name });
    console.log(`User with email ${email} created successfully`);
    process.exit(0);
  } catch (err) {
    console.error("Failed to create user:", err);
    process.exit(1);
  }
}

createUser(email, name);
