#!perl
use Cassandane::Tiny;

sub test_card_set_create_personal
    :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 $res = $jmap->CallMethods([
        ['ContactCard/set', {
            create => {
                "1" => {
                    '@type' => 'Card',
                    version => '1.0',
                    uid => $id,
                    name => { full => 'Jane Doe' },
                    personalInfo => {
                        'PERSINFO-1'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'expertise',
                            value => 'chinese literature',
                            level => 'low',
                            listAs => 2
                        },
                        'PERSINFO-2'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'expertise',
                            value => 'chemistry',
                            level => 'high',
                            listAs => 1
                        },
                        'PERSINFO-3'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'hobby',
                            value => 'reading',
                            level => 'low',
                            listAs => 1
                        },
                        'PERSINFO-4'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'hobby',
                            value => 'sewing',
                            level => 'high',
                            listAs => 2
                        },
                        'PERSINFO-5'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'interest',
                            value => 'r&b music',
                            level => 'medium',
                            listAs => 1
                        },
                        'PERSINFO-6'  => {
                            '@type' => 'PersonalInfo',
                            kind => 'interest',
                            value => 'rock&roll music',
                            level => 'high',
                            listAs => 2
                        }
                    }
                }
            }
        }, 'R1']
    ]);

    $self->assert_not_null($res->[0][1]{created}{1});

    my $href = $res->[0][1]{created}{1}{'cyrusimap.org:href'};
    $res = $carddav->Request('GET', $href, '',
                             'Accept' => 'text/vcard; version=4.0');

    my $card = $res->{content};
    $card =~ s/\r?\n[ \t]+//gs;  # unfold long properties

    $self->assert_matches(qr|EXPERTISE;PROP-ID=PERSINFO-1;LEVEL=BEGINNER;INDEX=2:chinese literature|, $card);
    $self->assert_matches(qr|EXPERTISE;PROP-ID=PERSINFO-2;LEVEL=EXPERT;INDEX=1:chemistry|, $card);
    $self->assert_matches(qr|HOBBY;PROP-ID=PERSINFO-3;LEVEL=LOW;INDEX=1:reading|, $card);
    $self->assert_matches(qr|HOBBY;PROP-ID=PERSINFO-4;LEVEL=HIGH;INDEX=2:sewing|, $card);
    $self->assert_matches(qr|INTEREST;PROP-ID=PERSINFO-5;LEVEL=MEDIUM;INDEX=1:r&b music|, $card);
    $self->assert_matches(qr|INTEREST;PROP-ID=PERSINFO-6;LEVEL=HIGH;INDEX=2:rock&roll music|, $card);
    $self->assert_does_not_match(qr|JSPROP|, $card);
}
