#!perl
use Cassandane::Tiny;

sub test_addressbook_set_destroy_contents
    :min_version_3_9 :needs_dependency_icalvcard
{
    my ($self) = @_;

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

    xlog "Create addressbook and contact";
    my $abookId = $carddav->NewAddressBook("foo");

    my $card = <<EOF;
BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
EMAIL;TYPE=INTERNET:user\@example.com
REV:2008-04-24T19:52:43Z
END:VCARD
EOF

    my $VCard = Net::CardDAVTalk::VCard->new_fromstring($card);
    my $cardId = basename($carddav->NewContact($abookId, $VCard), '.vcf');

    xlog "Destroy addressbook (with and without onDestroyRemoveContents)";
    my $res = $jmap->CallMethods([
        ['AddressBook/set', {
            destroy => [$abookId],
        }, 'R1'],
# XXX  Change to ContactCard/get once implemented
        ['Contact/get', {
            ids => [$cardId],
            properties => ['id'],
        }, 'R2'],
        ['AddressBook/set', {
            destroy => [$abookId],
            onDestroyRemoveContents => JSON::true,
        }, 'R3'],
        ['Contact/get', {
            ids => [$cardId],
            properties => ['id'],
        }, 'R2'],
    ]);
    $self->assert_str_equals('addressBookHasContents',
        $res->[0][1]{notDestroyed}{$abookId}{type});
    $self->assert_str_equals($cardId, $res->[1][1]{list}[0]{id});
    $self->assert_deep_equals([$abookId], $res->[2][1]{destroyed});
    $self->assert_deep_equals([$cardId], $res->[3][1]{notFound});
}
