package Common::Amazon::SQS::EmailBounces;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use base 'Common::Amazon::SQS';

sub qname { 'EmailBounces' }

sub parse_message {
    my $self    = shift;
    my $message = shift;

    my $href = $self->message_ref($message);

    ## mail
    my $mail  = $href->{'mail'};
    my $sub   = $mail->{'commonHeaders'}->{'subject'};
    my $mtime = $self->parse_timestamp( $mail->{'timestamp'} );

    ## bounce
    my $bounce  = $href->{'bounce'};
    my $type    = $bounce->{'bounceType'};
    my $subtype = $bounce->{'bounceSubType'};
    my $btime   = $self->parse_timestamp( $bounce->{'timestamp'} );

    my $data;
    foreach my $rec ( @{ $bounce->{'bouncedRecipients'} } ) {
        my $add  = $rec->{'emailAddress'};
        my $act  = $rec->{'action'};
        my $diag = $rec->{'diagnosticCode'};
        push @{$data},
          {
            'email_address'   => $add,
            'email_subject'   => $sub,
            'diagnostic_code' => $diag,
            'bounce_action'   => $act,
            'bounce_type'     => $type,
            'bounce_subtype'  => $subtype,
            'bounced'         => $btime,
            'mailed'          => $mtime,
          };
    }
    $data;
}

1;
