package t::lib::Utils;

use strict;
use warnings;

use base 'Exporter';
use vars '@EXPORT';

@EXPORT = qw/ populate_database create_database /;

sub populate_database
{
  my $schema = shift;

#  here's some of the SQL that is going to be generated by the schema
#  INSERT INTO artist VALUES (NULL,'Michael Jackson');
#  INSERT INTO artist VALUES (NULL,'Eminem');

  my @artists = (['Michael Jackson'], ['Eminem']);
  $schema->populate('Artist', [
    [qw/name/],
    @artists,
    ]);

  my %albums = (
    'Thriller' => 'Michael Jackson',
    'Bad' => 'Michael Jackson',
    'The Marshall Mathers LP' => 'Eminem',
  );

  my @cds;
  foreach my $lp (keys %albums) {
    my $artist = $schema->resultset('Artist')->find({
        name => $albums{$lp}
      });
    push @cds, [$lp, $artist->id];
  }

  $schema->populate('Cd', [
    [qw/title artistid/],
    @cds,
    ]);

  my %featuring = (
    'Thriller' => 'Eminem',
  );

  my @cds_artists;
  foreach my $lp (keys %albums) {
    my $artist = $schema->resultset('Artist')->find({ name => $albums{$lp} });
    my $cd = $schema->resultset('Cd')->find({ title => $lp });
    push @cds_artists, [$cd->id, $artist->id];
  }

  foreach my $lp (keys %featuring) {
    my $artist = $schema->resultset('Artist')->find({ name => $featuring{$lp} });
    my $cd = $schema->resultset('Cd')->find({ title => $lp });
    push @cds_artists, [$cd->id, $artist->id];
  }


  $schema->populate('M2MCdsM2MArtist', [
    [qw/cdid artistid/],
    @cds_artists,
    ]);



  my %tracks = (
    'Beat It'         => 'Thriller',
    'Billie Jean'     => 'Thriller',
    'Dirty Diana'     => 'Bad',
    'Smooth Criminal' => 'Bad',
    'Leave Me Alone'  => 'Bad',
    'Stan'            => 'The Marshall Mathers LP',
    'The Way I Am'    => 'The Marshall Mathers LP',
  );

  my @tracks;
  foreach my $track (keys %tracks) {
    my $cdname = $schema->resultset('Cd')->find({
        title => $tracks{$track},
      });
    push @tracks, [$cdname->id, $track];
  }

  $schema->populate('Track',[
    [qw/cdid title/],
    @tracks,
    ]);
}

1;
