package Product::ProductAlbum;

use strict;
use warnings;
use Carp;

use lib '/app/tools/sale_import/lib';
use Sale::Match;

use lib '/app/tools/common/lib';
use Common::RSDB;
use Common::Consts;
use Common::RSApp;

# private constants
# use constant DB_TABLE => "album";
use constant PRODUCT_TYPE => "A";

# private attributes
my @attributes = qw(product_id
  product_type_id
  product_title
  album_id
  client_album_id
  title
  title_clean
  release_date
  upc_ean
  upc_alt
  artist_id
  name
  label_id
  label_name
  catalog_number
  custom_1
  custom_2
  custom_3
);

# what should show up in xml by default
# $sql = "SELECT p.product_id, alb.*, art.artist_id,art.artist_name"
my @xml_attributes = qw(ProductID
  ProductType
  ProductTypeID
  ProductTitle
  AlbumID
  ClientAlbumID
  AlbumName
  AlbumNameClean
  ReleaseDate
  ReleaseDatePretty
  UPC
  UPCAlt
  ArtistID
  ArtistName
  LabelID
  LabelName
  CatalogID
  AlbumCustom1
  AlbumCustom2
  AlbumCustom3
);

#						NumberOfDiscs
#						DiscNumber

use lib '/app/tools/data_classes/lib';
use Item;
use base 'Item';

# --------------------------------
# Constructor
# --------------------------------
sub new {
    my $class = shift;
    my %args  = @_;

    my $self = $class->SUPER::new(@_);

    $self->_init(%args);

    return $self;
}

sub _init {
    my $self = shift;
    my %args = @_;

    # initialize user properties
    foreach (@attributes) {
        $self->{$_} = undef;
    }

    # are we loading an existing?
    if ( $args{product_id} ) {
        $self->Load( product_id => $args{product_id} );
    }
}

# --------------------------------
# Properties
# --------------------------------

sub ProductID {
    my $self = shift;
    return $self->{product_id};
}

sub ProductType {
    return PRODUCT_TYPE();
}

sub ProductTypeID {
    my $self = shift;
    return $self->{product_type_id};
}

sub ProductTitle {
    my $self = shift;
    return $self->{product_title};
}

sub AlbumID {
    my $self = shift;
    return $self->{album_id};
}

sub ClientAlbumID {
    my $self = shift;
    return $self->{client_album_id};
}

sub AlbumName {
    my $self = shift;
    return $self->{title};
}

sub AlbumNameClean {
    my $self = shift;
    return $self->{title_clean};
}

sub ReleaseDate {
    my $self = shift;
    return $self->{release_date};
}

sub ReleaseDatePretty {
    my $self = shift;
    return '' unless $self->{release_date};
    return Common::Client::Current()->Locale()->formatDate( $self->{release_date} );
}

sub UPC {
    my $self = shift;
    return $self->{upc_ean};
}

sub UPCAlt {
    my $self = shift;
    return $self->{upc_alt};
}

sub ArtistID {
    my $self = shift;
    return $self->{artist_id};
}

sub ArtistName {
    my $self = shift;
    return $self->{name};
}

sub LabelID {
    my $self = shift;
    return $self->{label_id};
}

sub LabelName {
    my $self = shift;
    return $self->{label_name};
}

sub CatalogID {
    my $self = shift;
    return $self->{catalog_number};
}

sub AlbumCustom1 {
    my $self = shift;
    return $self->{custom_1};
}

sub AlbumCustom2 {
    my $self = shift;
    return $self->{custom_2};
}

sub AlbumCustom3 {
    my $self = shift;
    return $self->{custom_3};
}

# leave them accessible for now and cluck into STDERR
sub DiscNumber {
    Carp::cluck('"ProductAlbum->DiscNumber" no longer supported');
    return undef;
}

sub DiscID {
    Carp::cluck('"ProductAlbum->DiscNumber" no longer supported');
    return undef;
}

sub NumberOfDiscs {
    Carp::cluck('"ProductAlbum->DiscNumber" no longer supported');
    return undef;
}

sub Error {
    my $self = shift;
    $self->{errstr};
}

sub GetObjectXML {
    my $self = shift;

    my $xml = $self->SUPER::GetObjectXML(@xml_attributes);

    return $xml;
}

# -------------------------------
# Public Methods
# -------------------------------
sub Load {
    my $self = shift;
    my %args = @_;

    # !!! disc_id?  We don't have an album_disc table.  We have a product_track table.
    # !!! I'll do my best to replace it, and we'll see what blows up.
    # !!! ... Actually, we don't normally care about disc_number for albums.  Disc_number, at
    # !!!     least in RPS, is only interesting for tracks.
    #
    my $sql;
    if ( defined $args{product_id} && $args{product_id} =~ /^\d+$/ ) {
        $sql =
            "SELECT"
          . " p.product_id,"
          . " p.product_type_id,"
          . " p.title as product_title,"
          . " p.upc_ean,"
          . " p.upc_alt,"
          . " p.release_date,"
          . " alb.*,"
          . " art.artist_id,"
          . " art.name,"
          . " l.label_name"

          . " FROM" . " product p," . " album alb," . " artist art," . " label l"

          . " WHERE p.product_id="
          . $self->{dbo}->DBQuote( $args{product_id} )
          . " AND p.asset_id=alb.album_id"
          . " AND p.product_type_id<>4"
          . " AND alb.artist_id=art.artist_id"
          . " AND alb.label_id=l.label_id";
    } else {
        $self->{errstr} = "product_id ($args{product_id}) not specified or not valid";
        return undef;
    }

    my $sth = $self->{dbo}->DoCmd($sql);
    unless ( defined $sth ) {
        $self->{errstr} = "database error: " . $DBI::errstr;
        return undef;
    }

    my $href = $sth->fetchrow_hashref();
    if ( !defined $href || $sth->rows == 0 ) {
        $self->{errstr} = "no product album record for product_id=$args{product_id}";
        return undef;
    }

    $self->_load($href);

    return 1;
}

sub get_track_durations {
    my $self = shift;

    my $sql = 'SELECT m.duration FROM track t, master m WHERE t.master_id = m.master_id AND t.album_id=' . $self->AlbumID;
    my $sth = $self->{dbo}->DBH()->prepare($sql);
    $sth->execute;
    my @list = map { $_->[0] } @{ $sth->fetchall_arrayref( [0] ) };

    return \@list;
}

# -------------------------------
# Private Methods
# -------------------------------
sub _load {
    my $self = shift;
    my $href = shift;

    map { $self->{$_} = $href->{$_} } @attributes;
}

###
1;    # Play nicely.
###
