#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package AppUser::User::Command::Search;
use strict;
use warnings;

use lib '/app/tools/appuser/lib';
use AppUser::User::User;
use AppUser::User::AccessLevelList;
use AppUser::DB::Item::User;
use AppUser::DB::Item::AccessLevel;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;

use lib '/app/tools/rps/lib';
use RPS::SearchCommand;
use base 'RPS::SearchCommand';

sub cmd  { return 'search'; }
sub area { return 'user'; }

sub _template { return "/app/tools/appuser/templates/user/index.xsl"; }

sub execute {
    my ($self) = @_;

    print STDERR "in search\n";

    my $response = $self->SUPER::execute(

        #        dbClass => 'AppUser::DB::Item::User',
        dbClass  => 'AppUser::User::Command::SearchDBItem',
        xmlClass => 'AppUser::User::User',

        # !!! These callback members are now deprecated.
        # SearchCommand will now just invoke the Search or GetAll methods of the
        # dbClass.
        # We will declare a special subclass of User later in this package that does the special
        # stuff we want.
        #
        getAllCallback => \&AppUser::User::Command::Search::GetAll,
        searchCallback => \&AppUser::User::Command::Search::Search,
        xmlTag         => 'AppUser',
        loadSubs       => 1,                                          # so we can show address info
        pageSize       => 30,
    );
    return $response if $response;

    # We need this list to figure out who can edit whom.
    $self->{xml}->{AccessLevelList} = AppUser::User::AccessLevelList->new();

    $response = RSApache::Response::XSLT->new( $self->{xml}, $self->_template() );
    return $response;
}

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

# jpk - the SearchCommand base class is designed to use callbacks to fetch the DB::ItemCollection it will work on.
# This is ok, but there is the issue of enabling different sorts of queries to occur depending on 'random' circumstances.
#
# In this case, we need to filter out RoyaltyShare users when this command is executed by a non-royaltyshare user.
# What I do _not_ want to do is stuff a bunch of user_id/user_level checking code into the AppUser::DB::Item::User... that
# would be poor encapsulation.   But, the current Search command infrastructure does not give me a way to pass additional
# arguments to the DB::Item accessor methods directly.
#
# So, I'm going to create some callback methods here that I'll use to inject additional arguments into these queries.
#
sub GetAll {
    my ( $class, $hParams ) = @_;

    # Exclude royaltyshare users unless this user is a level 1 or 2
    # (this would be a lot simpler if these were virtual methods, not static class methods...)
    #
    my $activeUserID = Common::RSApp::GetActiveUserID();
    my $user         = AppUser::User::User->new( userID => $activeUserID );
    my $accessLevel  = $user->AccessLevel();

    if (   AppUser::DB::Item::AccessLevel::kRSAdmin == $accessLevel
        || AppUser::DB::Item::AccessLevel::kRSStandard == $accessLevel
        || AppUser::DB::Item::AccessLevel::kRSAccountRep == $accessLevel ) {
        return AppUser::DB::Item::User->Get(excludeRS => 0, %$hParams);
    }
    return AppUser::DB::Item::User->Get( excludeRS => 1, %$hParams );
}

sub Search {
    my ( $class, $arg ) = @_;

    # Exclude royaltyshare users unless this user is a level 1 or 2
    # (this would be a lot simpler if these were virtual methods, not static class methods...)
    #
    my $activeUserID = Common::RSApp::GetActiveUserID();
    my $user         = AppUser::User::User->new( userID => $activeUserID );
    my $accessLevel  = $user->AccessLevel();

    if (   AppUser::DB::Item::AccessLevel::kRSAdmin == $accessLevel
        || AppUser::DB::Item::AccessLevel::kRSAccountRep == $accessLevel ) {
        return AppUser::DB::Item::User->Search($arg);
    }

    # The second param here is the 'excludeRSFlag'... yes, that's pretty cheesy.
    #
    return AppUser::DB::Item::User->Search( $arg, 1 );
}

1;
