#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Main::Command::Redirect;
use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::Command;
use RPS::Dashboard::Command::Show;
use RPS::Catalog::Command::Main;
use base 'RPS::Command';

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

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

use lib '/app/tools/raptor/lib';
use Raptor::SaleReport::Command::Main;

sub cmd  { return 'redirect'; }
sub area { return 'main'; }

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    # The point of this command is to redirect users to the correct landing
    # page when they go to the default page.
    # Because it can vary by user level, and the landing page for some
    # users is forbidden for others, it's easiest to just have a
    # command like this that manages this for us.
    #

    # Reports and Marketing users don't have access to the dashboard.
    #
    my $accessLevel = $self->_getUser()->AccessLevel();
    if (   AppUser::DB::Item::AccessLevel::kLabelReport == $accessLevel
        || AppUser::DB::Item::AccessLevel::kMarketing == $accessLevel ) {
        my $reportCommand = Raptor::SaleReport::Command::Main->new();
        $response = RSApache::Response::Redirect->new( $reportCommand->getRedirect() );
    }

    # Catalog Only users can only access ... wait for it ... the Catalog tab.
    #
    elsif ( AppUser::DB::Item::AccessLevel::kCatalogOnly == $accessLevel ) {
        my $reportCommand = RPS::Catalog::Command::Main->new();
        $response = RSApache::Response::Redirect->new( $reportCommand->getRedirect() );
    } else {

        # Everybody else goes to the dashboard.
        #
        my $reportCommand = RPS::Dashboard::Command::Show->new();
        $response = RSApache::Response::Redirect->new( $reportCommand->getRedirect() );
    }

    return $response;
}

1;
