#!perl
use Cassandane::Tiny;

sub test_calendarevent_set_updated_scheduled_source
    :min_version_3_7
{
    my ($self) = @_;
    my $jmap = $self->{jmap};

    my $t = DateTime->now();
    $t->set_time_zone('Etc/UTC');
    my $start = $t->strftime('%Y-%m-%dT%H:%M:%S');
    my $now= $t->strftime('%Y-%m-%dT%H:%M:%SZ');
    $t->add(DateTime::Duration->new(days => -2));
    my $past = $t->strftime('%Y-%m-%dT%H:%M:%SZ');

    xlog "Create event where cassandane is owner";
    my $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            create => {
                event => {
                    calendarIds => {
                        'Default' => JSON::true,
                    },
                    start => $start,
                    timeZone => 'Etc/UTC',
                    duration => 'PT1H',
                    title => 'event',
                    created => $past,
                    updated => $past,
                    replyTo => {
                        imip => 'mailto:cassandane@example.com',
                    },
                    participants => {
                        someone => {
                            sendTo => {
                                imip => 'mailto:someone@example.com',
                            },
                            expectReply => JSON::true,
                            participationStatus => 'needs-action',
                        },
                    },
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [ '#event' ],
            properties => ['updated'],
        }, 'R2'],
    ]);
    my $updated = $res->[1][1]{list}[0]{updated};
    $self->assert($past lt $updated);
    my $eventId = $res->[1][1]{list}[0]{id};

    sleep(1);

    xlog "Invite someone else";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    'participants/someoneelse' => {
                        sendTo => {
                            imip => 'mailto:someoneelse@example.com',
                        },
                        expectReply => JSON::true,
                        participationStatus => 'needs-action',
                    },
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [ '#event' ],
            properties => ['updated'],
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventId});
    $self->assert($updated lt $res->[1][1]{list}[0]{updated});
    $updated = $res->[1][1]{list}[0]{updated};

    xlog "Client updates updated property themselves";
    $res = $jmap->CallMethods([
        ['CalendarEvent/set', {
            update => {
                $eventId => {
                    updated => $past,
                },
            },
        }, 'R1'],
        ['CalendarEvent/get', {
            ids => [ '#event' ],
            properties => ['updated'],
        }, 'R2'],
    ]);
    $self->assert(exists $res->[0][1]{updated}{$eventId});
    $self->assert_date_matches($updated, $res->[1][1]{list}[0]{updated}, 2);
}
