import { Client } from "pg";
import { auth } from "../../../pkg/auth/auth.js";
import { createLogger } from "../../../pkg/logger/logger.js";

export const userMigrate = async () => {
	const log = createLogger({ module: "admin", subModule: "migrate users" });
	const client = new Client({
		connectionString: process.env.DATABASE_URL_V1,
	});

	await client.connect();

	const res = await client.query(`
      SELECT username, email
      FROM users     
    `);

	for (const u of res.rows) {
		log.info({}, `${u.username}, being transferred`);
		try {
			await auth.api.signUpEmail({
				body: {
					username: u.username,
					name: u.username,
					email: u.email,
					password: "Alpla2025!",
				},
			});
			log.info({}, `${u.username}, transferred`);
		} catch (e) {
			log.info({}, `${u.username}, already existed`);
		}
	}
};
