#!/usr/bin/perl -X
use strict;
use warnings;
use JSON::XS;
use Data::Printer;
use AI::MicroStructure;
use AI::MicroStructure::Util;
#use AI::MicroStructure::Fitnes;
#use AI::MicroStructure::Categorizer;
#use AI::MicroStructure::Memorizer;
#use AI::MicroStructure::Tree;
#use AI::MicroStructure::Collection;
use Storable::CouchDB;

my $state = AI::MicroStructure::Util::load_config(); my @CWD=$state->{cwd}; my $config=$state->{cfg};




  sub checkIsThere {

    my $key =shift;


    require LWP::UserAgent;
    my $ua = LWP::UserAgent->new;
    my ($server,$db) = ($config->{couchdb},$config->{db});

   my $res ;

    $res = $ua->get(sprintf('%s/%s/_design/base/_view/instances?reduce=false&start_key=["%s"]&end_key=["%sz"]',
                              $server,
                              $db,
                              $key,
                              $key));


$res = decode_json($res->content);
return @{$res->{rows}};


}


   our $x = Storable::CouchDB->new(
                                      uri => $config->{couchdb},  #default
                                      db  => $config->{db}
                                     );



push @ARGV,'film';
push @ARGV,"band";
push @ARGV,"single";
push @ARGV,"album";
push @ARGV,"music";
push @ARGV,"movie";
push @ARGV,"rapstar";
push @ARGV,"artist";
push @ARGV,"actor";
push @ARGV,"movie";
push @ARGV,"rock";
push @ARGV,"pop";
push @ARGV,"song";


sub deleteByCategory  {
my $cat = shift;
my @q=();
my @set = checkIsThere($cat);
print  map{  push @q,$_->{id} unless(!$_->{id});}@set;
print join "\n",@q;
map{ $x->delete($_); }@q;
}


deleteByCategory($_) for(@ARGV);


  1;

  __END__
