package SLUB::LZA::Rosetta::TA::Command::count;
use strict;
use warnings;
use feature qw(say);
use SLUB::LZA::Rosetta::TA -command;

sub abstract {"count IEs in Rosetta based Archival Information System";}

my $description=<<"DESCR";
Searches a Rosetta-based AIS for descriptive oder source metadata on behalf of the Technical Analyst
and return counts of matches.

Examples:

  * Is this dc identifier in Archiv?
    $0 count -d SLUB:LZA:Kitodo:kitodo:422766
  * How many IEs were modified in 2021-05-31?
    $0 count -m 2021-05-31
DESCR

sub description {
    "$description"
}
sub opt_spec {
    return(
        ["verbose|v" => "enable verbose output"],
        ["datemode" => hidden => {one_of => [
            ["creationdate|c=s" => "search based on creationdate string"],
            ["modificationdate|m=s" => "search based on modificationdate string"]
        ] } ],
        ["descriptive|d" => "count based on string search in descriptive metadata"],
        ["source|s" => "count based on string search in source metadata"],
        ["ie|i=s" => "search a specific IE"],
    );
}
sub validate_args {
    my ($self, $opt, $args) = @_;
    # no args allowed but options!
    $self->usage_error("No args allowed") if @$args;
}

sub execute {
    my ($self, $opt, $args) = @_;
    my $maxrecords="0";
    my $startrecord=1;
    my @queries;
    if (exists $opt->{source}) {
        push @queries, "IE.sourceMD.content=$opt->{source}";
    }
    if (exists $opt->{ie}) {
        push @queries, "IE.dc.identifier==$opt->{ie}";
    }
    if (exists $opt->{descriptive}) {
        push @queries, "IE.dc.identifier==$opt->{descriptive}";
    }
    if (exists $opt->{creationdate}) {
        push @queries, "IE.objectCharacteristics.creationDate==$opt->{creationdate}";
    }
    if (exists $opt->{modificationdate}) {
        push @queries, "FILE.objectCharacteristics.modificationDate==$opt->{modificationdate}";
    }
    my $query = join(" and ", @queries);
    my $response = SLUB::LZA::Rosetta::TA::sru_search('ie', $query, $startrecord, $maxrecords, $opt->{verbose});
    $response=~s|.*?<numberOfRecords>(\d+)</numberOfRecords.*|$1|s;
    say $response;
}

1;