#!/usr/bin/env perl
# PODNAME: qzoom
# ABSTRACT: A script to print metadata from Qiime2 artifacts
use 5.014;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Qiime2::Artifact;
use File::Basename;
use File::Path qw(make_path);
use Term::ANSIColor qw(:constants);
use File::Spec;
use Data::Dumper;
my $VERSION = '0.12.0';

# Command line options
my $opt = {
    'info'     => 0,
    'cite'     => 0,
    'extract'  => 0,
    'data'     => 0,
    'outdir'   => './',
    'bibtex'   => undef,
    'rename'   => 0,
    'verbose'  => 0,
    'help'     => 0,
    'version'  => 0,
    'tabular'  => 0,
};

GetOptions(
    'i|info'     => \$opt->{info},
    'c|cite'     => \$opt->{cite},
    'x|extract'  => \$opt->{extract},
    'd|data'     => \$opt->{data},
    'o|outdir=s' => \$opt->{outdir},
    'b|bibtex=s' => \$opt->{bibtex},
    'r|rename'   => \$opt->{rename},
    't|tabular'  => \$opt->{tabular},
    'verbose'    => \$opt->{verbose},
    'help'       => \$opt->{help},
    'version'    => \$opt->{version},
) or pod2usage(-verbose => 1, -exitval => 1);

pod2usage(-verbose => 2, -exitval => 0) if $opt->{help};
if ($opt->{version}) {
    say "qzoom.pl v$VERSION";
    exit 0;
}

# If no specific action is requested, default to info
$opt->{info} = 1 if (!$opt->{cite} && !$opt->{extract} && !$opt->{data});

# Need at least one file
pod2usage(-verbose => 1, -exitval => 1) if scalar(@ARGV) == 0;

# Print table header if tabular mode is enabled
if ($opt->{tabular}) {
    printf "%-30s %-36s %-8s %-15s %s\n",
        "FILENAME", "UUID", "TYPE", "VERSION", "FILES";
    printf "%s\n", "-" x 100;
}

# Process each artifact
for my $file (@ARGV) {
    process_artifact($file, $opt);
}

sub process_artifact {
    my ($file, $opt) = @_;
    my $artifact;
    
    # Try to load artifact
    eval {
        $artifact = Qiime2::Artifact->new({
            filename => $file,
            verbose  => $opt->{verbose}
        });
    };
    if ($@) {
        warn "Error processing $file: $@\n";
        return;
    }

    my $basename = basename($file);
    my $id = $artifact->get('id');
    my $is_viz = $artifact->get('visualization');
    my @data_files = @{$artifact->get('data')};
    
    # Show basic info
    if ($opt->{info}) {
        if ($opt->{tabular}) {
            printf "%-30s %-36s %-8s %-15s %d\n",
                substr($basename, 0, 30),
                $id,
                $is_viz ? "VIZ" : "DATA",
                $artifact->get('version'),
                scalar(@data_files);
        } else {
            printf "%s%s%s [%s]\n", 
                BOLD, $basename, RESET,
                $is_viz ? 'HTML Visualization' : 'Data Artifact';
            printf "  UUID: %s\n", $id;
            printf "  QIIME2 version: %s\n", $artifact->get('version');
            printf "  Files: %s\n", scalar(@data_files);
            print "\n" if $opt->{verbose};
        }
    }
    
    # Show data files
    if ($opt->{data}) {
        say "Files in $basename:";
        for my $data_file (@data_files) {
            say "  $data_file";
        }
        print "\n" if $opt->{verbose};
    }
    
    # Extract data
    if ($opt->{extract}) {
        my $target_dir = $opt->{outdir};
        make_path($target_dir) unless -d $target_dir;
        
        for my $data_file (@data_files) {
            my $output_file = $data_file;
            
            # Handle rename option
            if ($opt->{rename} && scalar(@data_files) == 1) {
                my ($name, $path, $suffix) = fileparse($data_file, qr/\.[^.]*/);
                my ($artifact_name, $artifact_path, $artifact_suffix) = 
                    fileparse($basename, qr/\.qz[av]/);
                $output_file = $artifact_name . $suffix;
            }
            
            my $target_path = File::Spec->catfile($target_dir, $output_file);
            
            if ($opt->{verbose}) {
                say "Extracting: $data_file -> $target_path";
            }
            
            # Here you would add the actual extraction code
            # This would require additional methods in the Qiime2::Artifact module
            # to extract files from the zip archive
        }
    }
    
    # Handle citations (placeholder - would need citation data from artifact)
    if ($opt->{cite}) {
        if ($opt->{bibtex}) {
            # Append citations to bibtex file
            # Would need to implement citation extraction from artifact
            my $citation = $artifact->get_bib();
            open my $fh, '>>', $opt->{bibtex} or die "Cannot open $opt->{bibtex}: $!";
            print $fh $citation;
            close $fh;
        } else {
            say $artifact->get_bib();
        }
    }
}

