import { drizzle } from 'drizzle-orm/node-postgres';
import { promises as fs } from 'fs';
import { Pool } from 'pg';
import * as schema from './schema';

// Drizzle-Tabellen-Definitionen (hier hast du bereits die Tabellen definiert, wir nehmen an, sie werden hier importiert)
import { businesses, commercials, users } from './schema'; // Anpassen je nach tatsächlicher Struktur

const connectionString = process.env.DATABASE_URL;
console.log(connectionString);
const client = new Pool({ connectionString });
const db = drizzle(client, { schema, logger: true });
(async () => {
  try {
    // Abfrage der Daten für jede Tabelle
    const usersData = await db.select().from(users).execute();
    const businessesData = await db.select().from(businesses).execute();
    const commercialsData = await db.select().from(commercials).execute();

    // Speichern der Daten in JSON-Dateien
    await fs.writeFile('./data/users_export.json', JSON.stringify(usersData, null, 2));
    console.log('Users exportiert in users.json');

    await fs.writeFile('./data/businesses_export.json', JSON.stringify(businessesData, null, 2));
    console.log('Businesses exportiert in businesses.json');

    await fs.writeFile('./data/commercials_export.json', JSON.stringify(commercialsData, null, 2));
    console.log('Commercials exportiert in commercials.json');
  } catch (error) {
    console.error('Fehler beim Exportieren der Tabellen:', error);
  } finally {
    await client.end();
  }
})();
