#!perl
use Cassandane::Tiny;

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

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

    my $body = "--047d7b33dd729737fe04d3bde348\r\n";
    $body .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $body .= "\r\n";
    $body .= "This is the lady plain text part.";
    $body .= "\r\n";
    $body .= "--047d7b33dd729737fe04d3bde348\r\n";
    $body .= "Content-Type: text/html;charset=\"UTF-8\"\r\n";
    $body .= "\r\n";
    $body .= "<html><body><p>This is the lady html part.</p></body></html>";
    $body .= "\r\n";
    $body .= "--047d7b33dd729737fe04d3bde348--\r\n";

    $self->make_message("lady subject",
        mime_type => "multipart/alternative",
        mime_boundary => "047d7b33dd729737fe04d3bde348",
        body => $body
    ) || die;

    $body = "--h8h89737fe04d3bde348\r\n";
    $body .= "Content-Type: text/plain; charset=UTF-8\r\n";
    $body .= "\r\n";
    $body .= "This is the foobar plain text part.";
    $body .= "\r\n";
    $body .= "--h8h89737fe04d3bde348\r\n";
    $body .= "Content-Type: text/html;charset=\"UTF-8\"\r\n";
    $body .= "\r\n";
    $body .= "<html><body><p>This is the lady html part.</p></body></html>";
    $body .= "\r\n";
    $body .= "--h8h89737fe04d3bde348--\r\n";

    $self->make_message("foobar subject",
        mime_type => "multipart/alternative",
        mime_boundary => "h8h89737fe04d3bde348",
        body => $body
    ) || die;


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

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

    my $res = $jmap->CallMethods([
        ['Email/query', {
            filter => {text => "foobar"},
            findMatchingParts => JSON::true,
        },"R1"],
    ], $using);
    my $emailIds = $res->[0][1]{ids};
    my $partIds = $res->[0][1]{partIds};

    my $fooId = $emailIds->[0];

    $self->assert_num_equals(1, scalar @$emailIds);
    $self->assert_num_equals(1, scalar keys %$partIds);
    $self->assert_num_equals(1, scalar @{$partIds->{$fooId}});
    $self->assert_str_equals("1", $partIds->{$fooId}[0]);

    $res = $jmap->CallMethods([
        ['Email/query', {
            filter => {text => "lady"},
            findMatchingParts => JSON::true,
        }, "R1"],
    ], $using);
    $emailIds = $res->[0][1]{ids};
    $partIds = $res->[0][1]{partIds};

    my ($ladyId) = grep { $_ ne $fooId } @$emailIds;

    $self->assert_num_equals(2, scalar @$emailIds);
    $self->assert_num_equals(2, scalar keys %$partIds);
    $self->assert_num_equals(1, scalar @{$partIds->{$fooId}});
    $self->assert_num_equals(2, scalar @{$partIds->{$ladyId}});
    $self->assert_not_null(grep { $_ eq "2" } @{$partIds->{$fooId}});
    $self->assert_not_null(grep { $_ eq "1" } @{$partIds->{$ladyId}});
    $self->assert_not_null(grep { $_ eq "2" } @{$partIds->{$ladyId}});
}