__END__

=pod

=encoding UTF-8

=head1 NAME

qzoom - A script to print metadata from Qiime2 artifacts

=head1 VERSION

version 0.13.0

=head1 SYNOPSIS

qzoom.pl [options] <artifact1.qza/v> [<artifact2.qza ...]

=head1 OPTIONS

=head2 Main Actions

=over 4

=item B<-i, --info>

Print artifact information to STDOUT. Enabled by default if no other action specified.

=item B<-c, --cite>

Print artifact citation to STDOUT or to file.

=item B<-x, --extract>

Extract the content of the 'data' directory.

=item B<-d, --data>

List all the files contained in the ./data directory of the artifacts.

=back

=head2 Other Parameters

=over 4

=item B<-o, --outdir> I<OUTDIR>

Directory where to extract files (default: ./).

=item B<-b, --bibtex> I<FILE>

Save citations to a file (append).

=item B<-r, --rename>

Rename extracted files to {artifactbasename}.{ext}.

=item B<-t, --tabular>

Print output in tabular format.

=item B<--verbose>

Print verbose output.

=back

=head2 EXAMPLES

=over 4

=item Get attributes from single artifact:

  qzoom  data/reads.qzv

Will produce:

  reads.qzv [HTML Visualization]
    UUID: 44df755f-c76f-46b8-9011-ba50f6716d4c
    QIIME2 version: 2019.10.0
    Files: 26

=item List multiple artifacts:

    qzoom data/reads.qz*

Will simply list one artifact after the other.

    reads.qza [Data Artifact]
      UUID: cf3d88d9-51e8-411a-a52b-10646b2940c1
      QIIME2 version: 2019.10.0
      Files: 10
    reads.qzv [HTML Visualization]
      UUID: 44df755f-c76f-46b8-9011-ba50f6716d4c
      QIIME2 version: 2019.10.0
      Files: 26

=item List in tabular format:

    qzoom -t data/*.qz{a,v}

Will produce:

  FILENAME                       UUID                                 TYPE     VERSION         FILES
  ----------------------------------------------------------------------------------------------------
  reads.qza                      cf3d88d9-51e8-411a-a52b-10646b2940c1 DATA     2019.10.0       10
  reads.qzv                      44df755f-c76f-46b8-9011-ba50f6716d4c VIZ      2019.10.0       26
  table.qza                      d27b6a68-5c6e-46d9-9866-7b4d46cca533 DATA     2018.6.0        1
  tree-derived.qza               54e4cde6-29d4-4da9-a6f1-9324b7780819 DATA     2019.10.0       1
  tree-imported.qza              c2d390bf-c37f-412e-9d17-dd8f5a7ef2cf DATA     2019.10.0       1

=back

=head1 AUTHOR

Andrea Telatin <andrea@telatin.com>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2020 by Andrea Telatin.

This is free software, licensed under:

  The MIT (X11) License

=cut
