#!perl
use Cassandane::Tiny;

sub test_mailbox_get_inbox_sub
    :min_version_3_1 :NoAltNameSpace
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $imaptalk = $self->{store}->get_client();

    $imaptalk->create("INBOX.INBOX.foo")
        or die "Cannot create mailbox INBOX.INBOX.foo: $@";

    $imaptalk->create("INBOX.INBOX.foo.bar")
        or die "Cannot create mailbox INBOX.INBOX.foo.bar: $@";

    xlog $self, "get existing mailboxes";
    my $res = $jmap->CallMethods([['Mailbox/get', {}, "R1"]]);
    $self->assert_not_null($res);
    $self->assert_str_equals('Mailbox/get', $res->[0][0]);
    $self->assert_str_equals('R1', $res->[0][2]);

    my %m = map { $_->{name} => $_ } @{$res->[0][1]{list}};
    $self->assert_num_equals(3, scalar keys %m);
    my $inbox = $m{"Inbox"};
    my $foo = $m{"foo"};
    my $bar = $m{"bar"};

    # INBOX
    $self->assert_str_equals("Inbox", $inbox->{name});
    $self->assert_null($inbox->{parentId});
    $self->assert_str_equals("inbox", $inbox->{role});
    $self->assert_num_equals(1, $inbox->{sortOrder});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{mayReadItems});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{mayAddItems});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{mayRemoveItems});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{mayCreateChild});
    $self->assert_equals(JSON::false, $inbox->{myRights}->{mayRename});
    $self->assert_equals(JSON::false, $inbox->{myRights}->{mayDelete});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{maySetSeen});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{maySetKeywords});
    $self->assert_equals(JSON::true, $inbox->{myRights}->{maySubmit});
    $self->assert_num_equals(0, $inbox->{totalEmails});
    $self->assert_num_equals(0, $inbox->{unreadEmails});
    $self->assert_num_equals(0, $inbox->{totalThreads});
    $self->assert_num_equals(0, $inbox->{unreadThreads});

    # INBOX.INBOX.foo
    $self->assert_str_equals("foo", $foo->{name});
    $self->assert_str_equals($inbox->{id}, $foo->{parentId});
    $self->assert_null($foo->{role});
    $self->assert_num_equals(10, $foo->{sortOrder});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayReadItems});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayAddItems});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayRemoveItems});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayCreateChild});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayRename});
    $self->assert_equals(JSON::true, $foo->{myRights}->{mayDelete});
    $self->assert_num_equals(0, $foo->{totalEmails});
    $self->assert_num_equals(0, $foo->{unreadEmails});
    $self->assert_num_equals(0, $foo->{totalThreads});
    $self->assert_num_equals(0, $foo->{unreadThreads});

    # INBOX.INBOX.foo.bar
    $self->assert_str_equals("bar", $bar->{name});
    $self->assert_str_equals($foo->{id}, $bar->{parentId});
    $self->assert_null($bar->{role});
    $self->assert_num_equals(10, $bar->{sortOrder});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayReadItems});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayAddItems});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayRemoveItems});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayCreateChild});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayRename});
    $self->assert_equals(JSON::true, $bar->{myRights}->{mayDelete});
    $self->assert_num_equals(0, $bar->{totalEmails});
    $self->assert_num_equals(0, $bar->{unreadEmails});
    $self->assert_num_equals(0, $bar->{totalThreads});
    $self->assert_num_equals(0, $bar->{unreadThreads});
}
