#!perl
use Cassandane::Tiny;

sub test_emailsubmission_cancel_creation
    :min_version_3_1
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();

    my $res = $jmap->CallMethods( [ [ 'Identity/get', {}, "R1" ] ] );
    my $identityId = $res->[0][1]->{list}[0]->{id};
    $self->assert_not_null($identityId);

    xlog $self, "create mailboxes";
    $imap->create("INBOX.A") or die;
    $imap->create("INBOX.B") or die;
    $res = $jmap->CallMethods([
        ['Mailbox/get', { properties => ['name'], }, "R1"]
    ]);
    my %mboxByName = map { $_->{name} => $_ } @{$res->[0][1]{list}};
    my $mboxIdA = $mboxByName{A}->{id};
    my $mboxIdB = $mboxByName{B}->{id};

    xlog $self, "create, send and update email";
    $res = $jmap->CallMethods([
        ['Email/set', {
            create => {
                'm1' => {
                    mailboxIds => {
                        $mboxIdA => JSON::true,
                    },
                    from => [{
                        name => '', email => 'foo@local'
                    }],
                    to => [{
                        name => '', email => 'bar@local'
                    }],
                    subject => 'hello',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part1',
                    },
                    bodyValues => {
                        part1 => {
                            value => 'world',
                        }
                    },
                },
            },
        }, 'R1'],
        [ 'EmailSubmission/set', {
            create => {
                's1' => {
                    identityId => $identityId,
                    emailId  => '#m1',
                    envelope => {
                        mailFrom => {
                            email => 'foo@local',
                            parameters => {
                                "holdfor" => "30",
                            }
                        },
                        rcptTo => [{
                            email => 'bar@local',
                        }],
                    },
                },
           },
           onSuccessUpdateEmail => {
               '#s1' => {
                    mailboxIds => {
                        $mboxIdB => JSON::true,
                    },
               },
           },
        }, 'R2' ],
        [ 'Email/get', {
            ids => ['#m1'],
            properties => ['mailboxIds'],
        }, 'R3'],
    ]);

    xlog $self, "event gets added to the alarmdb";
    my $alarmdata = $self->{instance}->getalarmdb();
    $self->assert_num_equals(1, scalar @$alarmdata);

    my $emailId = $res->[0][1]->{created}{m1}{id};
    $self->assert_not_null($emailId);
    my $msgSubId = $res->[1][1]->{created}{s1}{id};
    $self->assert_not_null($msgSubId);
    $self->assert(exists $res->[2][1]{updated}{$emailId});
    $self->assert_num_equals(1, scalar keys %{$res->[3][1]{list}[0]{mailboxIds}});
    $self->assert(exists $res->[3][1]{list}[0]{mailboxIds}{$mboxIdB});

    xlog $self, "cancel the send and revert the mailbox";
    $res = $jmap->CallMethods([
        [ 'EmailSubmission/set', {
            update => {
                $msgSubId => {
                    undoStatus => 'canceled',
                }
            },
            onSuccessUpdateEmail => {
                $msgSubId => {
                    mailboxIds => {
                        $mboxIdA => JSON::true,
                    },
                },
            },
        }, 'R2' ],
        [ 'Email/get', {
            ids => [$emailId],
            properties => ['mailboxIds'],
        }, 'R3'],
    ]);

    $self->assert(exists $res->[0][1]{updated}{$msgSubId});
    $self->assert(exists $res->[1][1]{updated}{$emailId});
    $self->assert_num_equals(1, scalar keys %{$res->[2][1]{list}[0]{mailboxIds}});
    $self->assert(exists $res->[2][1]{list}[0]{mailboxIds}{$mboxIdA});

    xlog $self, "event is no longer in the alarmdb";
    $alarmdata = $self->{instance}->getalarmdb();
    $self->assert_num_equals(0, scalar @$alarmdata);

    xlog $self, "destroy and destroy the email too";
    $res = $jmap->CallMethods([
        [ 'EmailSubmission/set', {
            destroy => [$msgSubId],
            onSuccessDestroyEmail => [$msgSubId],
        }, 'R2' ],
        [ 'Email/get', {
            ids => [$emailId],
            properties => ['mailboxIds'],
        }, 'R3'],
    ]);

    $self->assert_str_equals($msgSubId, $res->[0][1]{destroyed}[0]);
    $self->assert_str_equals($emailId, $res->[1][1]{destroyed}[0]);
    $self->assert_str_equals($emailId, $res->[2][1]{notFound}[0]);
}
