package Common::EventLogger;

use strict;
use warnings;
use Data::Dumper;

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

use vars qw(@ISA @EXPORT);

@EXPORT = qw(LogEvent);
@ISA    = 'Exporter';

# Event Constants

# User events
use constant kUserLogin          => 100;
use constant kUserLoginAs        => 101;
use constant kUserLogout         => 102;
use constant kUserLoginFailed    => 103;
use constant kUserChangePW       => 104;
use constant kUserForgotPWEmail  => 105;
use constant kUserForgotPWSubmit => 106;
use constant kUserUpdateProfile  => 107;
use constant kUserEdit           => 108;
use constant kUserDisable        => 109;
use constant kUserCreate         => 110;
use constant kUserImport         => 111;

# Raptor events
use constant kUploadFile        => 200;
use constant kFinishFile        => 201;
use constant kDeleteFile        => 202;
use constant kMakeMatch         => 203;
use constant kUndoMatch         => 204;
use constant kSetConversionRate => 205;
use constant kSetRevenue        => 206;
use constant kClosePeriod       => 207;
use constant kDownloadReport    => 208;

use constant kDbTable => 'event_log';

# ---
# sub: LogEvent
#  in: event id, hashref of params
# out: n/a
# ---
# in: event_id
#   all event constants are established in this file
#
# in: hashref of params
#   parameters relevant to the event are usually
#   included in the hashref, as well as useful "notes"
#   regarding the event.
#
# Example (mod_perl):
#
# 	use Common::EventLogger;
#
# 	LogEvent(Common::EventLogger::kFinishFile, { file_id => $file_id, notes => "File was split." });
# ---
sub LogEvent {
    my ( $eventID, $params ) = @_;

    if ( !( $eventID && $eventID > 0 ) ) {
        warn "LogEvent: invalid eventID ($eventID) passed";
        return undef;
    }

    # validate eventID
    # ???
    warn "LogEvent: second parameter must be a hashref" if ( !ref($params) eq 'HASH' );

    my $masterUserID = Common::RSApp::GetMasterUserID();
    my $activeUserID = Common::RSApp::GetActiveUserID();
    my $ipAddr       = Common::RSApp::GetIPAddr();
    my $paramStr     = _hrefToString($params);

    # sanity check...
    if ( !( $masterUserID >= 0 && $activeUserID >= 0 && defined $ipAddr && defined $paramStr ) ) {
        warn "LogEvent: invalid required params";
        return undef;
    }

    my $sql =
        "INSERT INTO "
      . kDbTable
      . " (event_id, master_user_id, active_user_id, ip_address, params, date_created)"
      . "  VALUES (?, ?, ?, ?, ?, now())";

    my $dbObj = Common::RSApp::GetClientDB();
    my $sth   = $dbObj->Prepare($sql);
    if ( !$sth ) {
        warn "LogEvent: sql prepare failed: " . $sth->errstr;
        return undef;
    }

    if ( !$sth->execute( $eventID, $masterUserID, $activeUserID, $ipAddr, $paramStr ) ) {
        warn "LogEvent: sql execute failed: " . $sth->errstr;
        return undef;
    }

    return 1;
}

# ---
# sub: _hrefToString
#  in: hashref of parameters
# out: string representation of hashref
# ---
# in: params
# 	a list of key=>value pairs to be stored in the
# 	params field of the event_log table
#
# Purpose:
# 	Store the hashref in a standardized way such that
# 	the original structure can be retrieved later
# ---
sub _hrefToString {
    my $params = shift;

    if ( keys %$params ) {
        return Dumper($params);
    } else {
        return "";
    }
}

# ---
# sub: _stringToHref
#  in: string value returned from sql
# out: hashref of key=>value pairs
# ---
# in: string
# 	the raw Dumper value stored by the db
#
# out: hashref
# 	the hashref of key=>value pairs extracted
# 	from the raw db string
# ---
sub _stringToHref {
    my $paramStr = shift;
    my $VAR1     = {};

    eval $paramStr;

    return $VAR1;
}

###
1;    # Play nicely.
###
