package Artist;

use strict;

#use warnings;
use Carp;

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

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

# private attributes
my @attributes = qw(artist_id
  client_artist_id
  name
  artist_name_clean
);

# what should show up in xml by default
my @xml_attributes = qw(ArtistID
  ClientArtistID
  ArtistName
  ArtistNameClean
);

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{artist_id} ) {
        $self->Load( artist_id => $args{artist_id} );
    }
}

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

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

sub ClientArtistID {
    my $self = shift;
    return $self->{client_artist_id};
}

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

sub ArtistNameClean {
    my $self = shift;
    return $self->{artist_name_clean};
}

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

    my $sql;
    if ( defined $args{artist_id} && $args{artist_id} =~ /^\d+$/ ) {
        $sql = "SELECT * FROM artist WHERE artist_id = $args{artist_id}";
    } else {
        $self->{errstr} = "artist_id ($args{artist_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 artist record for artist_id=$args{artist_id}";
        return undef;
    }

    $self->_load($href);

    return 1;
}

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

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

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