#!perl
use Cassandane::Tiny;

sub test_cardgroup_get_v3
    :min_version_3_9 :needs_component_jmap :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 = 'urn:uuid:03a0e51f-d1aa-4385-8a53-e29025acd8af';
    my $member2 = 'urn:uuid:b8767877-b4a1-4c70-9acc-505d3819e519';
    my $href = "Default/$id.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:3.0
X-ADDRESSBOOKSERVER-KIND:group
UID:$id
FN:The Doe Family
N:;;;;
X-ADDRESSBOOKSERVER-MEMBER:$member1
X-ADDRESSBOOKSERVER-MEMBER:$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 $want_jscard = {
        '@type' => 'Card',
        version => '1.0',
        addressBookId => 'Default',
        'cyrusimap.org:href' => $carddav->fullpath() . $href,
        id => $id,
        uid => $id,
        kind => 'group',
        vCardProps => [
            [ 'version', {}, 'text', '3.0' ]
        ],
        name => {
            full => 'The Doe Family'
        },
        members => {
            $member1 => JSON::true,
            $member2 => JSON::true
        }
    };

    
    my $have_jscard = $res->[0][1]{list}[0];

    # Delete generated fields
    delete $have_jscard->{blobId};
    delete $have_jscard->{'cyrusimap.org:blobId'};
    delete $have_jscard->{'cyrusimap.org:size'};

    # Normalize and compare cards
    normalize_jscard($want_jscard);
    normalize_jscard($have_jscard);
    $self->assert_deep_equals($want_jscard, $have_jscard);
}
