import 'dotenv/config';
import mysql from 'mysql2/promise';

const BASELINE_MIGRATIONS = [
  {
    timestamp: 1780721550917,
    name: 'InitialSchema1780721550917',
  },
];

async function main() {
  const databaseUrl = process.env.DATABASE_URL;

  if (!databaseUrl) {
    throw new Error('DATABASE_URL is not set');
  }

  const connection = await mysql.createConnection(databaseUrl);

  try {
    for (const migration of BASELINE_MIGRATIONS) {
      const [rows] = await connection.query(
        'SELECT id FROM migrations WHERE name = ? LIMIT 1',
        [migration.name],
      );

      if (Array.isArray(rows) && rows.length > 0) {
        console.log(`Already recorded: ${migration.name}`);
        continue;
      }

      await connection.query(
        'INSERT INTO migrations (timestamp, name) VALUES (?, ?)',
        [migration.timestamp, migration.name],
      );

      console.log(`Recorded baseline migration: ${migration.name}`);
    }
  } finally {
    await connection.end();
  }
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});
