#!perl
use Cassandane::Tiny;

sub test_cardgroup_set_destroy
    :min_version_3_9 :needs_dependency_icalvcard
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $service = $self->{instance}->get_service("http");
    $ENV{DEBUGDAV} = 1;
    my $carddav = Net::CardDAVTalk->new(
        user => 'cassandane',
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $id = 'ae2640cc-234a-4dd9-95cc-3106258445b9';
    my $member1 = '03a0e51f-d1aa-4385-8a53-e29025acd8af';
    my $member2 = 'b8767877-b4a1-4c70-9acc-505d3819e519';
    my $href = "Default/test.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:4.0
KIND:group
UID:$id
FN:The Doe Family
MEMBER:urn:uuid:$member1
MEMBER:urn:uuid:$member2
END:VCARD
EOF

    $card =~ s/\r?\n/\r\n/gs;
    $carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

    my $res = $jmap->CallMethods([
        ['ContactCard/get', { } , 'R1']
    ]);
    my $cardId = $res->[0][1]{list}[0]{id};
    $self->assert_not_null($cardId);

    $res = $jmap->CallMethods([
        ['ContactCard/set', {
            destroy => [$cardId]
        }, 'R1']
    ]);

    $self->assert_str_equals($cardId, $res->[0][1]{destroyed}[0]);

    $res = $jmap->CallMethods([
        ['ContactCard/get', {
        }, 'R1']
    ]);

    $self->assert_num_equals(0, scalar @{$res->[0][1]{list}});
}
