#!perl
use Cassandane::Tiny;

sub test_calendar_defaultalerts_synctoken
    :min_version_3_9 :needs_component_jmap
{
    my ($self) = @_;

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

    xlog "Set default alerts on calendar";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    defaultAlertsWithTime => {
                        alert1 => {
                            '@type' => 'Alert',
                            trigger => {
                                '@type' => 'OffsetTrigger',
                                relativeTo => 'start',
                                offset => '-PT5M',
                            },
                            action => 'display',
                        },
                    },
                }
            }
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    xlog "Create events with and without default alerts";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                1 => {
                    uid => 'eventuid1local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event1",
                    start => "2020-01-19T11:00:00",
                    duration => "PT1H",
                    timeZone => "Australia/Melbourne",
                    alerts => {
                        alert1 => {
                            trigger => {
                                '@type' => 'OffsetTrigger',
                                offset => "-PT10M",
                            },
                         },
                    },
                },
                2 => {
                    uid => 'eventuid2local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event2",
                    start => "2020-01-21T13:00:00",
                    duration => "PT1H",
                    timeZone => "Europe/Vienna",
                    useDefaultAlerts => JSON::true,
                },
            },
        }, 'R1'],
    ]);
    my $event1Uid = $res->[0][1]{created}{1}{uid};
    $self->assert_not_null($event1Uid);
    my $event2Uid = $res->[0][1]{created}{2}{uid};
    $self->assert_not_null($event2Uid);

    my $using = [
        'urn:ietf:params:jmap:core',
        'urn:ietf:params:jmap:calendars',
        'https://cyrusimap.org/ns/jmap/calendars',
    ];

    xlog "Fetch sync token";
    my $Cal = $CalDAV->GetCalendar('Default');
    my $syncToken = $Cal->{syncToken};
    $self->assert_not_null($syncToken);

    xlog "Update default alerts on calendar";
    $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    defaultAlertsWithTime => {
                        alert2 => {
                            '@type' => 'Alert',
                            trigger => {
                                '@type' => 'OffsetTrigger',
                                relativeTo => 'start',
                                offset => '-PT15M',
                            },
                            action => 'display',
                        },
                    },
                }
            }
        }, 'R1'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});

    xlog "Sync CalDAV changes";
    my ($adds, $removes, $errors) = $CalDAV->SyncEvents('Default', syncToken => $syncToken);

    $self->assert_num_equals(1, scalar @{$adds});
    $self->assert_str_equals($adds->[0]{uid}, $event2Uid);
    $self->assert_deep_equals($removes, []);
    $self->assert_deep_equals($errors, []);
}
