import { Client as PgClient } from 'pg';

async function seed() {
  const pgClient = new PgClient({
    connectionString: process.env.DATABASE_URL,
  });

  await pgClient.connect();

  console.log('Seeding database...');

  try {
    // Clear existing data
    await pgClient.query('TRUNCATE artists, albums, tracks, genre, track_genre, dislikes, recommendation_batch, recommendation_batch_track, track_audio_features, track_lyrics CASCADE');

    // Insert an artist
    const artistRes = await pgClient.query(
      'INSERT INTO artists (name, mbid) VALUES ($1, $2) RETURNING id',
      ['Daft Punk', '5742e173-e031-4848-90a4-977799791608']
    );
    const artistId = artistRes.rows[0].id;

    // Insert an album
    const albumRes = await pgClient.query(
      'INSERT INTO albums (artist_id, title, year) VALUES ($1, $2, $3) RETURNING id',
      [artistId, 'Discovery', 2001]
    );
    const albumId = albumRes.rows[0].id;

    // Insert tracks
    await pgClient.query(
      `INSERT INTO tracks (path, hash, title, artist, album_id, duration, state, source_type)
       VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
      ['/music/daft_punk/discovery/one_more_time.mp3', 'hash1', 'One More Time', 'Daft Punk', albumId, 320, 'LIBRARY', 'MANUAL']
    );

    await pgClient.query(
      `INSERT INTO tracks (path, hash, title, artist, album_id, duration, state, source_type)
       VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
      ['/music/daft_punk/discovery/harder_better_faster_stronger.mp3', 'hash2', 'Harder, Better, Faster, Stronger', 'Daft Punk', albumId, 224, 'LIBRARY', 'MANUAL']
    );

    console.log('Seeding successful!');
  } catch (err) {
    console.error('Seeding failed:', err);
    process.exit(1);
  } finally {
    await pgClient.end();
  }
}

seed();
