#!perl
use Cassandane::Tiny;

sub test_card_set_importance_peruser
    :min_version_3_5 :needs_component_jmap :needs_dependency_icalvcard
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    my $admin = $self->{adminstore}->get_client();

    $admin->create("user.manifold");
    my $http = $self->{instance}->get_service("http");
    my $manjmap = Mail::JMAPTalk->new(
        user => 'manifold',
        password => 'pass',
        host => $http->host(),
        port => $http->port(),
        scheme => 'http',
        url => '/jmap/',
    );
    $manjmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:contacts',
        'https://cyrusimap.org/ns/jmap/contacts',
    ]);
    $admin->setacl("user.cassandane.#addressbooks.Default",
        "manifold" => 'lrswipkxtecdn') or die;

    my $res = $jmap->CallMethods([
        ['ContactCard/set', {
            create => {
                c1 => {
                    name => { full => 'John Doe' },
                    'cyrusimap.org:importance' => 1.0,
                },
            },
        }, 'R1'],
        ['ContactCard/get', {
            ids => ['#c1'],
            properties => ['cyrusimap.org:importance'],
        }, 'R2'],
    ]);
    my $contactId = $res->[0][1]{created}{c1}{id};
    $self->assert_not_null($contactId);
    $self->assert_equals(1.0, $res->[1][1]{list}[0]{'cyrusimap.org:importance'});

    $res = $manjmap->CallMethods([
        ['ContactCard/get', {
            accountId => 'cassandane',
            ids => [$contactId],
            properties => ['cyrusimap.org:importance'],
        }, 'R1'],
        ['ContactCard/set', {
            accountId => 'cassandane',
            update => {
                $contactId => {
                    'cyrusimap.org:importance' => 2.0,
                },
            },
        }, 'R2'],
        ['ContactCard/get', {
            accountId => 'cassandane',
            ids => [$contactId],
            properties => ['cyrusimap.org:importance'],
        }, 'R3'],
    ]);

    $self->assert_equals(1.0, $res->[0][1]{list}[0]{'cyrusimap.org:importance'});
    $self->assert(exists $res->[1][1]{updated}{$contactId});
    $self->assert_equals(2.0, $res->[2][1]{list}[0]{'cyrusimap.org:importance'});

    $res = $jmap->CallMethods([
        ['ContactCard/get', {
            ids => ['#c1'],
            properties => ['cyrusimap.org:importance'],
        }, 'R1'],
    ]);
    $self->assert_equals(1.0, $res->[0][1]{list}[0]{'cyrusimap.org:importance'});
}
