#!perl
use Cassandane::Tiny;

sub test_calendareventnotification_get
    :min_version_3_3 :needs_component_jmap
{
    my ($self) = @_;
    my $jmap = $self->{jmap};
    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',
    ]);

    xlog "Create event";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    shareWith => {
                        manifold => {
                            mayReadFreeBusy => JSON::true,
                            mayReadItems => JSON::true,
                            mayUpdatePrivate => JSON::true,
                            mayWriteOwn => JSON::true,
                            mayAdmin => JSON::false
                        },
                    },
                },
            },
        }, 'R1'],
        ['CalendarEvent/set', {
            create => {
                event1 => {
                    title => 'event1',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    start => '2011-01-01T04:05:06',
                    duration => 'PT1H',
                },
            },
        }, 'R2'],
        ['CalendarEventNotification/get', {
        }, 'R3'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});
    my $eventId = $res->[1][1]{created}{event1}{id};
    $self->assert_not_null($eventId);
    # Event creator is not notified.
    $self->assert_num_equals(0, scalar @{$res->[2][1]{list}});

    # Event sharee is notified.
    $res = $manjmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
        }, 'R1'],
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});
    $self->assert_str_equals('created', $res->[0][1]{list}[0]{type});
    my $notif1 = $res->[0][1]{list}[0]{id};

    xlog "Update event";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    title => 'event1updated',
                },
            },
        }, 'R1'],
        ['CalendarEventNotification/get', {
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventId});
    # Event updater is not notified.
    $self->assert_num_equals(0, scalar @{$res->[1][1]{list}});
    # Event sharee is notified.
    $res = $manjmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
        }, 'R1'],
    ]);
    $self->assert_num_equals(2, scalar @{$res->[0][1]{list}});

    my %notifs = map { $_->{type} => $_ } @{$res->[0][1]{list}};
    $self->assert_str_equals($notif1, $notifs{created}{id});
    my $notif2 = $notifs{updated}{id};
    $self->assert_str_not_equals($notif2, $notif1);

    xlog "Destroy event";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            destroy => [$eventId],
        }, 'R1'],
        ['CalendarEventNotification/get', {
        }, 'R2'],
    ]);
    $self->assert_deep_equals([$eventId], $res->[0][1]{destroyed});
    # Event destroyer is not notified.
    $self->assert_num_equals(0, scalar @{$res->[2][1]{list}});

    # Event sharee only sees destroy notification.
    $res = $manjmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
        }, 'R1'],
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});
    $self->assert_str_not_equals($notif1, $res->[0][1]{list}[0]{id});
    $self->assert_str_not_equals($notif2, $res->[0][1]{list}[0]{id});
    $self->assert_str_equals('destroyed', $res->[0][1]{list}[0]{type});
}
