#!perl
use Cassandane::Tiny;

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

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

    xlog $self, "create contacts";
    my $res = $jmap->CallMethods([
        ['Contact/set', {
            create => {
                contact1 => {
                    notes => 'cats and dogs',
                },
                contact2 => {
                    notes => 'hats and bats',
                },
            },
        }, 'R1'],
    ]);
    my $contactId1 = $res->[0][1]{created}{contact1}{id};
    $self->assert_not_null($contactId1);
    my $contactId2 = $res->[0][1]{created}{contact2}{id};
    $self->assert_not_null($contactId2);

    xlog "Query with loose terms";
    $res = $jmap->CallMethods([
        ['Contact/query', {
            filter => {
                notes => "cats dogs",
            },
        }, 'R1'],
        ['Contact/query', {
            filter => {
                operator => 'NOT',
                conditions => [{
                    notes => 'cats dogs',
                }],
            },
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$contactId1], $res->[0][1]{ids});
    $self->assert_deep_equals([$contactId2], $res->[1][1]{ids});

    xlog "Query with phrase";
    $res = $jmap->CallMethods([
        ['Contact/query', {
            filter => {
                notes => "'cats and dogs'",
            },
        }, 'R1'],
        ['Contact/query', {
            filter => {
                operator => 'NOT',
                conditions => [{
                    notes => "'cats and dogs'",
                }],
            },
        }, 'R1'],
    ]);
    $self->assert_deep_equals([$contactId1], $res->[0][1]{ids});
    $self->assert_deep_equals([$contactId2], $res->[1][1]{ids});

    xlog "Query with both phrase and loose terms";
    $res = $jmap->CallMethods([
        ['Contact/query', {
            filter => {
                notes => "cats 'cats and dogs' dogs",
            },
        }, 'R1'],
        ['Contact/query', {
            filter => {
                operator => 'NOT',
                conditions => [{
                    notes => "cats 'cats and dogs' dogs",
                }],
            },
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$contactId1], $res->[0][1]{ids});
    $self->assert_deep_equals([$contactId2], $res->[1][1]{ids});

    xlog "Query text";
    $res = $jmap->CallMethods([
        ['Contact/query', {
            filter => {
                text => "cats dogs",
            },
        }, 'R1'],
        ['Contact/query', {
            filter => {
                operator => 'NOT',
                conditions => [{
                    text => "cats dogs",
                }],
            },
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$contactId1], $res->[0][1]{ids});
    $self->assert_deep_equals([$contactId2], $res->[1][1]{ids});

    xlog "Query text and notes";
    $res = $jmap->CallMethods([
        ['Contact/query', {
            filter => {
                operator => 'AND',
                conditions => [{
                    text => "cats",
                }, {
                    notes => "dogs",
                }],
            },
        }, 'R1'],
        ['Contact/query', {

            filter => {
                operator => 'NOT',
                conditions => [{
                    operator => 'AND',
                    conditions => [{
                        text => "cats",
                    }, {
                        notes => "dogs",
                    }],
                }],
            },
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$contactId1], $res->[0][1]{ids});
    $self->assert_deep_equals([$contactId2], $res->[1][1]{ids});
}
