#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_utcstart
    :min_version_3_1 :needs_component_jmap
{
    my ($self) = @_;

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

    # Assert event creation.
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                1 => {
                    uid => 'eventuid1local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event1",
                    utcStart => "2019-12-10T23:30:00Z",
                    duration => "PT1H",
                    timeZone => "Australia/Melbourne",
                },
                2 => {
                    uid => 'eventuid2local',
                    calendarIds => {
                        Default => JSON::true,
                    },
                    title => "event2",
                    utcStart => "2019-12-10T23:30:00Z",
                    duration => "PT1H",
                    timeZone => undef, # floating
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => ['#1'],
            properties => ['start', 'utcStart', 'utcEnd', 'timeZone', 'duration'],
        }, 'R2'],
        ['CalendarEvent/get', {
            ids => ['#2'],
            properties => ['start', 'utcStart', 'utcEnd', 'timeZone', 'duration'],
        }, 'R3']
    ]);
    my $eventId1 = $res->[0][1]{created}{1}{id};
    $self->assert_not_null($eventId1);
    my $eventId2 = $res->[0][1]{created}{2}{id};
    $self->assert_not_null($eventId2);

    my $event1 = $res->[1][1]{list}[0];
    $self->assert_str_equals('2019-12-11T10:30:00', $event1->{start});
    $self->assert_str_equals('2019-12-10T23:30:00Z', $event1->{utcStart});
    $self->assert_str_equals('2019-12-11T00:30:00Z', $event1->{utcEnd});
    $self->assert_str_equals('Australia/Melbourne', $event1->{timeZone});
    $self->assert_str_equals('PT1H', $event1->{duration});

    my $event2 = $res->[2][1]{list}[0];
    $self->assert_str_equals('2019-12-10T23:30:00', $event2->{start});
    $self->assert_str_equals('2019-12-10T23:30:00Z', $event2->{utcStart});
    $self->assert_str_equals('2019-12-11T00:30:00Z', $event2->{utcEnd});
    $self->assert_str_equals('Etc/UTC', $event2->{timeZone});
    $self->assert_str_equals('PT1H', $event2->{duration});

    # Assert event updates.
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId1 => {
                    utcStart => "2019-12-11T01:30:00Z",
                },
                $eventId2 => {
                    utcStart => "2019-12-10T11:30:00Z",
                    duration => 'PT30M',
                    timeZone => 'America/New_York',
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [$eventId1],
            properties => ['start', 'utcStart', 'utcEnd', 'timeZone', 'duration'],
        }, 'R2'],
        ['CalendarEvent/get', {
            ids => [$eventId2],
            properties => ['start', 'utcStart', 'utcEnd', 'timeZone', 'duration'],
        }, 'R3']
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventId1});

    $event1 = $res->[1][1]{list}[0];
    $self->assert_str_equals('2019-12-11T12:30:00', $event1->{start});
    $self->assert_str_equals('2019-12-11T01:30:00Z', $event1->{utcStart});
    $self->assert_str_equals('2019-12-11T02:30:00Z', $event1->{utcEnd});
    $self->assert_str_equals('Australia/Melbourne', $event1->{timeZone});
    $self->assert_str_equals('PT1H', $event1->{duration});

    $event2 = $res->[2][1]{list}[0];
    $self->assert_str_equals('2019-12-10T06:30:00', $event2->{start});
    $self->assert_str_equals('2019-12-10T11:30:00Z', $event2->{utcStart});
    $self->assert_str_equals('2019-12-10T12:00:00Z', $event2->{utcEnd});
    $self->assert_str_equals('America/New_York', $event2->{timeZone});
    $self->assert_str_equals('PT30M', $event2->{duration});
}
