#!perl
use Cassandane::Tiny;
use Cyrus::IndexFile;

sub test_19_replicate_to_19
    :MailboxVersion(19) :Replication
    ($self)
{
    my $jmap = $self->{jmap};

    xlog "Create calendar and event";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            create => {
                1 => {
                    name => 'test',
                },
            },
        }, 'R1'],
        ['CalendarEvent/set', {
            create => {
                2 => {
                    uid => 'eventuid1local',
                    calendarIds => {
                        '#1' => JSON::true,
                    },
                    title => "event1",
                    start => "2020-03-30T11:00:00",
                    duration => "PT1H",
                    timeZone => "Australia/Melbourne",
                },
            },
        }, 'R2'],
    ]);
    my $calendarId = $res->[0][1]{created}{1}{id};
    my $uniqueId = $res->[0][1]{created}{1}{mailboxUniqueId};
    $self->assert_not_null($calendarId);
    my $eventId = $res->[1][1]{created}{2}{id};
    $self->assert_not_null($eventId);

    $self->run_replication();
    $self->check_replication('cassandane');

    xlog $self, "Verify conv.db is v1";
    my $basedir = $self->{replica}->{basedir};
    my $outfile = "$basedir/conv-output.txt";
    $self->{instance}->run_command({ cyrus => 1,
                                     redirects => { stdout => $outfile } },
                                   'ctl_conversationsdb', '-d', 'cassandane');
    my $data = slurp_file($outfile);
    $self->assert_matches(qr/\$VERSION\t1/, $data);

    xlog $self, "Verify INBOX with index v19";
    my $dir = $self->{replica}->folder_to_directory('user.cassandane');
    my $file = "$dir/cyrus.index";
    my $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    my $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(19, $index->header('MinorVersion'));

    xlog $self, "Verify calendar with index v19";
    my $mboxname = "user.cassandane.#calendars.$calendarId";
    $dir = $self->{replica}->folder_to_directory($mboxname);
    $file = "$dir/cyrus.index";
    $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(19, $index->header('MinorVersion'));

    xlog "Destroy calendar with onDestroyEvents";
    $res = $jmap->CallMethods([
        ['Calendar/set', {
            destroy => [$calendarId],
            onDestroyRemoveEvents => JSON::true,
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [$eventId],
            properties => ['id'],
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$calendarId], $res->[0][1]{destroyed});
    $self->assert_deep_equals([$eventId], $res->[1][1]{notFound});

    $self->run_replication();
    $self->check_replication('cassandane');

    xlog $self, "Verify deleted calendar with index v19";
    my $adminstore = $self->{adminstore};
    my $admintalk = $adminstore->get_client();
    my $list = $admintalk->list("DELETED.$mboxname", '*');
    $mboxname = $list->[0][2];

    $dir = $self->{replica}->folder_to_directory($mboxname);
    $file = "$dir/cyrus.index";
    $fh = IO::File->new($file, "+<");
    die "NO SUCH FILE $file" unless $fh;
    $index = Cyrus::IndexFile->new($fh);
    $self->assert_num_equals(19, $index->header('MinorVersion'));
}
