#!perl
use Cassandane::Tiny;

sub test_restore_notes_all_dryrun
    :min_version_3_5
{
    my ($self) = @_;

    my $jmap = $self->{jmap};

    # force creation of notes mailbox prior to creating notes
    my $res = $jmap->CallMethods([
        ['Note/set', {
         }, "R0"]
    ]);

    xlog "create notes";
    $res = $jmap->CallMethods([['Note/set', {create => {
                        "a" => {title => "a"},
                        "b" => {title => "b"},
                        "c" => {title => "c"},
                        "d" => {title => "d"}
                    }}, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Note/set', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);
    my $noteA = $res->[0][1]{created}{"a"}{id};
    my $noteB = $res->[0][1]{created}{"b"}{id};
    my $noteC = $res->[0][1]{created}{"c"}{id};
    my $noteD = $res->[0][1]{created}{"d"}{id};

    xlog "destroy note A, update note B";
    $res = $jmap->CallMethods([['Note/set', {
                    destroy => [$noteA],
                    update => {$noteB => {title => "B"}}
                }, "R2"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Note/set', $res->[0][0]);
    $self->assert_str_equals('R2', $res->[0][2]);

    xlog "get notes";
    $res = $jmap->CallMethods([
        ['Note/get', {
            properties => ['title', 'isFlagged'],
         }, "R3"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Note/get', $res->[0][0]);
    $self->assert_str_equals('R3', $res->[0][2]);

    my @expect = sort { $a->{title} cmp $b->{title} } @{$res->[0][1]{list}};

    my $mark = time();
    sleep 2;

    xlog "destroy note C, update notes B and D, create note E";
    $res = $jmap->CallMethods([['Note/set', {
                    destroy => [$noteC],
                    update => {
                        $noteB => {isFlagged => JSON::true},
                        $noteD => {isFlagged => JSON::true},
                    },
                    create => {
                        "e" => {title => "e"}
                    }
                }, "R4"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Note/set', $res->[0][0]);
    $self->assert_str_equals('R4', $res->[0][2]);
    my $noteE = $res->[0][1]{created}{"e"}{id};
    my $state = $res->[0][1]{newState};

    my $diff = time() - $mark;
    my $period = "PT" . $diff . "S";

    xlog "restore notes prior to most recent changes";
    $res = $jmap->CallMethods([['Backup/restoreNotes', {
                    performDryRun => JSON::true,
                    undoPeriod => $period,
                    undoAll => JSON::true
                }, "R5"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Backup/restoreNotes', $res->[0][0]);
    $self->assert_str_equals('R5', $res->[0][2]);
    $self->assert_num_equals(1, $res->[0][1]{numCreatesUndone});
    $self->assert_num_equals(2, $res->[0][1]{numUpdatesUndone});
    $self->assert_num_equals(1, $res->[0][1]{numDestroysUndone});

    xlog "get note updates";
    $res = $jmap->CallMethods([
        ['Note/changes', {
            sinceState => $state
         }, "R6.5"]
    ]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Note/changes', $res->[0][0]);
    $self->assert_str_equals('R6.5', $res->[0][2]);
    $self->assert_str_equals($state, $res->[0][1]{oldState});
    $self->assert_str_equals($state, $res->[0][1]{newState});
    $self->assert_equals(JSON::false, $res->[0][1]{hasMoreChanges});
    $self->assert_num_equals(0, scalar @{$res->[0][1]{created}});
    $self->assert_num_equals(0, scalar @{$res->[0][1]{updated}});
    $self->assert_num_equals(0, scalar @{$res->[0][1]{destroyed}});
}
