#!perl
use Cassandane::Tiny;

my sub groups_from_vcardhash ($input) {
    my $props = $input->{properties};
    my %group;
    for my $key (keys %$props) {
        for my $entry ($props->{$key}->@*) {
            my $gname = $entry->{group} // "";

            $group{ $gname }{$key} = $entry->{value};
        }
    }

    return \%group;
}

sub test_card_apple_label_handling
    ($self)
{
    my $user = $self->default_user;
    my $jmap = $user->jmap; 
    my $carddav = $user->carddav;

    xlog $self, "create a contact with 3 labels: unassociated, shared, & unshared";
    my $id = 'ae2640cc-234a-4dd9-95cc-3106258445b9';
    my $href = "Default/$id.vcf";
    my $card = <<EOF;
BEGIN:VCARD
VERSION:4.0
UID:$id
FN:Forrest Gump
X-ABLabel:this-should-not-crash-cyrus
foo.ADR;PROP-ID=a1;LABEL="My Address":
 ;;123 Main Street;Any Town;LA;91921-1234;U.S.A.
foo.X-ABLabel:_\$!<foo>!\$_
foo.EMAIL;PROP-ID=e1;TYPE=work:bubba\@local
bar.X-ABLabel:bar
bar.TEL;PROP-ID=p1;VALUE=uri;TYPE="home":tel:+1-555-555-5555
email0.X-ABLabel:aaa
email0.EMAIL;PROP-ID=e0;TYPE=work:shrimp\@local
END:VCARD
EOF

    $card =~ s/\r?\n/\r\n/gs;

    $carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

    my $res = $jmap->request([[
        'ContactCard/get' => {
            properties => ['addresses', 'emails', 'phones'],
        },
    ]])->single_sentence("ContactCard/get")->arguments;

    $self->assert_cmp_deeply(
        superhashof({
            addresses => {
                a1 => superhashof({ label => 'foo' })
            },
            emails => {
                e0 => superhashof({ label => 'aaa' }),
                e1 => superhashof({ label => 'foo' })
            },
            phones => {
                p1 => superhashof({ label => 'bar' })
            }
        }),
        $res->{list}[0]
    );
    $id = $res->{list}[0]{id};

    xlog $self, "update contact";
    $res = $user->jmap->request([[
        'ContactCard/set' => {
            update => {
                $id => {
                    'emails/e1/label' => JSON::null,
                    'emails/e2' => {       
                        contexts => { private => JSON::true },
                        label => "bbb",
                        address => "gump\@local"
                    },
                    'phones/p1/label' => JSON::null
                }
            }
        },
    ]])->single_sentence("ContactCard/set")->arguments;
    $self->assert(exists $res->{updated}{$id});

    $res = $jmap->request([[
        'ContactCard/get' => {
            properties => ['addresses', 'emails', 'phones'],
        },
    ]])->single_sentence("ContactCard/get")->arguments;

    $self->assert_cmp_deeply(
        superhashof({
            addresses => {
                a1 => superhashof({ label => 'foo', }),
            },
            emails => {
                e0 => superhashof({ label => 'aaa' }),
                e1 => { address => ignore(), contexts => ignore() },
                e2 => superhashof({ label => 'bbb' }),
            },
            phones => {
                p1 => { number => ignore(), contexts => ignore() },
            }
        }),
        $res->{list}[0]
    );

    xlog $self, "download and check content";
    $card = $user->contacts->get($id)->as_vcard4_struct;

    $self->assert_cmp_deeply(
        superhashof({
            '' => superhashof({
                email       => 'bubba@local',
                tel         => 'tel:+1-555-555-5555',
                'x-ablabel' => 'this-should-not-crash-cyrus',
            }),
            'adr0'   => superhashof({ 'x-ablabel' => 'foo' }),
            'email0' => superhashof({ 'x-ablabel' => 'aaa' }),
            'email1' => superhashof({ 'x-ablabel' => 'bbb' }),
        }),
        groups_from_vcardhash($card)
    );        
}
