package AppUser::DB::Item::UserClientLabelMap;
use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::DB::Item;
use Common::Assert;
use Common::RSApp;
use base 'Common::DB::Item';

use constant kTable => 'user_client_label_map';
use constant kDB    => Common::DB::Item::kCommonDB();

# JPK - Let's try building an accessor that returns label users as a sorted list.

sub GetAllForClientAndLabel {
    my ( $class, $clientID, $labelID, %options ) = @_;

    # I'm going to include (hidden, really) the user last name and first name for sorting purposes.
    #
    my $sql =
        'SELECT '
      . kTable
      . '.*, user.last_name, user.first_name FROM '
      . kTable
      . ' LEFT JOIN user USING (user_id) WHERE client_id='
      . $class->quote($clientID)
      . ' AND label_id='
      . $class->quote($labelID)
      . " ORDER BY user.last_name,user.first_name";
    return $class->GetAll($sql);
}

sub GetByUserIDClientID {
    my ( $class, $userID, $clientID ) = @_;

    #    assert($userID);
    #    assert($clientID);

    my $sql = "SELECT * FROM " . kTable . " WHERE user_id=$userID and client_id=$clientID";

    # I want to sort this list by label name...
    #

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

sub DeleteByUserIDClientID {
    my ( $class, $userID, $clientID ) = @_;

    my $dbo = Common::RSApp::GetCommonDB();

    my $sql = "DELETE FROM " . kTable . " WHERE user_id=$userID and client_id=$clientID";

    $dbo->DoCmd($sql);
}

sub DeleteByLabelIDClientID {
    my ( $class, $labelID, $clientID ) = @_;

    my $dbo = Common::RSApp::GetCommonDB();

    my $sql = "DELETE FROM " . kTable . " WHERE label_id=$labelID and client_id=$clientID";

    $dbo->DoCmd($sql);
}
1;

