package Stats::Redshift::Dimension::Aggregate;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use Common::Session;

use lib '/app/tools/stats/lib';

use base 'Class::Accessor';

__PACKAGE__->mk_accessors(qw/tablename columns groupby key type/);

sub new {
    my $class = shift;
    my $self  = { 'tablename' => shift, 'type' => shift };
    bless $self, $class;
    $self->_init();
    $self;
}

sub _init {
    my $self   = shift;
    my $tab    = $self->tablename;
    my $schema = $self->type eq 'rps' ? 'C_E1' : 'BP_WILEY';
    my $ldbh   = Common::Session::getdbh('localhost');
    my $cols   = $ldbh->selectcol_arrayref(
        qq{
SELECT c.column_name
FROM information_schema.columns c
WHERE c.table_schema=? AND c.table_name=? AND c.column_name NOT IN ('units','revenue')
ORDER BY c.ordinal_position
    }, undef, ( $schema, $tab )
    );
    $ldbh->disconnect;

	$cols = [ map { $_ eq 'author_id' ? "bc.contributor_id" : "s." . $_ } @{$cols} ];

    $self->columns($cols);
}

1;
