#!perl
use Cassandane::Tiny;

sub test_ajaxui_jmapcontacts_contactgroup_set
    :min_version_3_1
    :JMAPExtensions
{
    my ($self) = @_;

    my $jmap = $self->{jmap};
    my $service = $self->{instance}->get_service("http");

    my $admintalk = $self->{adminstore}->get_client();
    $admintalk->create("user.masteruser");
    $admintalk->setacl("user.masteruser", admin => 'lrswipkxtecdan');
    $admintalk->setacl("user.masteruser", masteruser => 'lrswipkxtecdn');
    $admintalk->create("user.masteruser.#addressbooks.Default", ['TYPE', 'ADDRESSBOOK']);
    $admintalk->create("user.masteruser.#addressbooks.Shared", ['TYPE', 'ADDRESSBOOK']);
    $admintalk->setacl("user.masteruser.#addressbooks.Default", "masteruser" => 'lrswipkxtecdn') or die;
    $admintalk->setacl("user.masteruser.#addressbooks.Shared", "masteruser" => 'lrswipkxtecdn') or die;
    $admintalk->setacl("user.masteruser.#addressbooks.Shared", "cassandane" => 'lrswipkxtecdn') or die;

    my $mastertalk = Net::CardDAVTalk->new(
        user => "masteruser",
        password => 'pass',
        host => $service->host(),
        port => $service->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );

    my $res;

    xlog $self, "create contact group";
    $res = $self->_fmjmap_ok('ContactGroup/set',
        accountId => 'cassandane',
        create => {
            "k2519" => {
                name => "personal group",
                addressbookId => 'Default',
                contactIds => [],
                otherAccountContactIds => {
                    masteruser => [],
                },
            },
        },
        update => {},
        destroy => [],
    );
    my $groupid = $res->{created}{"k2519"}{id};
    $self->assert_not_null($groupid);

    $res = $self->_fmjmap_ok('ContactGroup/get',
        ids => [$groupid],
    );

    $self->assert_num_equals(1, scalar @{$res->{list}});
    # check the rest?

    xlog $self, "create contact group";
    $res = $self->_fmjmap_ok('ContactGroup/set',
        accountId => 'masteruser',
        create => {
            "k2520" => {
                name => "shared group",
                addressbookId => 'Shared',
                contactIds => [],
                otherAccountContactIds => {},
            },
        },
        update => {},
        destroy => [],
    );
    my $sgroupid = $res->{created}{"k2520"}{id};
    $self->assert_not_null($sgroupid);

    xlog $self, "create invalid shared contact group";
    $res = $self->_fmjmap_ok('ContactGroup/set',
        accountId => 'masteruser',
        create => {
            "k2521" => {
                name => "invalid group",
                addressbookId => 'Default',
                contactIds => [],
                otherAccountContactIds => {},
            },
        },
        update => {},
        destroy => [],
    );

    $self->assert_not_null($res->{notCreated}{"k2521"});
    $self->assert_null($res->{created}{"k2521"});

    # now let's create a contact and put it in the event...
}
