#!perl
use Cassandane::Tiny;

sub test_email_copy_hasattachment
    :min_version_3_1 :needs_component_sieve :needs_component_jmap
    :JMAPNoHasAttachment
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $imaptalk = $self->{store}->get_client();
    my $admintalk = $self->{adminstore}->get_client();

    xlog $self, "Create user and share mailbox";
    $self->{instance}->create_user("other");
    $admintalk->setacl("user.other", "cassandane", "lrsiwntex") or die;

    my $srcInboxId = $self->getinbox()->{id};
    $self->assert_not_null($srcInboxId);

    my $dstInboxId = $self->getinbox({accountId => 'other'})->{id};
    $self->assert_not_null($dstInboxId);

    xlog $self, "create emails";
    my $res = $jmap->CallMethods([
        ['Email/set', {
            create => {
                1 => {
                    mailboxIds => {
                        $srcInboxId => JSON::true,
                    },
                    keywords => {
                        'foo' => JSON::true,
                        '$seen' => JSON::true,
                    },
                    subject => 'email1',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part1',
                    },
                    bodyValues => {
                        part1 => {
                            value => 'part1',
                        }
                    },
                },
                2 => {
                    mailboxIds => {
                        $srcInboxId => JSON::true,
                    },
                    keywords => {
                        'foo' => JSON::true,
                        '$seen' => JSON::true,
                    },
                    subject => 'email2',
                    bodyStructure => {
                        type => 'text/plain',
                        partId => 'part2',
                    },
                    bodyValues => {
                        part2 => {
                            value => 'part2',
                        }
                    },
                },
            },
        }, 'R1'],
    ]);
    my $emailId1 = $res->[0][1]->{created}{1}{id};
    $self->assert_not_null($emailId1);
    my $emailId2 = $res->[0][1]->{created}{2}{id};
    $self->assert_not_null($emailId2);

    xlog $self, "set hasAttachment";
    my $store = $self->{store};
    $store->set_folder('INBOX');
    $store->_select();
    my $talk = $store->get_client();
    $talk->store('1:2', '+flags', '($HasAttachment)') or die;

    xlog $self, "copy email";
    $res = $jmap->CallMethods([
        ['Email/copy', {
            fromAccountId => 'cassandane',
            accountId => 'other',
            create => {
                1 => {
                    id => $emailId1,
                    mailboxIds => {
                        $dstInboxId => JSON::true,
                    },
                },
                2 => {
                    id => $emailId2,
                    mailboxIds => {
                        $dstInboxId => JSON::true,
                    },
                    keywords => {
                        'baz' => JSON::true,
                    },
                },
            },
        }, 'R1'],
    ]);

    my $copiedEmailId1 = $res->[0][1]->{created}{1}{id};
    $self->assert_not_null($copiedEmailId1);
    my $copiedEmailId2 = $res->[0][1]->{created}{2}{id};
    $self->assert_not_null($copiedEmailId2);

    xlog $self, "get copied email";
    $res = $jmap->CallMethods([
        ['Email/get', {
            accountId => 'other',
            ids => [$copiedEmailId1, $copiedEmailId2],
            properties => ['keywords'],
        }, 'R1']
    ]);
    my $wantKeywords1 = {
        '$hasattachment' => JSON::true,
        foo => JSON::true,
        '$seen' => JSON::true,
    };
    my $wantKeywords2 = {
        '$hasattachment' => JSON::true,
        baz => JSON::true,
    };
    $self->assert_deep_equals($wantKeywords1, $res->[0][1]{list}[0]{keywords});
    $self->assert_deep_equals($wantKeywords2, $res->[0][1]{list}[1]{keywords});
}
