#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts::Maintenance;
use LATMOS::Accounts::Log;
use Getopt::Long;
use Pod::Usage;
use Text::CSV;

=head1 NAME

    la-sql-stat2csv - Output collected statistics data

=head1 SYNOPSIS

    la-expired-reminder [options] statname

=cut

GetOptions(
    'c|config=s' => \my $config,
    'b|base=s'   => \my $base,
    'help'       => sub { pod2usage(0) },
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base base

Use this specific base

=back

=cut

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase && $labase->load or die "Cannot load base";
$labase->wexported(1);

my ($name) = @ARGV;

my $stat = $labase->get_object('stat', $name) or die "no stat name $name";

my $data = $stat->getAllStat();
my $csv = Text::CSV->new({eol => $/});

foreach my $date (sort keys %$data) {
    foreach my $val (sort keys %{ $data->{$date} }) {
        $csv->print(\*STDOUT, [ $date, $val, $data->{$date}{$val} ]);
    }
}

exit 0
