#!perl
use Cassandane::Tiny;

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

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

    xlog "Set default alerts on calendar and event";
    my $res = $jmap->CallMethods([
        ['Calendar/set', {
            update => {
                Default => {
                    defaultAlertsWithTime => {
                        alert1 => {
                            '@type' => 'Alert',
                            trigger => {
                                '@type' => 'OffsetTrigger',
                                relativeTo => 'start',
                                offset => '-PT5M',
                            },
                            action => 'display',
                        },
                    },
                }
            }
        }, 'R1'],
        ['CalendarEvent/set', {
            create => {
                1 => {
                    uid => 'eventuid1local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event1",
                    start => "2020-01-19T11:00:00",
                    duration => "PT1H",
                    timeZone => "Australia/Melbourne",
                    useDefaultAlerts => JSON::true,
                },
                2 => {
                    uid => 'eventuid2local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event1",
                    start => "2020-01-21T11:00:00",
                    duration => "PT1H",
                    timeZone => "Australia/Melbourne",
                    useDefaultAlerts => JSON::false,
                },
            },
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{Default});
    my $event1Href = $res->[1][1]{created}{1}{'x-href'};
    $self->assert_not_null($event1Href);
    my $event2Href = $res->[1][1]{created}{2}{'x-href'};
    $self->assert_not_null($event2Href);

    xlog "Get ETags of events";
    my %Headers;
    if ($CalDAV->{user}) {
        $Headers{'Authorization'} = $CalDAV->auth_header();
    }
    my $event1URI = $CalDAV->request_url($event1Href);
    my $Response = $CalDAV->{ua}->request('HEAD', $event1URI, {
            headers => \%Headers,
    });
    my $event1ETag = $Response->{headers}{etag};
    $self->assert_not_null($event1ETag);
    my $event2URI = $CalDAV->request_url($event2Href);
    $Response = $CalDAV->{ua}->request('HEAD', $event2URI, {
            headers => \%Headers,
    });
    my $event2ETag = $Response->{headers}{etag};
    $self->assert_not_null($event2ETag);

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

    xlog "Refetch ETags of events";
    $Response = $CalDAV->{ua}->request('HEAD', $event1URI, {
            headers => \%Headers,
    });
    $self->assert_not_null($Response->{headers}{etag});
    $self->assert_str_not_equals($event1ETag, $Response->{headers}{etag});
    $Response = $CalDAV->{ua}->request('HEAD', $event2URI, {
            headers => \%Headers,
    });
    $self->assert_not_null($Response->{headers}{etag});
    $self->assert_str_equals($event2ETag, $Response->{headers}{etag});
}
