package API::Command::GetStatementInfo;
use strict;
use warnings;

use Apache2::Const qw(OK FORBIDDEN HTTP_OK HTTP_UNAUTHORIZED HTTP_NOT_FOUND HTTP_NOT_IMPLEMENTED);

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

use lib '/app/tools/api/lib';
use API::Response;

use base 'API::Command::StatementBase';

sub area { return "client"; }
sub cmd  { return "statement"; }

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

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

    my $clientID = $self->getRouteParam(":clientId");
    my $runType  = $self->getRouteParam(":runTypeId");
    my $stmtID   = $self->getRouteParam(":statementId");

    my $userID = $self->{userID};    # derived from session (see SUPER::execute)

    $ENV{HOME} = '/';

    # verify that the user has access to the client

    if ( !$self->canAccess( $userID, $clientID ) ) {
        return $self->createResponse(
            status => HTTP_UNAUTHORIZED,
            data   => { msg => "user not authorized for client" }
        );
    }

    if ( $runType =~ /^artist$/i ) {

        my $appSingleton = Common::RSApp->new_temporary( clientID => $clientID );

        my ( $status, $_statementData ) = $self->getArtistStatementData( $userID, $clientID, $stmtID );

        return $self->createResponse(
            status => $status,
            data   => \%$_statementData
        );

    } else {
        return $self->createResponse(
            status => HTTP_NOT_IMPLEMENTED,
            data   => { msg => "invalid runtype" }
        );
    }
}

1;
