#!perl
use Cassandane::Tiny;

sub test_calendareventnotification_prune
    :min_version_3_9 :needs_component_jmap :JmapMaxCalendarEventNotifs
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $jmap_max_calendareventnotifs = $self->{instance}->{
            config}->get('jmap_max_calendareventnotifs');
    $self->assert_not_null($jmap_max_calendareventnotifs);

    my ($manJmap) = $self->create_user('manifold');
    $manJmap->DefaultUsing([
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:calendars',
        'urn:ietf:params:jmap:principals',
        'https://cyrusimap.org/ns/jmap/calendars',
    ]);

    xlog $self, "Share calendar";
    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'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    xlog $self, "Create event notification";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                event1 => {
                    title => 'event1',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    start => '2011-01-01T04:05:06',
                    duration => 'PT1H',
                },
            },
        }, 'R1'],
    ]);
    $self->assert_not_null($res->[0][1]{created}{event1});

    xlog $self, "Get event notification";
    $res = $manJmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
        }, 'R1'],
    ]);
    $self->assert_num_equals(1, scalar @{$res->[0][1]{list}});
    my $notif1Id = $res->[0][1]{list}[0]{id};
    $self->assert_not_null($notif1Id);

    xlog $self, "Create maximum count of allowed notifications";
    for my $i (2 .. $jmap_max_calendareventnotifs) {
        my $res = $jmap->CallMethods([
            ['CalendarEvent/set', {
                create => {
                    "event$i" => {
                        title => "event$i",
                        calendarIds => {
                            Default => JSON::true,
                        },
                        start => '2011-01-01T04:05:06',
                        duration => 'PT1H',
                    },
                },
            }, 'R1'],
        ]);
        $self->assert_not_null($res->[0][1]{created}{"event$i"});
    }

    xlog $self, "Get event notifications";
    $res = $manJmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
            properties => ['id'],
        }, 'R1'],
    ]);
    $self->assert_num_equals($jmap_max_calendareventnotifs, scalar @{$res->[0][1]{list}});

    xlog $self, "Assert first event notification exists";
    $self->assert_equals(1, scalar grep { $_->{id} eq $notif1Id } @{$res->[0][1]{list}});

    xlog $self, "Create one more event notification";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                eventX => {
                    title => 'eventX',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    start => '2011-01-01T04:05:06',
                    duration => 'PT1H',
                },
            },
        }, 'R1'],
    ]);
    $self->assert_not_null($res->[0][1]{created}{eventX});

    xlog $self, "Get event notifications";
    $res = $manJmap->CallMethods([
        ['CalendarEventNotification/get', {
            accountId => 'cassandane',
            properties => ['id'],
        }, 'R1'],
    ]);
    $self->assert_num_equals($jmap_max_calendareventnotifs, scalar @{$res->[0][1]{list}});

    xlog $self, "Assert first event notification does not exist";
    $self->assert_equals(0, scalar grep { $_->{id} eq $notif1Id } @{$res->[0][1]{list}});
}
