# In order to make this as fast as possible, I'm going to avoid the generic DB::Item::Album class and create a custom class.
# That way we can join album and label in a single query, and avoid a lot of overhead.
#
package RPS::DB::Item::AlbumListData;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/rps/lib';
use Common::Assert;

use base 'Common::DB::Item';

use constant kDB => Common::DB::Item::kClientDB();

# We'll return the same columns for both types of license reports.
#
sub _config {
    my ($class) = @_;

    return {
        'album_title'    => {},
        'artist_name'    => {},
        'label_id'       => {},
        'catalog_number' => {},
        'status'         => {},
        'album_id'       => {},
    };
}

sub GetList {
    my ( $class, $label_id ) = @_;

    my $sql =
        "SELECT album.title as album_title, album.album_id as album_id, album.catalog_number as catalog_number, artist.name as artist_name,"
      . "album.status as status, album.label_id as label_id"
      . " FROM album LEFT JOIN artist USING (artist_id)";

    if ($label_id) {
        $sql .= " WHERE album.label_id=" . $class->quote($label_id);
    }

    $sql .= ' ORDER BY title,artist_id';

    return $class->GetAll($sql);
}

1;
