#!perl
use Cassandane::Tiny;

sub test_calendarsharenotification_get
    :min_version_3_3 :needs_component_jmap
{
    my ($self) = @_;

    my $jmap = $self->{jmap};

    # Create sharee
    my $admin = $self->{adminstore}->get_client();
    $admin->create("user.manifold");
    my $http = $self->{instance}->get_service("http");
    my $mantalk = Net::CalDAVTalk->new(
        user => "manifold",
        password => 'pass',
        host => $http->host(),
        port => $http->port(),
        scheme => 'http',
        url => '/',
        expandurl => 1,
    );
    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:calendars',
        'urn:ietf:params:jmap:principals',
        'https://cyrusimap.org/ns/jmap/calendars',
    ]);

    my $res = $manjmap->CallMethods([
        ['ShareNotification/get', {
        }, 'R1']
    ]);
    $self->assert_num_equals(0, scalar @{$res->[0][1]{list}});
    my $state = $res->[0][1]{state};

    $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    name => 'myname',
                    "shareWith/manifold" => {
                        mayReadFreeBusy => JSON::true,
                        mayReadItems => JSON::true,
                    },
                },
            },
        }, "R1"]
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    $res = $manjmap->CallMethods([
        ['ShareNotification/get', {
        }, 'R1']
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});

    my $notif = $res->[0][1]{list}[0];
    # Assert dynamically generated values.
    my $notifId = $notif->{id};
    $self->assert_not_null($notifId);
    delete($notif->{id});
    $self->assert_not_null($notif->{created});
    delete($notif->{created});
    # Assert remaining values.
    $self->assert_deep_equals({
        changedBy => {
            name => 'Test User',
            email => 'cassandane@example.com',
            principalId => 'cassandane',
        },
        objectType => 'Calendar',
        objectAccountId => 'cassandane',
        objectId => 'Default',
        oldRights => undef,
        newRights => {
            mayReadFreeBusy => JSON::true,
            mayReadItems => JSON::true,
            mayWriteAll => JSON::false,
            mayRSVP => JSON::false,
            mayDelete => JSON::false,
            mayAdmin => JSON::false,
            mayUpdatePrivate => JSON::false,
            mayWriteOwn => JSON::false,
        },
    }, $notif);

    $res = $manjmap->CallMethods([
        ['ShareNotification/get', {
            ids => [$notifId, 'nope'],
        }, 'R1']
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});
    $self->assert_deep_equals(['nope'], $res->[0][1]{notFound});
    $self->assert_str_not_equals($state, $res->[0][1]{state});
}
