#!perl
use Cassandane::Tiny;

sub test_email_query_guidsearch_inmailboxotherthan
    :min_version_3_3 :needs_component_sieve :needs_component_jmap
    :JMAPExtensions
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imap = $self->{store}->get_client();

    my $using = [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:mail',
        'urn:ietf:params:jmap:submission',
        'https://cyrusimap.org/ns/jmap/mail',
        'https://cyrusimap.org/ns/jmap/quota',
        'https://cyrusimap.org/ns/jmap/debug',
        'https://cyrusimap.org/ns/jmap/performance',
    ];

    xlog $self, "create mailboxes";
    $imap->create("INBOX.A") or die;

    my $res = $jmap->CallMethods([
        ['Mailbox/get', {
            properties => ['name', 'parentId'],
        }, "R1"]
    ], $using);

    my %mboxByName = map { $_->{name} => $_ } @{$res->[0][1]{list}};
    my $mboxA = $mboxByName{'A'}->{id};
    $self->assert_not_null($mboxA);
    my $inbox = $mboxByName{'Inbox'}->{id};
    $self->assert_not_null($inbox);

    xlog $self, "create emails";
    $res = $jmap->CallMethods([
        ['Email/set', {
            create => {
                'msgInbox' => {
                    mailboxIds => {
                        $inbox => JSON::true,
                    },
                    from => [{
                        name => '', email => 'from@local'
                    }],
                    to => [{
                        name => '', email => 'to@local'
                    }],
                    subject => 'msgInbox',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part1',
                    },
                    bodyValues => {
                        part1 => {
                            value => 'test',
                        }
                    },
                },
                'msgA' => {
                    mailboxIds => {
                        $mboxA => JSON::true,
                    },
                    from => [{
                        name => '', email => 'from@local'
                    }],
                    to => [{
                        name => '', email => 'to@local'
                    }],
                    subject => 'msgA',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part1',
                    },
                    bodyValues => {
                        part1 => {
                            value => 'test',
                        }
                    },
                },
            },
        }, 'R1'],
    ], $using);
    my $emailInbox = $res->[0][1]->{created}{msgInbox}{id};
    $self->assert_not_null($emailInbox);
    my $emailA = $res->[0][1]->{created}{msgA}{id};
    $self->assert_not_null($emailA);

    xlog $self, "run squatter";
    $self->{instance}->run_command({cyrus => 1}, 'squatter');

    xlog "Running query with guidsearch";
    $res = $jmap->CallMethods([
        ['Email/query', {
            filter => {
                operator => 'AND',
                conditions => [{
                    body => 'test',
                    inMailboxOtherThan => [
                        $inbox,
                    ],
                }],
            },
            collapseThreads => JSON::true,
            findAllInThread => JSON::true,
        }, 'R1']
    ], $using);
    $self->assert_equals(JSON::true, $res->[0][1]{performance}{details}{isGuidSearch});
    my @wantIds = sort ($emailA);
    $self->assert_deep_equals(\@wantIds, $res->[0][1]{ids});
}
