import postgres from "postgres";
import { env } from "@/env";
import { assertLocalDatabaseUrl } from "./guard";

async function main(): Promise<void> {
  assertLocalDatabaseUrl(env.DATABASE_URL);
  const sql = postgres(env.DATABASE_URL, { max: 1 });
  try {
    // `drizzle` porte le journal des migrations (`drizzle.__drizzle_migrations`) : sans ce
    // drop, `db:migrate` croirait toutes les migrations déjà appliquées.
    await sql.unsafe("DROP SCHEMA IF EXISTS drizzle CASCADE");
    await sql.unsafe("DROP SCHEMA IF EXISTS public CASCADE");
    await sql.unsafe("CREATE SCHEMA public");
  } finally {
    await sql.end();
  }
  console.log("Base auth vidée (schémas public et drizzle recréés).");
}

main()
  .then(() => process.exit(0))
  .catch((error: unknown) => {
    console.error(error instanceof Error ? error.message : error);
    process.exit(1);
  });
